Skip to content

Commit

Permalink
repository map
Browse files Browse the repository at this point in the history
  • Loading branch information
mvkasatkin committed Oct 2, 2017
1 parent 06cc05e commit 4eda2f2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/entity/AbstractEntityRepositoryDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/factory/ObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion tests/core/entity/AbstractEntityRepositoryDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down

0 comments on commit 4eda2f2

Please sign in to comment.