From fd0b23a62995b800f5c44b720ef3f3ce2f1e545f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 25 Dec 2015 08:27:25 -0500 Subject: [PATCH] Add RemapKeysFilter --- README.markdown | 1 + .../Dotenv/Filter/RemapKeysFilter.php | 24 +++++++++++++++++++ tests/josegonzalez/Dotenv/LoaderTest.php | 22 +++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/josegonzalez/Dotenv/Filter/RemapKeysFilter.php diff --git a/README.markdown b/README.markdown index 14fe545..ecbef0c 100644 --- a/README.markdown +++ b/README.markdown @@ -297,6 +297,7 @@ The following filters are built into php-dotenv. - `josegonzalez\Dotenv\Filter\CallableFilter`: Wraps a callable and invokes it upon the environment. - `josegonzalez\Dotenv\Filter\LowercaseKeyFilter`: Lowercases all the keys for an environment to a single-depth. - `josegonzalez\Dotenv\Filter\NullFilter`: Returns the environment data without any changes. +- `josegonzalez\Dotenv\Filter\RemapKeysFilter`: Remaps specific keys in a `$config` array to a set of values at a single-depth. - `josegonzalez\Dotenv\Filter\UnderscoreArrayFilter`: Expands a flat array to a nested array. For example, `['0_Foo_Bar' => 'Far']` becomes `[['Foo' => ['Bar' => 'Far']]]`. - `josegonzalez\Dotenv\Filter\UppercaseFirstKeyFilter`: Uppercases the first letter for all the keys for an environment to a single-depth.. - `josegonzalez\Dotenv\Filter\UrlParseFilter`: When there is a key with the suffix `_URL`, this filter uses `parse_url` to add extra data to the environment. diff --git a/src/josegonzalez/Dotenv/Filter/RemapKeysFilter.php b/src/josegonzalez/Dotenv/Filter/RemapKeysFilter.php new file mode 100644 index 0000000..790156e --- /dev/null +++ b/src/josegonzalez/Dotenv/Filter/RemapKeysFilter.php @@ -0,0 +1,24 @@ + $value) { + if (array_key_exists($key, $environment)) { + $environment[$value] = $environment[$key]; + unset($environment[$key]); + } + } + return $environment; + } +} diff --git a/tests/josegonzalez/Dotenv/LoaderTest.php b/tests/josegonzalez/Dotenv/LoaderTest.php index 3f62891..24d26ba 100644 --- a/tests/josegonzalez/Dotenv/LoaderTest.php +++ b/tests/josegonzalez/Dotenv/LoaderTest.php @@ -331,6 +331,28 @@ public function testNullFilter() ), $this->Loader->toArray()); } + + /** + * @covers \josegonzalez\Dotenv\Filter\RemapKeysFilter::__invoke + */ + public function testRemapKeysFilter() + { + $this->Loader->setFilters(array( + 'josegonzalez\Dotenv\Filter\RemapKeysFilter' => array( + 'FOO' => 'QUX' + ), + )); + $this->Loader->setFilepath($this->fixturePath . '.env'); + $this->Loader->parse(); + $this->Loader->filter(); + $this->assertEquals(array( + 'QUX' => 'bar', + 'BAR' => 'baz', + 'SPACED' => 'with spaces', + 'EQUALS' => 'pgsql:host=localhost;dbname=test', + ), $this->Loader->toArray()); + } + /** * @covers \josegonzalez\Dotenv\Filter\UppercaseFirstKeyFilter::__invoke */