Skip to content

Commit

Permalink
Update AbstractHydratorEntity.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dezbyte authored Sep 15, 2017
1 parent 63373d2 commit a39963b
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions src/Colibri/Core/Hydrator/AbstractHydratorEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Colibri\Core\Entity\RepositoryInterface;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\Core\Hydrator;
use Colibri\Core\Metadata;
use Colibri\Core\ORMEvents;
use Colibri\Core\Proxy\EntityProxy;
use Colibri\Exception\NotFoundException;
Expand All @@ -18,23 +19,23 @@
*/
abstract class AbstractHydratorEntity extends Hydrator
{

/**
* @var RepositoryInterface
*/
protected $repository;

/**
* HydratorEntity constructor.
* @param RepositoryInterface $repository
*/
public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;

parent::__construct($repository->getEntityClassReflection());
}

/**
* @param array $data
* @param EntityInterface|EntityProxy $entity
Expand All @@ -49,7 +50,7 @@ public function hydrate(array $data, $entity)

return $entity;
}

/**
* @param EntityInterface $entity
* @param array $injectData
Expand All @@ -58,39 +59,32 @@ public function hydrate(array $data, $entity)
protected function hydrateEntityProperties(EntityInterface $entity, array $injectData)
{
$metadata = $this->getRepository()->getEntityMetadata();

foreach ($injectData as $keyName => $value) {
try {
$columnSQLName = $metadata->getSQLName($keyName);
if ($entity->hasName($columnSQLName)) {
$entity->setByName($columnSQLName, $value);
}
} catch (NotFoundException $exception) {
$entity->setVirtual($keyName, $value);
}

foreach ($injectData as $sqlName => $propertyValue) {
$propertyName = $metadata->getName($sqlName, Metadata::CAMILIZED);
$propertyValue = $propertyValue !== null
? $metadata->toPhp(Inflector::underscore($propertyName), $propertyValue) : null;
$this->setProperty($entity, $propertyName, $propertyValue);
}

return $this;
}

/**
* @param EntityInterface|EntityProxy $entity
* @param string $propertyName
* @param mixed $value
* @param mixed $propertyValue
* @return $this
*/
protected function setProperty($entity, $propertyName, $value)
protected function setProperty($entity, $propertyName, $propertyValue)
{
if($this->getReflection()->hasProperty($propertyName)) {
if ($this->getReflection()->hasProperty($propertyName)) {
$property = $this->getReflection()->getProperty($propertyName);

if (!$property->isPublic()) {
$property->setAccessible(true);
}

$property->setValue($entity, $value);
$property->setValue($entity, $propertyValue);
} else {
$entity->setVirtual($propertyName, $propertyValue);
}

return $this;
}

Expand All @@ -103,20 +97,18 @@ public function extract($entity)
$metadata = $this->getMetadata();
$collection = [];

foreach ($metadata->getNames() as $entityPropertyName) {
$propertyName = Inflector::camelize($entityPropertyName);
$rawPropertyName = $metadata->getSQLName($entityPropertyName);
foreach ($metadata->getNames() as $sqlName) {
$propertyName = $metadata->getName($sqlName, Metadata::CAMILIZED);
$rawPropertyName = $metadata->getSQLName($sqlName);
$propertyValue = $this->getReflection()->getProperty($propertyName)->getValue($entity);

$propertyValue = $propertyValue !== null
? $metadata->toPlatform($entityPropertyName, $propertyValue) : null;

? $metadata->toPlatform($sqlName, $propertyValue) : null;
$collection[$rawPropertyName] = $propertyValue;
}

return $collection;
}

/**
* @return RepositoryInterface
*/
Expand All @@ -132,5 +124,5 @@ public function getMetadata()
{
return $this->getRepository()->getEntityMetadata();
}

}

0 comments on commit a39963b

Please sign in to comment.