2
0

EntityHydrator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Hydrator;
  3. use InvalidArgumentException;
  4. use LogicException;
  5. use PhpDevCommunity\PaperORM\Cache\EntityMemcachedCache;
  6. use PhpDevCommunity\PaperORM\Collection\ObjectStorage;
  7. use PhpDevCommunity\PaperORM\Entity\EntityInterface;
  8. use PhpDevCommunity\PaperORM\Mapper\ColumnMapper;
  9. use PhpDevCommunity\PaperORM\Mapping\Column\JoinColumn;
  10. use PhpDevCommunity\PaperORM\Mapping\OneToMany;
  11. use PhpDevCommunity\PaperORM\Proxy\ProxyFactory;
  12. use PhpDevCommunity\PaperORM\Proxy\ProxyInterface;
  13. use ReflectionClass;
  14. final class EntityHydrator extends AbstractEntityHydrator
  15. {
  16. private EntityMemcachedCache $cache;
  17. public function __construct(EntityMemcachedCache $cache)
  18. {
  19. $this->cache = $cache;
  20. }
  21. protected function instantiate(string $class, array $data): object
  22. {
  23. $primaryKey = ColumnMapper::getPrimaryKeyColumnName($class);
  24. $object = $this->cache->get($class, $data[$primaryKey]) ?: ProxyFactory::create($class);
  25. $this->cache->set($class, $data[$primaryKey], $object);
  26. return $object;
  27. }
  28. }