Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Behavior to Map Multiple Keys at Once #4

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Configuration
*/
private $factory_mapper;

/**
* @var Mapper[]
*/
private $mappers;

/**
* @param array $config
* @param string $prefix
Expand Down Expand Up @@ -61,22 +66,24 @@ public function has($key)
}

/**
* Configures a Mapper object for the given Key
* Configures a Mapper object for the given Keys
*
* The Mapper object can be configured
* The Mapper object can be configured externally by being returned and, as it is stored as a reference,
* will be shared across all "mapped" keys.
*
* @param $key
* @param string[] $keys
* @return Mapper
*
* @throws InvalidKeyException
*/
public function map($key)
public function map(...$keys)
{
$this->checkKeyExists($key);

$mapper = $this->factory_mapper->createNew($this->config[$key]);
$mapper = $this->factory_mapper->createNew();

$this->config[$key] = $mapper;
array_walk($keys, function ($key) use ($mapper) {
$this->checkKeyExists($key);
$this->mappers[$key] = $mapper;
});

return $mapper;
}
Expand All @@ -95,8 +102,8 @@ public function get($key)
{
$this->checkKeyExists($key);

if ($this->config[$key] instanceof Mapper) {
return $this->config[$key]->map();
if (isset($this->mappers[$key])) {
return $this->mappers[$key]->map($this->config[$key]);
}

return $this->config[$key];
Expand Down
24 changes: 7 additions & 17 deletions src/Configuration/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

class Mapper
{
/**
* @var mixed
*/
private $value;

/**
* @var mixed
Expand All @@ -20,28 +16,22 @@ class Mapper
private $func;

/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @param string $value
*
* @return mixed
*/
public function map()
public function map($value)
{
if ($this->cached !== null) {
return $this->cached;
if (isset($this->cached[$value])) {
return $this->cached[$value];
}

if ($this->func === null) {
return $this->cached = $this->value;
return $this->cached[$value] = $value;
}

$func = $this->func;
return $this->cached = $func($this->value);
return $this->cached[$value] = $func($value);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function __construct($mapper_class = Mapper::class)
*
* @return Mapper
*/
public function createNew($value)
public function createNew()
{
return new $this->mapper_class($value);
return new $this->mapper_class();
}
}
5 changes: 2 additions & 3 deletions tests/unit/Configuration/MapperFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function defaultMapperClass()
{
$sut = new MapperFactory();

self::assertInstanceOf(Mapper::class, $sut->createNew('value'));

self::assertInstanceOf(Mapper::class, $sut->createNew());
}

/**
Expand All @@ -34,7 +33,7 @@ public function injectedMapperClass()
{
$sut = new MapperFactory(ValidMapper::class);

self::assertInstanceOf(ValidMapper::class, $sut->createNew('value'));
self::assertInstanceOf(ValidMapper::class, $sut->createNew());
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/Configuration/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MapperTest extends AbstractTestCase
*/
public function returnOriginalValueIfNoFunction()
{
self::assertSame('Original Value', (new Mapper('Original Value'))->map());
self::assertSame('Original Value', (new Mapper())->map('Original Value'));
}

/**
Expand All @@ -29,13 +29,13 @@ public function mappingValues()
$original_value = 'baloney';
$mapped_value = 'YENOLAB';

$sut = new Mapper($original_value);
$sut = new Mapper();

$sut->using(function ($value) {
return strrev(strtoupper($value));
});

self::assertSame($mapped_value, $sut->map());
self::assertSame($mapped_value, $sut->map($original_value));
}

/**
Expand All @@ -53,12 +53,12 @@ public function mappedValuesAreCached()
->with($original_value)
->willReturn($mapped_value);

$sut = new Mapper($original_value);
$sut = new Mapper();

$sut->using($test_map);

self::assertSame($mapped_value, $sut->map());
self::assertSame($mapped_value, $sut->map());
self::assertSame($mapped_value, $sut->map($original_value));
self::assertSame($mapped_value, $sut->map($original_value));
}

/**
Expand All @@ -67,10 +67,10 @@ public function mappedValuesAreCached()
*/
public function toInt($original, $expected)
{
$sut = new Mapper($original);
$sut = new Mapper();
$sut->toInt();

self::assertSame($expected, $sut->map());
self::assertSame($expected, $sut->map($original));
}

/**
Expand All @@ -79,10 +79,10 @@ public function toInt($original, $expected)
*/
public function toBool($original, $expected)
{
$sut = new Mapper($original);
$sut = new Mapper();
$sut->toBool();

self::assertSame($expected, $sut->map());
self::assertSame($expected, $sut->map($original));
}

/**
Expand Down
31 changes: 19 additions & 12 deletions tests/unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function basicGets()

self::assertSame('one', $sut_three->get('CFG_ONE'));
self::assertSame('otherone', $sut_three->get('OTHER_ONE'));

}

/**
Expand Down Expand Up @@ -97,25 +96,33 @@ public function defaultMapper()
*/
public function mappingValues()
{
$factory = $this->createMock(MapperFactory::class);
$mapper = $this->createMock(Mapper::class);

$key = 'BALONEY';
$original_value = 'Original Value';
$mapped_value = 'Mapped Value';
$factory = $this->createMock(MapperFactory::class);
$mapper = $this->createMock(Mapper::class);

$factory
->method('createNew')
->with($original_value)
->willReturn($mapper);

$mapper
->method('map')
->willReturn($mapped_value);
->willReturnMap([
[ 'Original Value 1', 'Mapped Value 1' ],
[ 'Original Value 2', 'Mapped Value 2' ],
[ 'Original Value 3', 'Mapped Value 3' ],
]);

$config = [
'KEY1' => 'Original Value 1',
'KEY2' => 'Original Value 2',
'KEY3' => 'Original Value 3',
];

$sut = new Configuration($config, '', $factory);

$sut = new Configuration([$key => $original_value], '', $factory);
self::assertSame($mapper, $sut->map('KEY1', 'KEY2', 'KEY3'));

self::assertSame($mapper, $sut->map($key));
self::assertSame($mapped_value, $sut->get($key));
self::assertSame('Mapped Value 1', $sut->get('KEY1'));
self::assertSame('Mapped Value 2', $sut->get('KEY2'));
self::assertSame('Mapped Value 3', $sut->get('KEY3'));
}
}