Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
66.67% |
10 / 15 |
CRAP | |
65.52% |
19 / 29 |
| ItemCollection | |
0.00% |
0 / 1 |
|
66.67% |
10 / 15 |
39.08 | |
65.52% |
19 / 29 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| fromArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| fromItems | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| emptyList | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| push | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| pop | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| first | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| last | |
0.00% |
0 / 1 |
3.33 | |
66.67% |
2 / 3 |
|||
| contains | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 4 |
|||
| items | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| toArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| equals | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| __toString | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| count | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| sameValueAs | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| <?php | |
| declare(strict_types=1); | |
| namespace App\Order\Domain\ValueObject; | |
| use App\Shared\ValueObjectInterface; | |
| use Countable; | |
| final class ItemCollection implements ValueObjectInterface, Countable | |
| { | |
| /** @var array Item */ | |
| private array $items; | |
| public function __construct(Item ...$items) | |
| { | |
| $this->items = $items; | |
| } | |
| public static function fromArray(array $items): self | |
| { | |
| return new self(...array_map(static function (array $items) { | |
| return Item::fromArray($items); | |
| }, $items)); | |
| } | |
| public static function fromItems(Item ...$items): self | |
| { | |
| return new self(...$items); | |
| } | |
| public static function emptyList(): self | |
| { | |
| return new self(); | |
| } | |
| public function push(Item $item): self | |
| { | |
| $copy = clone $this; | |
| $copy->items[] = $item; | |
| return $copy; | |
| } | |
| public function pop(): self | |
| { | |
| $copy = clone $this; | |
| array_pop($copy->items); | |
| return $copy; | |
| } | |
| public function first(): ?Item | |
| { | |
| return $this->items[0] ?? null; | |
| } | |
| public function last(): ?Item | |
| { | |
| if (0 === count($this->items)) { | |
| return null; | |
| } | |
| return $this->items[count($this->items) - 1]; | |
| } | |
| public function contains(Item $item): bool | |
| { | |
| foreach ($this->items as $existingItem) { | |
| if ($existingItem->equals($item)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public function items(): array | |
| { | |
| return $this->items; | |
| } | |
| public function toArray(): array | |
| { | |
| return array_map(static function (Item $item) { | |
| return $item->toArray(); | |
| }, $this->items); | |
| } | |
| public function equals(ValueObjectInterface $other): bool | |
| { | |
| if (!$other instanceof self) { | |
| return false; | |
| } | |
| return $this->toArray() === $other->toArray(); | |
| } | |
| public function __toString(): string | |
| { | |
| return (string) json_encode($this->toArray()); | |
| } | |
| public function count(): int | |
| { | |
| return count($this->items); | |
| } | |
| public function sameValueAs(ValueObjectInterface $other): bool | |
| { | |
| return $this->equals($other); | |
| } | |
| } |