From d1ef2c4117329c4f179266731098606aa4e74527 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 16 Oct 2022 02:00:37 +0100 Subject: [PATCH] Add support for unicode variable names Closes #540 Co-Authored-By: Thor Erik <219796+thorerik@users.noreply.github.com> --- src/Parser/EntryParser.php | 2 +- tests/Dotenv/DotenvTest.php | 8 ++++++++ tests/Dotenv/Parser/EntryParserTest.php | 12 ++++++------ tests/fixtures/env/unicodevarnames.env | 2 ++ 4 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/env/unicodevarnames.env diff --git a/src/Parser/EntryParser.php b/src/Parser/EntryParser.php index 6a4d54f1..e286840a 100644 --- a/src/Parser/EntryParser.php +++ b/src/Parser/EntryParser.php @@ -139,7 +139,7 @@ private static function isQuotedName(string $name) */ private static function isValidName(string $name) { - return Regex::matches('~\A[a-zA-Z0-9_.]+\z~', $name)->success()->getOrElse(false); + return Regex::matches('~(*UTF8)\A[\p{Ll}\p{Lu}\p{M}\p{N}_.]+\z~', $name)->success()->getOrElse(false); } /** diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 8baf5dd9..97ddfd73 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -335,6 +335,14 @@ public function testEmptyLoading() self::assertSame(['EMPTY_VAR' => null], $dotenv->load()); } + public function testUnicodeVarNames() + { + $dotenv = Dotenv::createImmutable(self::$folder, 'unicodevarnames.env'); + $dotenv->load(); + self::assertSame('Skybert', $_SERVER['AlbertÅberg']); + self::assertSame('2022-04-01T00:00', $_SERVER['ДатаЗакрытияРасчетногоПериода']); + } + public function testDirectConstructor() { $repository = RepositoryBuilder::createWithDefaultAdapters()->make(); diff --git a/tests/Dotenv/Parser/EntryParserTest.php b/tests/Dotenv/Parser/EntryParserTest.php index e61be25e..8a5f5f6d 100644 --- a/tests/Dotenv/Parser/EntryParserTest.php +++ b/tests/Dotenv/Parser/EntryParserTest.php @@ -24,6 +24,12 @@ public function testNullParse() $this->checkEmptyResult($result, 'FOO'); } + public function testUnicodeNameParse() + { + $result = EntryParser::parse('FOOƱ=BAZ'); + $this->checkPositiveResult($result, 'FOOƱ', 'BAZ'); + } + public function testQuotesParse() { $result = EntryParser::parse("FOO=\"BAR \n\""); @@ -139,12 +145,6 @@ public function testParseInvalidName() $this->checkErrorResult($result, 'Encountered an invalid name at [FOO_ASD!].'); } - public function testParseUnicodeName() - { - $result = EntryParser::parse('FOOƱ=BAZ'); - $this->checkErrorResult($result, 'Encountered an invalid name at [FOOƱ].'); - } - public function testParserEscapingDouble() { $result = EntryParser::parse('FOO_BAD="iiiiviiiixiiiiviiii\\a"'); diff --git a/tests/fixtures/env/unicodevarnames.env b/tests/fixtures/env/unicodevarnames.env new file mode 100644 index 00000000..e86f24ef --- /dev/null +++ b/tests/fixtures/env/unicodevarnames.env @@ -0,0 +1,2 @@ +AlbertÅberg=Skybert +ДатаЗакрытияРасчетногоПериода='2022-04-01T00:00'