From 4eda2f2a06aee9ccfeec6505cd6ea2611c91db3e Mon Sep 17 00:00:00 2001 From: mkas Date: Mon, 2 Oct 2017 23:29:40 +0700 Subject: [PATCH] repository map --- src/entity/AbstractEntityRepositoryDb.php | 9 +++++++-- src/factory/ObjectFactory.php | 6 ++++-- tests/core/entity/AbstractEntityRepositoryDbTest.php | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/entity/AbstractEntityRepositoryDb.php b/src/entity/AbstractEntityRepositoryDb.php index 74ce861..9f4e0ae 100644 --- a/src/entity/AbstractEntityRepositoryDb.php +++ b/src/entity/AbstractEntityRepositoryDb.php @@ -15,6 +15,11 @@ abstract class AbstractEntityRepositoryDb extends AbstractEntityRepository const SERIALIZE_STRATEGY_PHP = 2; protected $table; + /** + * map: fieldName: propertyName + * @var array|null + */ + protected $map; protected $serializeFields = []; protected $serializeStrategy = self::SERIALIZE_STRATEGY_JSON; @@ -109,7 +114,7 @@ public function count(Condition $condition = null): int */ public function save(AbstractEntity $item) { - $data = $this->hydrator->extract($item); + $data = $this->hydrator->extract($item, $this->map); $this->beforeDataSave($data); $this->serializeFields($data); if ($id = $item->getId()) { @@ -169,7 +174,7 @@ protected function beforeDataSave(&$data) private function rowToEntity($data) { $this->unserializeFields($data); - $entity = $this->factory->createFromData($data); + $entity = $this->factory->createFromData($data, $this->map); /** @var AbstractEntity $entity */ return $entity; } diff --git a/src/factory/ObjectFactory.php b/src/factory/ObjectFactory.php index e8608ba..af76922 100644 --- a/src/factory/ObjectFactory.php +++ b/src/factory/ObjectFactory.php @@ -48,12 +48,14 @@ public function create() /** * @param array $data + * @param array|null $map + * * @return object */ - public function createFromData(array $data) + public function createFromData(array $data, array $map = null) { $object = $this->create(); - $this->hydrator->hydrate($data, $object); + $this->hydrator->hydrate($data, $object, $map); return $object; } } diff --git a/tests/core/entity/AbstractEntityRepositoryDbTest.php b/tests/core/entity/AbstractEntityRepositoryDbTest.php index fc051f3..56dc6cc 100644 --- a/tests/core/entity/AbstractEntityRepositoryDbTest.php +++ b/tests/core/entity/AbstractEntityRepositoryDbTest.php @@ -54,10 +54,11 @@ public function testFindOne() $conn = $this->createMock(\Doctrine\DBAL\Connection::class); $of = $this->createMock(ObjectFactory::class); - $of->method('createFromData')->with(['a' => 1, 'arr' => [1,2,3], 'arr2' => null])->willReturn(2); + $of->method('createFromData')->with(['a' => 1, 'arr' => [1,2,3], 'arr2' => null], ['a' => 'b'])->willReturn(2); $rep = $this->createRep($of, null, null, $conn, ['selectQuery']); $rep->expects($this->once())->method('selectQuery')->willReturn($qb); + Mocker::setProperty($rep, 'map', ['a' => 'b']); $this->assertEquals(2, $rep->findOne(new Condition())); }