EntityHydrator.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 PhpDevCommunity\PaperORM\Schema\SchemaInterface;
  14. use ReflectionClass;
  15. final class EntityHydrator extends AbstractEntityHydrator
  16. {
  17. private EntityMemcachedCache $cache;
  18. private SchemaInterface $schema;
  19. public function __construct(SchemaInterface $schema, EntityMemcachedCache $cache)
  20. {
  21. $this->cache = $cache;
  22. $this->schema = $schema;
  23. }
  24. protected function instantiate(string $class, array $data): object
  25. {
  26. $primaryKey = ColumnMapper::getPrimaryKeyColumnName($class);
  27. $object = $this->cache->get($class, $data[$primaryKey]) ?: ProxyFactory::create($class);
  28. $this->cache->set($class, $data[$primaryKey], $object);
  29. return $object;
  30. }
  31. protected function getSchema(): SchemaInterface
  32. {
  33. return $this->schema;
  34. }
  35. }