8000 removed DomainCollection::fromValue(), GenericPaginatedDomainCollecti… · msgphp/msgphp@f3bc811 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3bc811

Browse files
authored
removed DomainCollection::fromValue(), GenericPaginatedDomainCollection (#388)
1 parent d0c96e7 commit f3bc811

17 files changed

+101
-414
lines changed

docs/ddd/collections.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ primitive iterable value. It may hold any type of element values.
1010
- [`\Countable`][countable]
1111
- [`\IteratorAggregate`][iterator-aggregate]
1212

13-
### `static fromValue(?iterable $value): DomainCollection`
14-
15-
Returns a factorized collection from any primitive iterable. Using `null` implies an empty collection.
16-
1713
### `isEmpty(): bool`
1814

1915
Tells if a collection is considered empty, i.e. contains zero elements.
@@ -87,7 +83,7 @@ Get the total no. of items in the full result set.
8783

8884
### `MsgPhp\Domain\GenericDomainCollection`
8985

90-
A generic collection compatible with any `iterable` value.
86+
A generic paginated collection compatible with any `iterable` value.
9187

9288
#### Basic Example
9389

@@ -129,10 +125,6 @@ $firstTwoIntsPlussed = $firstTwoInts->map(function (int $value): int {
129125
});
130126
```
131127

132-
### `MsgPhp\Domain\GenericPaginatedDomainCollection`
133-
134-
A generic paginated collection compatible with any `iterable` value.
135-
136128
### Infrastructural
137129

138130
- [Doctrine Collection](../infrastructure/doctrine-collections.md#domain-collection)

docs/ddd/object-factory.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Returns the actual class name the factory uses for a given class name.
2525

2626
### `MsgPhp\Domain\Factory\GenericDomainObjectFactory`
2727

28-
A generic object factory. It initializes a class by reading its [constructor] arguments. If the class is a sub class
29-
of `MsgPhp\Domain\DomainId` or `MsgPhp\Domain\DomainCollection` its static `fromValue` constructor will be used instead.
28+
A generic object factory. It initializes a class by reading its [constructor] arguments.
3029

3130
Context elements mapped by argument name will be used as argument value. In case of a type-hinted object argument a
3231
nested context may be provided to initialize the object with.

src/Domain/DomainCollection.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313
*/
1414
interface DomainCollection extends \Countable, \IteratorAggregate
1515
{
16-
/**
17-
* @template T2Key of array-key
18-
* @template T2
19-
*
20-
* @param null|iterable<T2Key, T2> $value
21-
*
22-
* @return self<T2Key, T2>
23-
*/
24-
public static function fromValue(?iterable $value): self;
25-
2616
public function isEmpty(): bool;
2717

2818
/**

src/Domain/Factory/GenericDomainObjectFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace MsgPhp\Domain\Factory;
66

7-
use MsgPhp\Domain\DomainCollection;
87
use MsgPhp\Domain\DomainId;
98
use MsgPhp\Domain\Exception\InvalidClass;
109
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
@@ -37,7 +36,7 @@ public function create(string $class, array $context = []): object
3736
{
3837
$class = $this->getClass($class, $context);
3938

40-
if (is_subclass_of($class, DomainId::class) || is_subclass_of($class, DomainCollection::class)) {
39+
if (is_subclass_of($class, DomainId::class)) {
4140
return $class::fromValue(...$this->resolveArguments($class, 'fromValue', $context));
4241
}
4342

src/Domain/GenericDomainCollection.php

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,31 @@
1212
*
1313
* @template TKey of array-key
1414
* @template T
15-
* @implements DomainCollection<TKey,T>
15+
* @implements PaginatedDomainCollection<TKey,T>
1616
*/
17-
final class GenericDomainCollection implements DomainCollection
17+
final class GenericDomainCollection implements PaginatedDomainCollection
1818
{
1919
/** @var iterable<TKey, T> */
2020
private $elements;
21+
/** @var float */
22+
private $offset;
23+
/** @var float */
24+
private $limit;
25+
/** @var null|float */
26+
private $count;
27+
/** @var null|float */
28+
private $totalCount;
2129

2230
/**
23-
* @param iterable<TKey, T> $elements
31+
* @param null|iterable<TKey, T> $elements
2432
*/
25-
public function __construct(iterable $elements)
33+
public function __construct(?iterable $elements = null, float $offset = .0, float $limit = .0, ?float $count = null, ?float $totalCount = null)
2634
{
27-
$this->elements = $elements;
28-
}
29-
30-
public static function fromValue(?iterable $value): DomainCollection
31-
{
32-
return new self($value ?? []);
35+
$this->elements = $elements ?? [];
36+
$this->offset = $offset;
37+
$this->limit = $limit;
38+
$this->count = $count;
39+
$this->totalCount = $totalCount;
3340
}
3441

3542
public function getIterator(): \Traversable
@@ -51,10 +58,6 @@ public function getIterator(): \Traversable
5158

5259
public function isEmpty(): bool
5360
{
54-
if ($this->elements instanceof DomainCollection) {
55-
return $this->elements->isEmpty();
56-
}
57-
5861
if ($this->elements instanceof \Traversable) {
5962
foreach ($this->elements as $element) {
6063
return false;
@@ -68,10 +71,6 @@ public function isEmpty(): bool
6871

6972
public function contains($element): bool
7073
{
71-
if ($this->elements instanceof DomainCollection) {
72-
return $this->elements->contains($element);
73-
}
74-
7574
if ($this->elements instanceof \Traversable) {
7675
foreach ($this->elements as $key => $knownElement) {
7776
if ($element === $knownElement) {
@@ -87,10 +86,6 @@ public function contains($element): bool
8786

8887
public function containsKey($key): bool
8988
{
90-
if ($this->elements instanceof DomainCollection) {
91-
return $this->elements->containsKey($key);
92-
}
93-
9489
if ($this->elements instanceof \Traversable) {
9590
/** @psalm-suppress InvalidCast */
9691
$key = \is_int($key) ? (string) $key : $key;
@@ -109,10 +104,6 @@ public function containsKey($key): bool
109104

110105
public function first()
111106
{
112-
if ($this->elements instanceof DomainCollection) {
113-
return $this->elements->first();
114-
}
115-
116107
if ($this->elements instanceof \Traversable) {
117108
foreach ($this->elements as $element) {
118109
return $element;
@@ -130,10 +121,6 @@ public function first()
130121

131122
public function last()
132123
{
133-
if ($this->elements instanceof DomainCollection) {
134-
return $this->elements->last();
135-
}
136-
137124
if ($this->elements instanceof \Traversable) {
138125
$empty = true;
139126
$element = null;
@@ -157,10 +144,6 @@ public function last()
157144

158145
public function get($key)
159146
{
160-
if ($this->elements instanceof DomainCollection) {
161-
return $this->elements->get($key);
162-
}
163-
164147
if ($this->elements instanceof \Traversable) {
165148
/** @psalm-suppress InvalidCast */
166149
$key = \is_int($key) ? (string) $key : $key;
@@ -183,11 +166,6 @@ public function get($key)
183166

184167
public function filter(callable $filter): DomainCollection
185168
{
186-
if ($this->elements instanceof DomainCollection) {
187-
/** @var DomainCollection<TKey, T> */
188-
return $this->elements->filter($filter);
189-
}
190-
191169
if ($this->elements instanceof \Traversable) {
192170
return new self((/** @return \Generator<TKey, T> */function () use ($filter): iterable {
193171
foreach ($this->elements as $key => $element) {
@@ -203,11 +181,6 @@ public function filter(callable $filter): DomainCollection
203181

204182
public function slice(int $offset, int $limit = 0): DomainCollection
205183
{
206-
if ($this->elements instanceof DomainCollection) {
207-
/** @var DomainCollection<TKey, T> */
208-
return $this->elements->slice($offset, $limit);
209-
}
210-
211184
if ($this->elements instanceof \Traversable) {
212185
return new self((/** @return \Generator<TKey, T> */function () use ($offset, $limit): iterable {
213186
$i = -1;
@@ -233,11 +206,6 @@ public function slice(int $offset, int $limit = 0): DomainCollection
233206
*/
234207
public function map(callable $mapper): DomainCollection
235208
{
236-
if ($this->elements instanceof DomainCollection) {
237-
/** @var DomainCollection<TKey, T2> */
238-
return $this->elements->map($mapper);
239-
}
240-
241209
if ($this->elements instanceof \Traversable) {
242210
return new self((/** @return \Generator<TKey, T2> */function () use ($mapper): iterable {
243211
foreach ($this->elements as $key => $element) {
@@ -251,10 +219,47 @@ public function map(callable $mapper): DomainCollection
251219

252220
public function count(): int
253221
{
222+
if (null !== $this->count) {
223+
return (int) $this->count;
224+
}
225+
254226
if ($this->elements instanceof \Countable) {
255227
return $this->elements->count();
256228
}
257229

258230
return $this->elements instanceof \Traversable ? iterator_count($this->elements) : \count($this->elements);
259231
}
232+
233+
public function getOffset(): flo E868 at
234+
{
235+
return $this->offset;
236+
}
237+
238+
public function getLimit(): float
239+
{
240+
return $this->limit;
241+
}
242+
243+
public function getCurrentPage(): float
244+
{
245+
if (0 >= $this->limit) {
246+
return 1.;
247+
}
248+
249+
return floor($this->offset / $this->limit) + 1.;
250+
}
251+
252+
public function getLastPage(): float
253+
{
254+
if (0 >= $this->limit) {
255+
return 1.;
256+
}
257+
258+
return ceil($this->getTotalCount() / $this->limit) ?: 1.;
259+
}
260+
261+
public function getTotalCount(): float
262+
{
263+
return $this->totalCount ?? (float) \count($this);
264+
}
260265
}

0 commit comments

Comments
 (0)
0