Created
July 9, 2012 14:57
-
-
Save awartoft/3077013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Antoine Hedgecock <[email protected]> | |
*/ | |
/** | |
* @namespace | |
*/ | |
namespace MCNCore\Object; | |
use Doctrine\ORM\EntityManager, | |
Doctrine\ORM\Mapping\ClassMetadataInfo, | |
Zend\Stdlib\Hydrator\HydratorInterface; | |
class Hydrator implements HydratorInterface | |
{ | |
/** | |
* @var \Doctrine\ORM\EntityManager | |
*/ | |
protected $em; | |
/** | |
* @var ClassMetadataInfo | |
*/ | |
protected $metadata; | |
/** | |
* @param EntityManager $em | |
*/ | |
public function __construct(EntityManager $em) | |
{ | |
$this->em = $em; | |
} | |
/** | |
* Extract values from an object | |
* | |
* @param object $object | |
* | |
* @return array | |
*/ | |
public function extract($object) | |
{ | |
return $object->toArray(); | |
} | |
/** | |
* Hydrate $object with the provided $data. | |
* | |
* @param array $data | |
* @param object $object | |
* @return object | |
*/ | |
public function hydrate(array $data, $object) | |
{ | |
if (! $object instanceof AbstractObject) { | |
throw new Exception\LogicException( | |
sprintf('The second parameter passed must be an instance of abstract object. "%s" passed', get_class($object)) | |
); | |
} | |
$this->metadata = $this->em->getClassMetadata(get_class($object)); | |
foreach($data as $field => $value) | |
{ | |
if ($this->metadata->hasAssociation($field)) { | |
$association = $this->metadata->getAssociationMapping($field); | |
switch($association['type']) | |
{ | |
case ClassMetadataInfo::ONE_TO_MANY: | |
case ClassMetadataInfo::MANY_TO_MANY: | |
$this->toMany($object, $field, $value, $association); | |
break; | |
case ClassMetadataInfo::ONE_TO_ONE: | |
case ClassMetadataInfo::MANY_TO_ONE: | |
$this->toOne($object, $field, $value, $association); | |
break; | |
default: | |
throw new \Exception('Unimplemented type'); | |
break; | |
} | |
} | |
if ($this->metadata->hasField($field)) { | |
$object->offsetSet($field, $value); | |
} | |
} | |
return $object; | |
} | |
/** | |
* @param AbstractObject $object | |
* @param string $field | |
* @param mixed $value | |
* @param array $association | |
* @return void | |
*/ | |
protected function toOne(AbstractObject $object, $field, AbstractObject $value, array $association) | |
{ | |
$identifiers = array(); | |
foreach($association['sourceToTargetKeyColumns'] as $column) { | |
$identifiers[$column] = $value[$column]; | |
} | |
$object->offsetSet($field, $this->em->getPartialReference(get_class($value), $identifiers)); | |
} | |
/** | |
* @param AbstractObject $object | |
* @param string $field | |
* @param mixed $value | |
* @param array $association | |
* @return void | |
*/ | |
protected function toMany(AbstractObject $object, $field, $value, array $association) | |
{ | |
if (!is_array($value)) { | |
$value = (array) $value; | |
} | |
$values = array(); | |
foreach($value as $v) { | |
$identifiers = array(); | |
foreach($association['relationToTargetKeyColumns'] as $column) { | |
$identifiers[$column] = $v[$column]; | |
} | |
$values[] = $this->em->getPartialReference(get_class($v), $identifiers); | |
} | |
$object->offsetSet($field, $values); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment