Skip to content

Commit

Permalink
Add RemapKeysFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Dec 25, 2015
1 parent 8597cb7 commit fd0b23a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions src/josegonzalez/Dotenv/Filter/RemapKeysFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace josegonzalez\Dotenv\Filter;

class RemapKeysFilter
{
/**
* Remaps specific keys in a $config array to a set of values at a single-depth.
*
* @param array $environment Array of environment data
* @param array $config Array of keys to remap to specific values
* @return array
*/
public function __invoke(array $environment, array $config)
{
foreach ($config as $key => $value) {
if (array_key_exists($key, $environment)) {
$environment[$value] = $environment[$key];
unset($environment[$key]);
}
}
return $environment;
}
}
22 changes: 22 additions & 0 deletions tests/josegonzalez/Dotenv/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit fd0b23a

Please sign in to comment.