Skip to content

Commit 9f121a1

Browse files
committed
universal collection for results
1 parent 1a3fa5d commit 9f121a1

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/EntityManager.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function __construct(
1414
private \Spameri\Elastic\EventManager\DispatchEvents $dispatchEvents,
1515
private \Spameri\Elastic\Model\IdentityMap $identityMap,
1616
private \Spameri\Elastic\Model\ChangeSet $changeSet,
17+
private \Spameri\Elastic\Factory\EntityFactory $entityFactory,
1718
)
1819
{
1920
}
@@ -93,7 +94,7 @@ public function findBy(
9394
throw $exception;
9495
}
9596

96-
return $this->createCollection($class, $resultSearch->hits()->ids());
97+
return $this->createCollection($class, $resultSearch);
9798
}
9899

99100

@@ -121,13 +122,24 @@ public function createEmptyCollection(
121122
*/
122123
protected function createCollection(
123124
string $class,
124-
array $ids,
125+
\Spameri\ElasticQuery\Response\ResultSearch $resultSearch,
125126
): \Spameri\Elastic\Entity\Collection\ElasticEntityCollection
126127
{
128+
$entities = [];
129+
foreach ($resultSearch->hits() as $hit) {
130+
try {
131+
$entities[] = $this->entityFactory->create($hit, $class, $this);
132+
133+
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
134+
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
135+
}
136+
}
137+
127138
return new \Spameri\Elastic\Entity\Collection\ElasticEntityCollection(
128-
entityManager: $this,
129-
entityClass: $class,
130-
elasticIds: $ids,
139+
$this,
140+
$class,
141+
$resultSearch->hits()->ids(),
142+
... $entities,
131143
);
132144
}
133145

src/Factory/EntityFactory.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public function __construct(
1515
}
1616

1717
/**
18-
* @return \Generator<\Spameri\Elastic\Entity\AbstractElasticEntity>
18+
* @template T
19+
* @param class-string<T> $class
20+
* @return T
1921
*/
2022
public function create(
2123
\Spameri\ElasticQuery\Response\Result\Hit $hit,
2224
string|null $class,
2325
\Spameri\Elastic\EntityManager|null $entityManager,
24-
): \Generator
26+
): \Spameri\Elastic\Entity\AbstractElasticEntity
2527
{
2628
if ($class === null) {
2729
throw new \InvalidArgumentException('Class must be set');
@@ -40,7 +42,7 @@ class: $class,
4042
id: $hit->id(),
4143
);
4244
if ($entity !== null) {
43-
yield $entity;
45+
return $entity;
4446
}
4547

4648
$properties = $this->resolveProperties(
@@ -57,7 +59,7 @@ class: $class,
5759

5860
$this->identityMap->add($entity);
5961

60-
yield $entity;
62+
return $entity;
6163
}
6264

6365
protected function resolveProperties(
@@ -86,6 +88,7 @@ protected function resolveProperties(
8688
$propertyTypeName = $reflectionPropertyType->getName();
8789
if ($reflectionPropertyType->allowsNull() && $value === null) {
8890
$propertyValue = null;
91+
$setNull = true;
8992

9093
} elseif ($property->hasDefaultValue() === true && $value === null) {
9194
$propertyValue = $property->getDefaultValue();
@@ -207,11 +210,13 @@ class: $propertyTypeName,
207210
$propertyValue = $value;
208211
}
209212

210-
if (isset($propertyValue)) {
213+
if (
214+
isset($propertyValue) || isset($setNull)
215+
) {
211216
$resolvedProperties[$property->getName()] = $propertyValue;
212217
}
213218

214-
unset($propertyValue);
219+
unset($propertyValue, $setNull);
215220
}
216221

217222
return $resolvedProperties;

src/Factory/EntityFactoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public function create(
99
\Spameri\ElasticQuery\Response\Result\Hit $hit,
1010
string|null $class,
1111
\Spameri\Elastic\EntityManager|null $entityManager,
12-
): \Generator;
12+
): \Spameri\Elastic\Entity\AbstractElasticEntity;
1313

1414
}

src/Model/AbstractBaseService.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function get(
5555
throw new \Spameri\Elastic\Exception\DocumentNotFound(' with id ' . $id->value());
5656
}
5757

58-
return $this->entityFactory->create($singleResult->hit(), null, $this->entityManager)->current();
58+
return $this->entityFactory->create($singleResult->hit(), null, $this->entityManager);
5959
}
6060

6161

@@ -80,7 +80,7 @@ public function getBy(
8080
throw new \Spameri\Elastic\Exception\DocumentNotFound($this->index, $elasticQuery);
8181
}
8282

83-
return $this->entityFactory->create($resultSearch->hits()->getIterator()->current(), null, $this->entityManager)->current();
83+
return $this->entityFactory->create($resultSearch->hits()->getIterator()->current(), null, $this->entityManager);
8484
}
8585

8686

@@ -107,7 +107,7 @@ public function getAllBy(
107107
$entities = [];
108108
foreach ($resultSearch->hits() as $hit) {
109109
try {
110-
$entities[] = $this->entityFactory->create($hit, null, $this->entityManager)->current();
110+
$entities[] = $this->entityFactory->create($hit, null, $this->entityManager);
111111

112112
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
113113
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);

tests/SpameriTests/Elastic/Factory/EntityFactory.phpt

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class EntityFactory extends \SpameriTests\Elastic\AbstractTestCase
5252
);
5353

5454
/** @var \SpameriTests\Elastic\Data\Entity\Person $entity */
55-
$entity = $entityFactory->create($hit, \SpameriTests\Elastic\Data\Entity\Person::class, $entityManager)
56-
->current();
55+
$entity = $entityFactory->create($hit, \SpameriTests\Elastic\Data\Entity\Person::class, $entityManager);
5756

5857
\Tester\Assert::same($person->id->value(), $entity->id->value());
5958
\Tester\Assert::same($person->identification->imdb->value(), $entity->identification->imdb->value());

0 commit comments

Comments
 (0)