<commit_before><?php

namespace Colibri\Core\Hydrator;

use Colibri\Common\Inflector;
use Colibri\Core\Entity;
use Colibri\Core\Entity\EntityInterface;
use Colibri\Core\Entity\RepositoryInterface;
use Colibri\Core\Event\EntityLifecycleEvent;
use Colibri\Core\Hydrator;
use Colibri\Core\ORMEvents;
use Colibri\Core\Proxy\EntityProxy;
use Colibri\Exception\NotFoundException;

/**
 * Class AbstractHydratorEntity
 * @package Colibri\Core\Hydrator
 */
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
   * @return EntityInterface|EntityProxy
   */
  public function hydrate(array $data, $entity)
  {
    $this->hydrateEntityProperties($entity, $data);
    
    $repository = $this->getRepository();
    $repository->dispatchEvent(ORMEvents::onEntityLoad, new EntityLifecycleEvent($repository, $entity));
    
    return $entity;
  }

  /**
   * @param EntityInterface $entity
   * @param array $injectData
   * @return $this
   */
  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);
      }
    }

    return $this;
  }

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

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

      $property->setValue($entity, $value);
    }

    return $this;
  }
  
  /**
   * @param object $entity
   * @return array
   */
  public function extract($entity)
  {
    $metadata = $this->getMetadata();
    $collection = [];
    
    foreach ($metadata->getNames() as $entityPropertyName) {
      $propertyName = Inflector::camelize($entityPropertyName);
      $rawPropertyName = $metadata->getSQLName($entityPropertyName);
      $propertyValue = $this->getReflection()->getProperty($propertyName)->getValue($entity);
  
      $propertyValue = $propertyValue !== null
        ? $metadata->toPlatform($entityPropertyName, $propertyValue) : null;

      $collection[$rawPropertyName] = $propertyValue;
    }
    
    return $collection;
  }

  /**
   * @return RepositoryInterface
   */
  public function getRepository()
  {
    return $this->repository;
  }
  
  /**
   * @return Entity\MetadataInterface
   */
  public function getMetadata()
  {
    return $this->getRepository()->getEntityMetadata();
  }

}<commit_msg>Update AbstractHydratorEntity.php<commit_after><?php

namespace Colibri\Core\Hydrator;

use Colibri\Common\Inflector;
use Colibri\Core\Entity;
use Colibri\Core\Entity\EntityInterface;
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;

/**
 * Class AbstractHydratorEntity
 * @package Colibri\Core\Hydrator
 */
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
   * @return EntityInterface|EntityProxy
   */
  public function hydrate(array $data, $entity)
  {
    $this->hydrateEntityProperties($entity, $data);
    
    $repository = $this->getRepository();
    $repository->dispatchEvent(ORMEvents::onEntityLoad, new EntityLifecycleEvent($repository, $entity));
    
    return $entity;
  }
  
  /**
   * @param EntityInterface $entity
   * @param array $injectData
   * @return $this
   */
  protected function hydrateEntityProperties(EntityInterface $entity, array $injectData)
  {
    $metadata = $this->getRepository()->getEntityMetadata();
    
    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 $propertyValue
   * @return $this
   */
  protected function setProperty($entity, $propertyName, $propertyValue)
  {
    if ($this->getReflection()->hasProperty($propertyName)) {
      $property = $this->getReflection()->getProperty($propertyName);
      $property->setValue($entity, $propertyValue);
    } else {
      $entity->setVirtual($propertyName, $propertyValue);
    }
    
    return $this;
  }
  
  /**
   * @param object $entity
   * @return array
   */
  public function extract($entity)
  {
    $metadata = $this->getMetadata();
    $collection = [];
    
    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($sqlName, $propertyValue) : null;
      $collection[$rawPropertyName] = $propertyValue;
    }
    
    return $collection;
  }
  
  /**
   * @return RepositoryInterface
   */
  public function getRepository()
  {
    return $this->repository;
  }
  
  /**
   * @return Entity\MetadataInterface
   */
  public function getMetadata()
  {
    return $this->getRepository()->getEntityMetadata();
  }
  
}
{'fraction_non_alphanumeric': 0.13519569763967731, 'fraction_numerical': 0.0, 'mean_word_length': 3.6557719054242, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4224879', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1971, 'n_words': 355}