Created
July 9, 2012 14:10
-
-
Save awartoft/3076800 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 ($field == 'category_id') continue; | |
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: | |
$this->toOne($object, $field, $value, $association); | |
break; | |
default: | |
throw new \Exception('Unimplemented type'); | |
break; | |
} | |
} | |
if ($this->metadata->hasField($field)) { | |
$object->offsetSet($field, $value); | |
} | |
} | |
var_dump($object); | |
exit; | |
} | |
protected function toOne($object, $field, $value, $association) | |
{ | |
} | |
/** | |
* @param $object | |
* @param $field | |
* @param $value | |
* @param $association | |
*/ | |
protected function toMany($object, $field, $value, $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]; | |
} | |
/** | |
* One may ask why i made this so complicated ? | |
* | |
* getPartialReference does some hidden magic, check it out ) | |
*/ | |
$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