diff --git a/src/Hydrator/ODM/MongoDB/DoctrineObject.php b/src/Hydrator/ODM/MongoDB/DoctrineObject.php index 11562b5..af7876b 100644 --- a/src/Hydrator/ODM/MongoDB/DoctrineObject.php +++ b/src/Hydrator/ODM/MongoDB/DoctrineObject.php @@ -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']; @@ -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()); } } diff --git a/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedCollection.php b/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedCollection.php index 354e080..c45625a 100644 --- a/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedCollection.php +++ b/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedCollection.php @@ -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 * @@ -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. @@ -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; diff --git a/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedField.php b/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedField.php index 021df66..5018049 100644 --- a/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedField.php +++ b/src/Hydrator/ODM/MongoDB/Strategy/EmbeddedField.php @@ -3,6 +3,8 @@ namespace Phpro\DoctrineHydrationModule\Hydrator\ODM\MongoDB\Strategy; use Doctrine\Instantiator\Instantiator; +use Doctrine\Common\Persistence\ObjectManager; +use Zend\Hydrator\NamingStrategy\NamingStrategyInterface; /** * Class PersistentCollection. @@ -10,13 +12,43 @@ 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); } @@ -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; diff --git a/src/Hydrator/ODM/MongoDB/Strategy/ReferencedCollection.php b/src/Hydrator/ODM/MongoDB/Strategy/ReferencedCollection.php index 41f6454..7a826cf 100644 --- a/src/Hydrator/ODM/MongoDB/Strategy/ReferencedCollection.php +++ b/src/Hydrator/ODM/MongoDB/Strategy/ReferencedCollection.php @@ -5,8 +5,7 @@ use Doctrine\Common\Collections\Collection; /** - * - * Class PersistentCollection + * Class PersistentCollection. */ class ReferencedCollection extends AbstractMongoStrategy { @@ -53,7 +52,6 @@ public function hydrate($value) } /** - * * @param $targetDocument * @param $document * diff --git a/test/config/module.config.php b/test/config/module.config.php index a93d74c..9f6e6fc 100644 --- a/test/config/module.config.php +++ b/test/config/module.config.php @@ -1,4 +1,5 @@ array( 'custom-hydrator' => array( diff --git a/test/src/Fixtures/Strategy/UnderscoreNamingStrategy.php b/test/src/Fixtures/Strategy/UnderscoreNamingStrategy.php new file mode 100644 index 0000000..1db7a15 --- /dev/null +++ b/test/src/Fixtures/Strategy/UnderscoreNamingStrategy.php @@ -0,0 +1,28 @@ +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 * diff --git a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/AbstractMongoStrategyTest.php b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/AbstractMongoStrategyTest.php index b2d4b13..a419e7b 100644 --- a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/AbstractMongoStrategyTest.php +++ b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/AbstractMongoStrategyTest.php @@ -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); @@ -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; } diff --git a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/DefaultRelationTest.php b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/DefaultRelationTest.php index 5d4fdb5..dfc8a2f 100644 --- a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/DefaultRelationTest.php +++ b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/DefaultRelationTest.php @@ -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'); diff --git a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/EmbeddedFieldTest.php b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/EmbeddedFieldTest.php index 852c6ef..b3bb416 100644 --- a/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/EmbeddedFieldTest.php +++ b/test/src/Tests/Hydrator/ODM/MongoDB/Strategy/EmbeddedFieldTest.php @@ -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); } /**