Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:
- src/
- tests/
ignoreErrors:
- message: '/Call to an undefined (static )?method Respect\\Data\\(AbstractMapper|InMemoryMapper|Collections\\(Collection|Composite|Typed))::\w+\(\)\./'
- message: '/Call to an undefined (static )?method Respect\\Data\\(AbstractMapper|InMemoryMapper|Collections\\Collection)::\w+\(\)\./'
- message: '/Unsafe usage of new static\(\)\./'
-
message: '/Property .+ is never read, only written\./'
Expand Down
26 changes: 3 additions & 23 deletions src/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,12 @@ public function derive(
): static {
return new static(
$this->name,
...$this->deriveArgs(
with: $with,
filter: $filter,
required: $required,
),
with: [...$this->with, ...$with],
filter: $filter ?? $this->filter,
required: $required ?? $this->required,
);
}

/**
* @param list<Collection> $with
* @param array<scalar, mixed>|scalar|null $filter
*
* @return array{with: list<Collection>, filter: array|int|float|string|bool|null, required: bool}
*/
protected function deriveArgs( // @phpstan-ignore missingType.iterableValue
array $with = [],
array|int|float|string|bool|null $filter = null,
bool|null $required = null,
): array {
return [
'with' => [...$this->with, ...$with],
'filter' => $filter ?? $this->filter,
'required' => $required ?? $this->required,
];
}

/**
* @param list<Collection> $children
*
Expand Down
46 changes: 0 additions & 46 deletions src/Collections/Composite.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/Collections/Typed.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function extractProperties(object $entity): array
$props = [];

foreach ($this->reflectProperties($entity::class) as $name => $prop) {
if (!$prop->isInitialized($entity) || $prop->getAttributes(NotPersistable::class)) {
if ($prop->isVirtual() || !$prop->isInitialized($entity) || $prop->getAttributes(NotPersistable::class)) {
continue;
}

Expand All @@ -218,7 +218,7 @@ public function enumerateFields(string $collectionName): array
$fields = [];

foreach ($this->reflectProperties($class) as $name => $prop) {
if ($prop->getAttributes(NotPersistable::class) || isset($relations[$name])) {
if ($prop->isVirtual() || $prop->getAttributes(NotPersistable::class) || isset($relations[$name])) {
continue;
}

Expand Down
17 changes: 0 additions & 17 deletions src/Hydrators/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use DomainException;
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Typed;
use Respect\Data\EntityFactory;
use Respect\Data\Hydrator;
use SplObjectStorage;
Expand Down Expand Up @@ -75,20 +74,4 @@ protected function wireRelationships(SplObjectStorage $entities): void
}
}
}

/**
* @param object|array<mixed, mixed> $row
*
* @return class-string
*/
protected function resolveEntityClass(
Collection $collection,
object|array $row,
): string {
if ($collection instanceof Typed) {
return $collection->resolveEntityClass($this->entityFactory, $row);
}

return $this->entityFactory->resolveClass((string) $collection->name);
}
}
2 changes: 1 addition & 1 deletion src/Hydrators/Nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function hydrateNode(
SplObjectStorage $entities,
): void {
$entity = $this->entityFactory->create(
$this->resolveEntityClass($collection, $data),
$this->entityFactory->resolveClass((string) $collection->name),
);

foreach ($data as $key => $value) {
Expand Down
44 changes: 6 additions & 38 deletions src/Hydrators/PrestyledAssoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use DomainException;
use Respect\Data\CollectionIterator;
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Composite;
use SplObjectStorage;

use function array_keys;
use function explode;
use function is_array;

Expand Down Expand Up @@ -52,11 +50,15 @@ public function hydrateAll(
$instances = [];

foreach ($grouped as $prefix => $props) {
$basePrefix = $this->resolveCompositionBase($prefix, $collMap);
if (!isset($collMap[$prefix])) {
throw new DomainException('Unknown column prefix "' . $prefix . '" in hydration row');
}

$basePrefix = $prefix;

if (!isset($instances[$basePrefix])) {
$coll = $collMap[$basePrefix];
$class = $this->resolveEntityClass($coll, $props);
$class = $this->entityFactory->resolveClass((string) $coll->name);
$instances[$basePrefix] = $this->entityFactory->create($class);
$entities[$instances[$basePrefix]] = $coll;
}
Expand All @@ -83,45 +85,11 @@ private function buildCollMap(Collection $collection): array

$this->collMap = [];
foreach (CollectionIterator::recursive($collection) as $spec => $c) {
if ($c->name === null) {
continue;
}

$this->collMap[$spec] = $c;
}

$this->cachedCollection = $collection;

return $this->collMap;
}

/**
* Resolve a composition prefix back to its base entity specifier.
*
* Composition columns use prefixes like "post_WITH_comment" (see Composite::COMPOSITION_MARKER).
* This returns "post" so properties are merged into the parent entity.
*
* @param array<string, Collection> $collMap
*/
private function resolveCompositionBase(string $prefix, array $collMap): string
{
if (isset($collMap[$prefix])) {
return $prefix;
}

// Look for a base specifier where this prefix is a composition alias
foreach ($collMap as $spec => $coll) {
if (!$coll instanceof Composite) {
continue;
}

foreach (array_keys($coll->compositions) as $compName) {
if ($prefix === $spec . Composite::COMPOSITION_MARKER . $compName) {
return $spec;
}
}
}

throw new DomainException('Unknown column prefix "' . $prefix . '" in hydration row');
}
}
25 changes: 10 additions & 15 deletions tests/AbstractMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Respect\Data\Collections\Collection;
use Respect\Data\Collections\Composite;
use Respect\Data\Hydrators\Nested;
use Respect\Data\Styles\CakePHP;
use Respect\Data\Styles\Standard;
Expand Down Expand Up @@ -52,20 +51,6 @@ public function registerCollectionShouldAddCollectionToPool(): void
$this->assertEquals($coll->name, $clone->name);
}

#[Test]
public function callingRegisteredCollectionWithArgsDerives(): void
{
$coll = Composite::post(['comment' => ['text']]);
$this->mapper->registerCollection('postComment', $coll);

$derived = $this->mapper->postComment(filter: 5);

$this->assertInstanceOf(Composite::class, $derived);
$this->assertEquals('post', $derived->name);
$this->assertEquals(['comment' => ['text']], $derived->compositions);
$this->assertEquals(5, $derived->filter);
}

#[Test]
public function callingRegisteredCollectionWithoutArgsClones(): void
{
Expand Down Expand Up @@ -1305,4 +1290,14 @@ public function mergeWithIdentityMapNormalizesConditionFallback(): void
$this->assertTrue($mapper->isTracked($merged));
$this->assertFalse($mapper->isTracked($fetched));
}

#[Test]
public function callingRegisteredCollectionWithArgsDerives(): void
{
$coll = Collection::post();
$this->mapper->registerCollection('post', $coll);
$derived = $this->mapper->post(filter: 5);
$this->assertEquals('post', $derived->name);
$this->assertEquals(5, $derived->filter);
}
}
53 changes: 0 additions & 53 deletions tests/Collections/CompositeTest.php

This file was deleted.

Loading
Loading