Skip to content

Commit

Permalink
Add native types to EntityHydrator
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jan 18, 2022
1 parent 3999837 commit be96bbf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
35 changes: 8 additions & 27 deletions lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,20 @@
*/
class DefaultEntityHydrator implements EntityHydrator
{
/** @var EntityManagerInterface */
private $em;

/** @var UnitOfWork */
private $uow;

/**
* The IdentifierFlattener used for manipulating identifiers
*
* @var IdentifierFlattener
*/
private $identifierFlattener;
private UnitOfWork $uow;
private IdentifierFlattener $identifierFlattener;

/** @var array<string,mixed> */
private static $hints = [Query::HINT_CACHE_ENABLED => true];
private static array $hints = [Query::HINT_CACHE_ENABLED => true];

/**
* @param EntityManagerInterface $em The entity manager.
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
public function __construct(
private EntityManagerInterface $em
) {
$this->uow = $em->getUnitOfWork();
$this->identifierFlattener = new IdentifierFlattener($em->getUnitOfWork(), $em->getMetadataFactory());
}

/**
* {@inheritdoc}
*/
public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity)
public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, object $entity): EntityCacheEntry
{
$data = $this->uow->getOriginalEntityData($entity);
$data = array_merge($data, $metadata->getIdentifierValues($entity)); // why update has no identifier values ?
Expand Down Expand Up @@ -139,10 +123,7 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $e
return new EntityCacheEntry($metadata->name, $data);
}

/**
* {@inheritdoc}
*/
public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, $entity = null)
public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, ?object $entity = null): ?object
{
$data = $entry->data;
$hints = self::$hints;
Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/ORM/Cache/EntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ interface EntityHydrator
* @param ClassMetadata $metadata The entity metadata.
* @param EntityCacheKey $key The entity cache key.
* @param object $entity The entity.
*
* @return EntityCacheEntry
*/
public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity);
public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, object $entity): EntityCacheEntry;

/**
* @param ClassMetadata $metadata The entity metadata.
* @param EntityCacheKey $key The entity cache key.
* @param EntityCacheEntry $entry The entity cache entry.
* @param object|null $entity The entity to load the cache into. If not specified, a new entity is created.
*
* @return object|null
*/
public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, $entity = null);
public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, ?object $entity = null): ?object;
}
11 changes: 4 additions & 7 deletions tests/Doctrine/Tests/ORM/Cache/DefaultEntityHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Doctrine\ORM\Cache\EntityCacheEntry;
use Doctrine\ORM\Cache\EntityCacheKey;
use Doctrine\ORM\Cache\EntityHydrator;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Models\Cache\Country;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\OrmTestCase;
Expand All @@ -21,11 +21,8 @@
*/
class DefaultEntityHydratorTest extends OrmTestCase
{
/** @var EntityHydrator */
private $structure;

/** @var EntityManagerInterface */
private $em;
private DefaultEntityHydrator $structure;
private EntityManagerMock $em;

protected function setUp(): void
{
Expand All @@ -37,7 +34,7 @@ protected function setUp(): void

public function testImplementsEntityEntryStructure(): void
{
self::assertInstanceOf('\Doctrine\ORM\Cache\EntityHydrator', $this->structure);
self::assertInstanceOf(EntityHydrator::class, $this->structure);
}

public function testCreateEntity(): void
Expand Down

0 comments on commit be96bbf

Please sign in to comment.