diff --git a/README.md b/README.md index 65d7fd2..81d0c23 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ DATABASE_DNS=mysql:host=localhost;dbname=test; DATABASE_USER="root" DATABASE_PASSWORD=root MODULE_ENABLED=true +NUMBER_LITERAL=0 +NULL_VALUE=null ``` ## Load the variables @@ -57,6 +59,20 @@ var_dump(getenv('DATABASE_USER')); * bool(true) */ var_dump(getenv('MODULE_ENABLED')); + +/** + * Process the numeric value: + * + * int(0) + */ +var_dump(getenv('NUMBER_LITERAL')); + +/** + * Check for literal null values: + * + * NULL + */ +var_dump(getenv('NULL_VALUE')); ``` Ideal for small project @@ -76,3 +92,13 @@ NOTE: ``VARIABLE="true"`` will be processed to ```string(4) "true"``` ## QuotedProcessor ``VARIABLE="anything"`` will be processed to ```string(8) "anything"``` + +## NullProcessor + +``VARIABLE=null`` will be processed to ```NULL``` + +## NumberProcessor + +``VARIABLE=0`` will be processed to ```int(0)``` + +``VARIABLE=0.1`` will be processed to ```float(0.1)``` \ No newline at end of file diff --git a/composer.json b/composer.json index c0af1fc..5e7833b 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,14 @@ "Test\\DevCoder\\": "tests" } }, + "scripts": { + "test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --testdox", + "test:coverage": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --testdox --coverage-clover build/logs/clover.xml --coverage-html build/coverage" + }, "require": { "php": ">=7.4" }, "require-dev": { - "phpunit/phpunit": "^9.4" + "phpunit/phpunit": "9.6.3" } } diff --git a/composer.lock b/composer.lock index 43b94da..4b41d26 100644 --- a/composer.lock +++ b/composer.lock @@ -4,35 +4,35 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4b5a9a3f0dd94d1a1d1a3b00d1705fe3", + "content-hash": "c275b361e97ea353ec521ca4c3fc2c1d", "packages": [], "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.4.1", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "10dcfce151b967d20fde1b34ae6640712c3891bc" + "reference": "c6222283fa3f4ac679f8b9ced9a4e23f163e80d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/10dcfce151b967d20fde1b34ae6640712c3891bc", - "reference": "10dcfce151b967d20fde1b34ae6640712c3891bc", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/c6222283fa3f4ac679f8b9ced9a4e23f163e80d0", + "reference": "c6222283fa3f4ac679f8b9ced9a4e23f163e80d0", "shasum": "" }, "require": { - "php": "^7.1 || ^8.0" + "php": "^8.1" }, "require-dev": { - "doctrine/coding-standard": "^9", + "doctrine/coding-standard": "^11", "ext-pdo": "*", "ext-phar": "*", - "phpbench/phpbench": "^0.16 || ^1", - "phpstan/phpstan": "^1.4", - "phpstan/phpstan-phpunit": "^1", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", - "vimeo/psalm": "^4.22" + "phpbench/phpbench": "^1.2", + "phpstan/phpstan": "^1.9.4", + "phpstan/phpstan-phpunit": "^1.3", + "phpunit/phpunit": "^9.5.27", + "vimeo/psalm": "^5.4" }, "type": "library", "autoload": { @@ -59,7 +59,7 @@ ], "support": { "issues": "https://github.com/doctrine/instantiator/issues", - "source": "https://github.com/doctrine/instantiator/tree/1.4.1" + "source": "https://github.com/doctrine/instantiator/tree/2.0.0" }, "funding": [ { @@ -75,7 +75,7 @@ "type": "tidelift" } ], - "time": "2022-03-03T08:28:38+00:00" + "time": "2022-12-30T00:23:10+00:00" }, { "name": "myclabs/deep-copy", @@ -138,16 +138,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.15.2", + "version": "v4.15.3", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc" + "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc", - "reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/570e980a201d8ed0236b0a62ddf2c9cbb2034039", + "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039", "shasum": "" }, "require": { @@ -188,9 +188,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.3" }, - "time": "2022-11-12T15:38:23+00:00" + "time": "2023-01-16T22:05:37+00:00" }, { "name": "phar-io/manifest", @@ -305,16 +305,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.21", + "version": "9.2.24", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "3f893e19712bb0c8bc86665d1562e9fd509c4ef0" + "reference": "2cf940ebc6355a9d430462811b5aaa308b174bed" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3f893e19712bb0c8bc86665d1562e9fd509c4ef0", - "reference": "3f893e19712bb0c8bc86665d1562e9fd509c4ef0", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2cf940ebc6355a9d430462811b5aaa308b174bed", + "reference": "2cf940ebc6355a9d430462811b5aaa308b174bed", "shasum": "" }, "require": { @@ -370,7 +370,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.21" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.24" }, "funding": [ { @@ -378,7 +378,7 @@ "type": "github" } ], - "time": "2022-12-14T13:26:54+00:00" + "time": "2023-01-26T08:26:55+00:00" }, { "name": "phpunit/php-file-iterator", @@ -623,20 +623,20 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.27", + "version": "9.6.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a2bc7ffdca99f92d959b3f2270529334030bba38" + "reference": "e7b1615e3e887d6c719121c6d4a44b0ab9645555" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a2bc7ffdca99f92d959b3f2270529334030bba38", - "reference": "a2bc7ffdca99f92d959b3f2270529334030bba38", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e7b1615e3e887d6c719121c6d4a44b0ab9645555", + "reference": "e7b1615e3e887d6c719121c6d4a44b0ab9645555", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.3.1", + "doctrine/instantiator": "^1.3.1 || ^2", "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", @@ -674,7 +674,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.5-dev" + "dev-master": "9.6-dev" } }, "autoload": { @@ -705,7 +705,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.27" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.3" }, "funding": [ { @@ -721,7 +721,7 @@ "type": "tidelift" } ], - "time": "2022-12-09T07:31:23+00:00" + "time": "2023-02-04T13:37:15+00:00" }, { "name": "sebastian/cli-parser", @@ -1089,16 +1089,16 @@ }, { "name": "sebastian/environment", - "version": "5.1.4", + "version": "5.1.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "1b5dff7bb151a4db11d49d90e5408e4e938270f7" + "reference": "830c43a844f1f8d5b7a1f6d6076b784454d8b7ed" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/1b5dff7bb151a4db11d49d90e5408e4e938270f7", - "reference": "1b5dff7bb151a4db11d49d90e5408e4e938270f7", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/830c43a844f1f8d5b7a1f6d6076b784454d8b7ed", + "reference": "830c43a844f1f8d5b7a1f6d6076b784454d8b7ed", "shasum": "" }, "require": { @@ -1140,7 +1140,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/environment/issues", - "source": "https://github.com/sebastianbergmann/environment/tree/5.1.4" + "source": "https://github.com/sebastianbergmann/environment/tree/5.1.5" }, "funding": [ { @@ -1148,7 +1148,7 @@ "type": "github" } ], - "time": "2022-04-03T09:37:03+00:00" + "time": "2023-02-03T06:03:51+00:00" }, { "name": "sebastian/exporter", @@ -1462,16 +1462,16 @@ }, { "name": "sebastian/recursion-context", - "version": "4.0.4", + "version": "4.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "cd9d8cf3c5804de4341c283ed787f099f5506172" + "reference": "e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/cd9d8cf3c5804de4341c283ed787f099f5506172", - "reference": "cd9d8cf3c5804de4341c283ed787f099f5506172", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1", + "reference": "e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1", "shasum": "" }, "require": { @@ -1510,10 +1510,10 @@ } ], "description": "Provides functionality to recursively process PHP variables", - "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "homepage": "https://github.com/sebastianbergmann/recursion-context", "support": { "issues": "https://github.com/sebastianbergmann/recursion-context/issues", - "source": "https://github.com/sebastianbergmann/recursion-context/tree/4.0.4" + "source": "https://github.com/sebastianbergmann/recursion-context/tree/4.0.5" }, "funding": [ { @@ -1521,7 +1521,7 @@ "type": "github" } ], - "time": "2020-10-26T13:17:30+00:00" + "time": "2023-02-03T06:07:39+00:00" }, { "name": "sebastian/resource-operations", @@ -1580,16 +1580,16 @@ }, { "name": "sebastian/type", - "version": "3.2.0", + "version": "3.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/type.git", - "reference": "fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e" + "reference": "75e2c2a32f5e0b3aef905b9ed0b179b953b3d7c7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e", - "reference": "fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/75e2c2a32f5e0b3aef905b9ed0b179b953b3d7c7", + "reference": "75e2c2a32f5e0b3aef905b9ed0b179b953b3d7c7", "shasum": "" }, "require": { @@ -1624,7 +1624,7 @@ "homepage": "https://github.com/sebastianbergmann/type", "support": { "issues": "https://github.com/sebastianbergmann/type/issues", - "source": "https://github.com/sebastianbergmann/type/tree/3.2.0" + "source": "https://github.com/sebastianbergmann/type/tree/3.2.1" }, "funding": [ { @@ -1632,7 +1632,7 @@ "type": "github" } ], - "time": "2022-09-12T14:47:03+00:00" + "time": "2023-02-03T06:13:03+00:00" }, { "name": "sebastian/version", @@ -1744,7 +1744,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.3" + "php": ">=7.4" }, "platform-dev": [], "plugin-api-version": "2.3.0" diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..31f0613 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,21 @@ + + + + + src + + + vendor + build + + + + + tests + + + + + + + \ No newline at end of file diff --git a/src/DotEnv.php b/src/DotEnv.php index 110ab60..37a013e 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -4,6 +4,8 @@ use DevCoder\Processor\AbstractProcessor; use DevCoder\Processor\BooleanProcessor; +use DevCoder\Processor\NullProcessor; +use DevCoder\Processor\NumberProcessor; use DevCoder\Processor\QuotedProcessor; use InvalidArgumentException; use RuntimeException; @@ -42,7 +44,9 @@ private function setProcessors(array $processors = null): self */ if ($processors === null) { $this->processors = [ + NullProcessor::class, BooleanProcessor::class, + NumberProcessor::class, QuotedProcessor::class ]; diff --git a/src/Processor/NullProcessor.php b/src/Processor/NullProcessor.php new file mode 100644 index 0000000..4653c3b --- /dev/null +++ b/src/Processor/NullProcessor.php @@ -0,0 +1,15 @@ +value, ['null', 'NULL'], true); + } + + public function execute() + { + return null; + } +} diff --git a/src/Processor/NumberProcessor.php b/src/Processor/NumberProcessor.php new file mode 100644 index 0000000..909cffd --- /dev/null +++ b/src/Processor/NumberProcessor.php @@ -0,0 +1,21 @@ +value); + } + + public function execute() + { + $int = filter_var($this->value, FILTER_VALIDATE_INT); + + if ($int !== false) { + return $int; + } + + return (float) $this->value; + } +} diff --git a/tests/DotenvTest.php b/tests/DotenvTest.php index 384c33f..e162bd4 100644 --- a/tests/DotenvTest.php +++ b/tests/DotenvTest.php @@ -3,10 +3,10 @@ namespace Test\DevCoder; use DevCoder\DotEnv; -use DevCoder\Option; use DevCoder\Processor\BooleanProcessor; +use DevCoder\Processor\NullProcessor; +use DevCoder\Processor\NumberProcessor; use DevCoder\Processor\QuotedProcessor; -use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; class DotenvTest extends TestCase @@ -46,6 +46,28 @@ public function testLoad() { $this->assertArrayNotHasKey('GOOGLE_MANAGER_KEY', $_SERVER); $this->assertEquals(true, $_SERVER['BOOLEAN_LITERAL']); $this->assertEquals('true', $_SERVER['BOOLEAN_QUOTED']); + + $this->assertEquals('🪄', $_SERVER['EMOJI']); + + $this->assertIsInt($_SERVER['ZERO_LITERAL']); + $this->assertEquals(0, $_SERVER['ZERO_LITERAL']); + + $this->assertIsString($_SERVER['ZERO_QUOTED']); + $this->assertEquals('0', $_SERVER['ZERO_QUOTED']); + + $this->assertIsInt($_SERVER['NUMBER_LITERAL']); + $this->assertEquals(1111, $_SERVER['NUMBER_LITERAL']); + + $this->assertIsString($_SERVER['NUMBER_QUOTED']); + $this->assertEquals('1111', $_SERVER['NUMBER_QUOTED']); + + $this->assertNull($_SERVER['NULL_LITERAL']); + $this->assertArrayHasKey('NULL_LITERAL', $_SERVER); + + $this->assertEquals('null', $_SERVER['NULL_QUOTED']); + + $this->assertEquals('', $_SERVER['EMPTY_LITERAL']); + $this->assertEquals('', $_SERVER['EMPTY_QUOTED']); } public function testFileNotExist() { @@ -53,6 +75,9 @@ public function testFileNotExist() { (new DotEnv($this->env('.env.not-exists')))->load(); } + /** + * @runInSeparateProcess + */ public function testIncompatibleProcessors() { $this->assertProcessors( [], @@ -61,7 +86,12 @@ public function testIncompatibleProcessors() { $this->assertProcessors( null, - [BooleanProcessor::class, QuotedProcessor::class] + [ + NullProcessor::class, + BooleanProcessor::class, + NumberProcessor::class, + QuotedProcessor::class + ] ); $this->assertProcessors( @@ -80,6 +110,9 @@ public function testIncompatibleProcessors() { ); } + /** + * @runInSeparateProcess + */ private function assertProcessors(array $processorsToUse = null, array $expectedProcessors = []) { $dotEnv = new DotEnv($this->env('.env.default'), $processorsToUse); @@ -165,5 +198,23 @@ public function testDontProcessQuotes() $this->assertEquals('\"This is a "sample" value\"', $_ENV['QUOTED5']); $this->assertEquals('"q6', $_ENV['QUOTED6']); $this->assertEquals('q7"', $_ENV['QUOTED7']); + $this->assertEquals('0', $_ENV['ZERO_LITERAL']); + $this->assertEquals('"0"', $_ENV['ZERO_QUOTED']); + } + + /** + * @runInSeparateProcess + */ + public function testProcessNumbers() + { + (new DotEnv($this->env('.env.number'), [ + NumberProcessor::class + ]))->load(); + + $this->assertEquals(0, $_ENV['NUMBER1']); + $this->assertIsNumeric($_ENV['NUMBER1']); + $this->assertEquals(0.0001, $_ENV['NUMBER2']); + $this->assertEquals(123456789, $_ENV['NUMBER3']); + $this->assertEquals(123456789.0, $_ENV['NUMBER4']); } } diff --git a/tests/env/.env.default b/tests/env/.env.default index 7025890..9c329ca 100644 --- a/tests/env/.env.default +++ b/tests/env/.env.default @@ -6,4 +6,13 @@ DATABASE_USER=root #GOOGLE_API=DJfa7czhKaJ0Pig6j9XpSjT6NpXZUZwK # GOOGLE_MANAGER_KEY=P7RkBUQHIkPUEPy3yCTT4gGYa2DjRth8 BOOLEAN_LITERAL=true -BOOLEAN_QUOTED="true" \ No newline at end of file +BOOLEAN_QUOTED="true" +ZERO_LITERAL=0 +ZERO_QUOTED="0" +NUMBER_LITERAL=1111 +NUMBER_QUOTED="1111" +NULL_LITERAL=null +NULL_QUOTED="null" +EMPTY_LITERAL= +EMPTY_QUOTED="" +EMOJI=🪄 \ No newline at end of file diff --git a/tests/env/.env.number b/tests/env/.env.number new file mode 100644 index 0000000..8cf56a6 --- /dev/null +++ b/tests/env/.env.number @@ -0,0 +1,4 @@ +NUMBER1=0 +NUMBER2=0.0001 +NUMBER3=123456789 +NUMBER4=123456789.0 \ No newline at end of file diff --git a/tests/env/.env.quotes b/tests/env/.env.quotes index 520da00..a0f7b7c 100644 --- a/tests/env/.env.quotes +++ b/tests/env/.env.quotes @@ -5,3 +5,5 @@ QUOTED4="This is a "sample" value" QUOTED5=\"This is a "sample" value\" QUOTED6="q6 QUOTED7=q7" +ZERO_LITERAL=0 +ZERO_QUOTED="0"