From aeec316aef19c1faa19caafe5a55b1bdb4adb4b5 Mon Sep 17 00:00:00 2001 From: Aurimas Niekis Date: Thu, 21 Apr 2016 12:04:50 +0200 Subject: [PATCH] Added a custom name support for adding DataMapper to DataMappers --- src/DataMappers.php | 8 ++++++-- src/Tests/DataMappersTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/DataMappers.php b/src/DataMappers.php index 94d7770..4c11c30 100644 --- a/src/DataMappers.php +++ b/src/DataMappers.php @@ -59,9 +59,13 @@ public function setValidator($validator) * * @return $this */ - public function addMapper(DataMapperInterface $dataMapper) : self + public function addMapper(DataMapperInterface $dataMapper, string $name = null) : self { - $this->dataMappers[get_class($dataMapper)] = $dataMapper; + if (null === $name) { + $name = get_class($dataMapper); + } + + $this->dataMappers[$name] = $dataMapper; $dataMapper->setDataMappers($this); diff --git a/src/Tests/DataMappersTest.php b/src/Tests/DataMappersTest.php index ddf780e..cadfa9e 100644 --- a/src/Tests/DataMappersTest.php +++ b/src/Tests/DataMappersTest.php @@ -46,4 +46,23 @@ public function testNotExistingMapper() $dataMappers->getMapper('demo'); } + + public function testCustomName() + { + $dataMappers = new DataMappers(); + + $mapperMock = $this->getMockForAbstractClass('\Thruster\Component\DataMapper\DataMapperInterface'); + $mapperName = 'demo'; + + $this->assertFalse($dataMappers->hasMapper($mapperName)); + + $dataMappers->addMapper($mapperMock, $mapperName); + + $this->assertTrue($dataMappers->hasMapper($mapperName)); + + $wrappedMapper = $dataMappers->getMapper($mapperName); + + $this->assertInstanceOf('\Thruster\Component\DataMapper\DataMapper', $wrappedMapper); + $this->assertEquals($mapperMock, $wrappedMapper->getDataMapper()); + } }