Skip to content

Commit 6a488e5

Browse files
committed
Redesign Collection as immutable value object, simplify DSL
Replace the broad DSL surface (chaining, __get, ArrayAccess, proxy methods) with a single immutable constructor: name, with, filter, required. Remove Filtered, CollectionNotBound, filterColumns, and all mapper coupling from Collection.
1 parent 9663392 commit 6a488e5

22 files changed

+328
-1062
lines changed

src/AbstractMapper.php

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
namespace Respect\Data;
66

77
use Respect\Data\Collections\Collection;
8-
use Respect\Data\Collections\Filtered;
98
use SplObjectStorage;
109

11-
use function array_flip;
12-
use function array_intersect_key;
1310
use function count;
1411
use function ctype_digit;
1512
use function is_int;
@@ -82,13 +79,6 @@ public function markTracked(object $entity, Collection $collection): bool
8279

8380
public function persist(object $object, Collection $onCollection): object
8481
{
85-
$connectsTo = $onCollection->connectsTo;
86-
if ($onCollection instanceof Filtered && $connectsTo !== null) {
87-
$this->persist($object, $connectsTo);
88-
89-
return $object;
90-
}
91-
9282
if ($this->isTracked($object)) {
9383
$currentOp = $this->pending[$object] ?? null;
9484
if ($currentOp !== 'insert') {
@@ -128,31 +118,9 @@ public function isTracked(object $entity): bool
128118

129119
public function registerCollection(string $alias, Collection $collection): void
130120
{
131-
$collection->bindMapper($this);
132121
$this->collections[$alias] = $collection;
133122
}
134123

135-
/**
136-
* @param array<string, mixed> $columns
137-
*
138-
* @return array<string, mixed>
139-
*/
140-
protected function filterColumns(array $columns, Collection $collection): array
141-
{
142-
if (
143-
!$collection instanceof Filtered
144-
|| !$collection->filters
145-
|| $collection->identifierOnly
146-
|| $collection->name === null
147-
) {
148-
return $columns;
149-
}
150-
151-
$id = $this->style->identifier($collection->name);
152-
153-
return array_intersect_key($columns, array_flip([...$collection->filters, $id]));
154-
}
155-
156124
protected function registerInIdentityMap(object $entity, Collection $coll): void
157125
{
158126
if ($coll->name === null) {
@@ -183,11 +151,11 @@ protected function evictFromIdentityMap(object $entity, Collection $coll): void
183151

184152
protected function findInIdentityMap(Collection $collection): object|null
185153
{
186-
if ($collection->name === null || !is_scalar($collection->condition) || $collection->hasMore) {
154+
if ($collection->name === null || !is_scalar($collection->filter) || $collection->hasChildren) {
187155
return null;
188156
}
189157

190-
$condition = $this->normalizeIdValue($collection->condition);
158+
$condition = $this->normalizeIdValue($collection->filter);
191159
if ($condition === null) {
192160
return null;
193161
}
@@ -202,7 +170,7 @@ private function tryMergeWithIdentityMap(object $entity, Collection $coll): obje
202170
}
203171

204172
$entityId = $this->entityIdValue($entity, $coll->name);
205-
$idValue = $entityId ?? $this->normalizeIdValue($coll->condition);
173+
$idValue = $entityId ?? $this->normalizeIdValue($coll->filter);
206174

207175
if ($idValue === null) {
208176
return null;
@@ -269,36 +237,22 @@ private function normalizeIdValue(mixed $value): int|string|null
269237
return null;
270238
}
271239

272-
public function __get(string $name): Collection
273-
{
274-
if (isset($this->collections[$name])) {
275-
return $this->collections[$name];
276-
}
277-
278-
$coll = new Collection($name);
279-
280-
return $coll->bindMapper($this);
281-
}
282-
283240
public function __isset(string $alias): bool
284241
{
285242
return isset($this->collections[$alias]);
286243
}
287244

288-
public function __set(string $alias, Collection $collection): void
289-
{
290-
$this->registerCollection($alias, $collection);
291-
}
292-
293-
/** @param list<Collection|array<scalar, mixed>|scalar|null> $arguments */
245+
/** @param list<mixed> $arguments */
294246
public function __call(string $name, array $arguments): Collection
295247
{
296248
if (isset($this->collections[$name])) {
297-
$collection = clone $this->collections[$name];
249+
if (empty($arguments)) {
250+
return clone $this->collections[$name];
251+
}
298252

299-
return $collection->bindMapper($this)->with(...$arguments);
253+
return $this->collections[$name]->derive(...$arguments);
300254
}
301255

302-
return Collection::__callstatic($name, $arguments)->bindMapper($this);
256+
return new Collection($name, ...$arguments);
303257
}
304258
}

src/CollectionIterator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,13 @@ public function key(): string
5151

5252
public function hasChildren(): bool
5353
{
54-
return $this->current()->hasMore;
54+
return $this->current()->hasChildren;
5555
}
5656

5757
public function getChildren(): RecursiveArrayIterator
5858
{
59-
$c = $this->current();
60-
$pool = $c->hasChildren ? $c->children : [];
61-
if ($c->connectsTo !== null) {
62-
$pool[] = $c->connectsTo;
63-
}
64-
6559
return new static(
66-
array_filter($pool, static fn(Collection $c): bool => $c->name !== null),
60+
array_filter($this->current()->with, static fn(Collection $c): bool => $c->name !== null),
6761
$this->namesCounts,
6862
);
6963
}

src/CollectionNotBound.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)