Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Feature/cascade naming strategy to embedded documents #33

Open
wants to merge 10 commits into
base: 2.0.x
Choose a base branch
from
6 changes: 3 additions & 3 deletions src/Hydrator/ODM/MongoDB/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function prepareAssociationStrategies()
continue;
}

// Create new strategy based on type of filed
// Create new strategy based on type of field
$fieldMeta = $this->metadata->fieldMappings[$association];
$reference = isset($fieldMeta['reference']) && $fieldMeta['reference'];
$embedded = isset($fieldMeta['embedded']) && $fieldMeta['embedded'];
Expand All @@ -78,13 +78,13 @@ protected function prepareAssociationStrategies()
if ($reference) {
$strategy = new Strategy\ReferencedCollection($this->objectManager);
} elseif ($embedded) {
$strategy = new Strategy\EmbeddedCollection($this->objectManager);
$strategy = new Strategy\EmbeddedCollection($this->objectManager, $this->getNamingStrategy());
}
} else {
if ($reference) {
$strategy = new Strategy\ReferencedField($this->objectManager);
} elseif ($embedded) {
$strategy = new Strategy\EmbeddedField($this->objectManager);
$strategy = new Strategy\EmbeddedField($this->objectManager, $this->getNamingStrategy());
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/Hydrator/ODM/MongoDB/Strategy/EmbeddedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,38 @@

use Doctrine\Common\Collections\Collection;
use Doctrine\Instantiator\Instantiator;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Hydrator\NamingStrategy\NamingStrategyInterface;

/**
* Class PersistentCollection.
*/
class EmbeddedCollection extends AbstractMongoStrategy
{
/**
* @var NamingStrategyInterface
*/
protected $namingStrategy;

/**
* @param ObjectManager $objectManager
* @param NamingStrategyInterface $namingStrategy
*/
public function __construct(ObjectManager $objectManager = null, $namingStrategy = null)
{
parent::__construct($objectManager);

$this->namingStrategy = $namingStrategy;
}

/**
* @param NamingStrategyInterface $namingStrategy
*/
public function setNamingStrategy(NamingStrategyInterface $namingStrategy)
{
$this->namingStrategy = $namingStrategy;
}

/**
* @param mixed $value
*
Expand All @@ -29,6 +55,9 @@ public function extract($value)
if ($value) {
foreach ($value as $index => $object) {
$hydrator = $this->getDoctrineHydrator();
if ($this->namingStrategy) {
$hydrator->setNamingStrategy($this->namingStrategy);
}
$result[$index] = $hydrator->extract($object);

// Add discrimator field if it can be found.
Expand Down Expand Up @@ -91,6 +120,9 @@ protected function hydrateSingle($targetDocument, $document)
$object = $instantiator->instantiate($targetDocument);

$hydrator = $this->getDoctrineHydrator();
if ($this->namingStrategy) {
$hydrator->setNamingStrategy($this->namingStrategy);
}
$hydrator->hydrate($document, $object);

return $object;
Expand Down
37 changes: 36 additions & 1 deletion src/Hydrator/ODM/MongoDB/Strategy/EmbeddedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,52 @@
namespace Phpro\DoctrineHydrationModule\Hydrator\ODM\MongoDB\Strategy;

use Doctrine\Instantiator\Instantiator;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Hydrator\NamingStrategy\NamingStrategyInterface;

/**
* Class PersistentCollection.
*/
class EmbeddedField extends AbstractMongoStrategy
{
/**
* @param mixed $value
* @var NamingStrategyInterface
*/
public $namingStrategy;

/**
* @param ObjectManager $objectManager
* @param NamingStrategyInterface $namingStrategy
*/
public function __construct(ObjectManager $objectManager = null, $namingStrategy = null)
{
parent::__construct($objectManager);

$this->namingStrategy = $namingStrategy;
}

/**
* @param NamingStrategyInterface $namingStrategy
*/
public function setNamingStrategy(NamingStrategyInterface $namingStrategy)
{
$this->namingStrategy = $namingStrategy;
}

/**
* @param object $value
*
* @return mixed
*/
public function extract($value)
{
if (!is_object($value)) {
return $value;
}
$hydrator = $this->getDoctrineHydrator();
if ($this->namingStrategy) {
$hydrator->setNamingStrategy($this->namingStrategy);
}

return $hydrator->extract($value);
}
Expand All @@ -39,6 +71,9 @@ public function hydrate($value)
$object = $instantiator->instantiate($targetDocument);

$hydrator = $this->getDoctrineHydrator();
if ($this->namingStrategy) {
$hydrator->setNamingStrategy($this->namingStrategy);
}
$hydrator->hydrate($value, $object);

return $object;
Expand Down
4 changes: 1 addition & 3 deletions src/Hydrator/ODM/MongoDB/Strategy/ReferencedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use Doctrine\Common\Collections\Collection;

/**
*
* Class PersistentCollection
* Class PersistentCollection.
*/
class ReferencedCollection extends AbstractMongoStrategy
{
Expand Down Expand Up @@ -53,7 +52,6 @@ public function hydrate($value)
}

/**
*
* @param $targetDocument
* @param $document
*
Expand Down
1 change: 1 addition & 0 deletions test/config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return array(
'doctrine-hydrator' => array(
'custom-hydrator' => array(
Expand Down
28 changes: 28 additions & 0 deletions test/src/Fixtures/Strategy/UnderscoreNamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhproTest\DoctrineHydrationModule\Fixtures\Strategy;

use Zend\Hydrator\NamingStrategy\NamingStrategyInterface;

class UnderscoreNamingStrategy implements NamingStrategyInterface
{
/**
* {@inheritdoc}
*
* @see \Zend\Hydrator\NamingStrategy\NamingStrategyInterface::hydrate()
*/
public function hydrate($name)
{
return trim($name, '_');
}

/**
* {@inheritdoc}
*
* @see \Zend\Hydrator\NamingStrategy\NamingStrategyInterface::extract()
*/
public function extract($name)
{
return '_'.$name.'_';
}
}
89 changes: 89 additions & 0 deletions test/src/Tests/Hydrator/ODM/MongoDB/DoctrineObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationReferenceMany;
use PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationReferenceOne;
use PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationUser;
use PhproTest\DoctrineHydrationModule\Fixtures\Strategy\UnderscoreNamingStrategy;

/**
* Class DoctrineObjectTest.
Expand Down Expand Up @@ -142,6 +143,94 @@ public function it_should_hydrate_a_document()
$this->assertEquals('name', $embedMany[0]->getName());
}

/**
* @test
*/
public function it_should_extract_document_and_cascade_naming_strategy_to_embedded_documents()
{
$creationDate = new \DateTime();
$birthday = new \DateTime('1 january 2014');

$user = new HydrationUser();
$user->setId(1);
$user->setName('user');
$user->setCreatedAt($creationDate->getTimestamp());
$user->setBirthday($birthday);

$embedOne = new HydrationEmbedOne();
$embedOne->setId(1);
$embedOne->setName('name');
$user->setEmbedOne($embedOne);

$embedMany = new HydrationEmbedMany();
$embedMany->setId(1);
$embedMany->setName('name');
$user->addEmbedMany(array($embedMany));

$referenceOne = new HydrationReferenceOne();
$referenceOne->setId(1);
$referenceOne->setName('name');
$user->setReferenceOne($referenceOne);

$referenceMany = new HydrationEmbedMany();
$referenceMany->setId(1);
$referenceMany->setName('name');
$user->addReferenceMany(array($referenceMany));

$hydrator = new DoctrineObject($this->dm);
$hydrator->setNamingStrategy(new UnderscoreNamingStrategy());
$result = $hydrator->extract($user);
$this->assertArrayHasKey('_embedOne_', $result);
$this->assertArrayHasKey('_name_', $result['_embedOne_']);
}

/**
* @test
*/
public function it_should_hydrate_document_and_cascade_naming_strategy_to_embedded_documents()
{
$creationDate = new \DateTime();
$birthday = new \DateTime('1 january 2014');

$user = new HydrationUser();
$data = array(
'_id_' => 1,
'_name_' => 'user',
'_creationDate_' => $creationDate->getTimestamp(),
'_birthday_' => $birthday->getTimestamp(),
'_referenceOne_' => $this->createReferenceOne('name'),
'_referenceMany_' => array($this->createReferenceMany('name')),
'_embedOne_' => array(
'_id_' => 1,
'_name_' => 'name',
),
'_embedMany_' => array(
array(
'_id_' => 1,
'_name_' => 'name',
),
),
);

$hydrator = new DoctrineObject($this->dm);
$hydrator->setNamingStrategy(new UnderscoreNamingStrategy());
$user = $hydrator->hydrate($data, $user);
$this->assertEquals(1, $user->getId());
$this->assertEquals('user', $user->getName());
$this->assertEquals($creationDate->getTimestamp(), $user->getCreatedAt());
$this->assertEquals($birthday->getTimestamp(), $user->getBirthday()->getTimestamp());
$this->assertInstanceOf('PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationReferenceOne', $user->getReferenceOne());
$referenceMany = $user->getReferenceMany();
$this->assertInstanceOf('PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationReferenceMany', $referenceMany[0]);
$this->assertInstanceOf('PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationEmbedOne', $user->getEmbedOne());
$embedMany = $user->getEmbedMany();
$this->assertInstanceOf('PhproTest\DoctrineHydrationModule\Fixtures\ODM\MongoDb\HydrationEmbedMany', $embedMany[0]);
$this->assertEquals('name', $user->getReferenceOne()->getName());
$this->assertEquals('name', $referenceMany[0]->getName());
$this->assertEquals('name', $user->getEmbedOne()->getName());
$this->assertEquals('name', $embedMany[0]->getName());
}

/**
* @param $name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract protected function createStrategy();
*
* @return StrategyInterface
*/
protected function getStrategy($objectManager, $object, $fieldName)
protected function getStrategy($objectManager, $object, $fieldName, $namingStrategy = null)
{
$objectClass = get_class($object);
$metadata = $objectManager->getClassMetadata($objectClass);
Expand All @@ -32,6 +32,9 @@ protected function getStrategy($objectManager, $object, $fieldName)
$strategy->setObjectManager($objectManager);
$strategy->setCollectionName($fieldName);
$strategy->setClassMetadata($metadata);
if ($namingStrategy) {
$strategy->setNamingStrategy($namingStrategy);
}

return $strategy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function it_should_hydrate_embedded_collections()
$embedded->setName('name');

$data = array(
$embedded
$embedded,
);

$strategy = $this->getStrategy($this->dm, $user, 'embedMany');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ class EmbeddedFieldTest extends AbstractMongoStrategyTest
*/
protected function createStrategy()
{
return new EmbeddedField();
return new EmbeddedField(null, null);
}

/**
* @test
*/
public function it_should_not_break_when_embed_field_not_set()
{
$user = new HydrationUser();
$user->setId(1);
$user->setName('username');
$embedded = new HydrationEmbedOne();
$embedded->setId(1);
$embedded->setName('name');
$strategy = $this->getStrategy($this->dm, $user, 'embedOne');
$result = $strategy->extract($user->getEmbedOne());
$this->assertNull($result);
}

/**
Expand Down