-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from lion-packages/new
Extended helper support
- Loading branch information
Showing
5 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lion\Bundle\Helpers; | ||
|
||
/** | ||
* Environment variable manager | ||
* | ||
* @package Lion\Bundle\Helpers | ||
*/ | ||
class Env | ||
{ | ||
/** | ||
* Gets the value defined for an environment variable | ||
* | ||
* @param string $key [Property name] | ||
* @param mixed $default [Default value] | ||
* | ||
* @return mixed | ||
*/ | ||
public static function get(string $key, mixed $default = null): mixed | ||
{ | ||
return self::getOption($key, $default); | ||
} | ||
|
||
/** | ||
* Gets and transforms the possible value of environment variables | ||
* | ||
* @param string $key [Property name] | ||
* @param mixed $default [Default value] | ||
* | ||
* @return mixed | ||
*/ | ||
private static function getOption(string $key, mixed $default): mixed | ||
{ | ||
$transform = function(mixed $value): mixed { | ||
switch ($value) { | ||
case 'true': | ||
case '(true)': | ||
return true; | ||
case 'false': | ||
case '(false)': | ||
return false; | ||
case 'empty': | ||
case '(empty)': | ||
return ''; | ||
case 'null': | ||
case '(null)': | ||
return null; | ||
default: | ||
return $value; | ||
} | ||
}; | ||
|
||
if (empty($_ENV[$key])) { | ||
return $transform($default); | ||
} | ||
|
||
$value = $transform($_ENV[$key]); | ||
|
||
if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) { | ||
return $matches[2]; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lion\Bundle\Helpers; | ||
|
||
use Faker\Factory; | ||
use Faker\Generator; | ||
|
||
/** | ||
* Create Generator objects with a single instance | ||
* | ||
* @package Lion\Bundle\Helpers | ||
*/ | ||
class Fake | ||
{ | ||
/** | ||
* [Generator class object] | ||
* | ||
* @var Generator|null $generator | ||
*/ | ||
private static ?Generator $generator = null; | ||
|
||
/** | ||
* Function that generates a Generator object to obtain fake data | ||
* | ||
* @param string $locale [Regional configuration] | ||
* | ||
* @return Generator | ||
*/ | ||
public static function get(string $locale = Factory::DEFAULT_LOCALE): Generator | ||
{ | ||
if (null === self::$generator) { | ||
self::$generator = Factory::create($locale); | ||
} | ||
|
||
return self::$generator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers\Helpers; | ||
|
||
trait HelpersProviderTrait | ||
{ | ||
public static function envProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'envKey' => 'APP_NAME', | ||
'envValue' => 'lion-bundle', | ||
'return' => 'lion-bundle' | ||
], | ||
[ | ||
'envKey' => 'SERVER_URL', | ||
'envValue' => 'http://127.0.0.1:8000', | ||
'return' => 'http://127.0.0.1:8000' | ||
], | ||
[ | ||
'envKey' => 'APP_NAME_TEST', | ||
'envValue' => 'lion-bundle', | ||
'return' => 'lion-bundle' | ||
], | ||
[ | ||
'envKey' => 'SERVER_URL_TEST', | ||
'envValue' => 'http://127.0.0.1:8000', | ||
'return' => 'http://127.0.0.1:8000' | ||
], | ||
[ | ||
'envKey' => 'MAIL_DEBUG_SUPP_TEST', | ||
'envValue' => 'true', | ||
'return' => true | ||
], | ||
[ | ||
'envKey' => 'MAIL_DEBUG_SUPP_TEST', | ||
'envValue' => '(true)', | ||
'return' => true | ||
], | ||
[ | ||
'envKey' => 'MAIL_DEBUG_SUPP_TEST', | ||
'envValue' => 'false', | ||
'return' => false | ||
], | ||
[ | ||
'envKey' => 'MAIL_DEBUG_SUPP_TEST', | ||
'envValue' => '(false)', | ||
'return' => false | ||
], | ||
[ | ||
'envKey' => 'APP_DEBUG', | ||
'envValue' => 'empty', | ||
'return' => '' | ||
], | ||
[ | ||
'envKey' => 'APP_DEBUG', | ||
'envValue' => '(empty)', | ||
'return' => '' | ||
], | ||
[ | ||
'envKey' => 'APP_DEBUG_TEST', | ||
'envValue' => 'null', | ||
'return' => null | ||
], | ||
[ | ||
'envKey' => 'APP_DEBUG_TEST', | ||
'envValue' => '(null)', | ||
'return' => null | ||
] | ||
]; | ||
} | ||
} |