Skip to content

Commit fd0b23a

Browse files
committed
Add RemapKeysFilter
1 parent 8597cb7 commit fd0b23a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ The following filters are built into php-dotenv.
297297
- `josegonzalez\Dotenv\Filter\CallableFilter`: Wraps a callable and invokes it upon the environment.
298298
- `josegonzalez\Dotenv\Filter\LowercaseKeyFilter`: Lowercases all the keys for an environment to a single-depth.
299299
- `josegonzalez\Dotenv\Filter\NullFilter`: Returns the environment data without any changes.
300+
- `josegonzalez\Dotenv\Filter\RemapKeysFilter`: Remaps specific keys in a `$config` array to a set of values at a single-depth.
300301
- `josegonzalez\Dotenv\Filter\UnderscoreArrayFilter`: Expands a flat array to a nested array. For example, `['0_Foo_Bar' => 'Far']` becomes `[['Foo' => ['Bar' => 'Far']]]`.
301302
- `josegonzalez\Dotenv\Filter\UppercaseFirstKeyFilter`: Uppercases the first letter for all the keys for an environment to a single-depth..
302303
- `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.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace josegonzalez\Dotenv\Filter;
4+
5+
class RemapKeysFilter
6+
{
7+
/**
8+
* Remaps specific keys in a $config array to a set of values at a single-depth.
9+
*
10+
* @param array $environment Array of environment data
11+
* @param array $config Array of keys to remap to specific values
12+
* @return array
13+
*/
14+
public function __invoke(array $environment, array $config)
15+
{
16+
foreach ($config as $key => $value) {
17+
if (array_key_exists($key, $environment)) {
18+
$environment[$value] = $environment[$key];
19+
unset($environment[$key]);
20+
}
21+
}
22+
return $environment;
23+
}
24+
}

tests/josegonzalez/Dotenv/LoaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ public function testNullFilter()
331331
), $this->Loader->toArray());
332332
}
333333

334+
335+
/**
336+
* @covers \josegonzalez\Dotenv\Filter\RemapKeysFilter::__invoke
337+
*/
338+
public function testRemapKeysFilter()
339+
{
340+
$this->Loader->setFilters(array(
341+
'josegonzalez\Dotenv\Filter\RemapKeysFilter' => array(
342+
'FOO' => 'QUX'
343+
),
344+
));
345+
$this->Loader->setFilepath($this->fixturePath . '.env');
346+
$this->Loader->parse();
347+
$this->Loader->filter();
348+
$this->assertEquals(array(
349+
'QUX' => 'bar',
350+
'BAR' => 'baz',
351+
'SPACED' => 'with spaces',
352+
'EQUALS' => 'pgsql:host=localhost;dbname=test',
353+
), $this->Loader->toArray());
354+
}
355+
334356
/**
335357
* @covers \josegonzalez\Dotenv\Filter\UppercaseFirstKeyFilter::__invoke
336358
*/

0 commit comments

Comments
 (0)