daikon-cqrs /
value-object
| 1 | <?php declare(strict_types=1); |
||||||
| 2 | /** |
||||||
| 3 | * This file is part of the daikon-cqrs/value-object project. |
||||||
| 4 | * |
||||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||||
| 6 | * file that was distributed with this source code. |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace Daikon\ValueObject; |
||||||
| 10 | |||||||
| 11 | use Daikon\Interop\Assertion; |
||||||
| 12 | use Daikon\Interop\SupportsAnnotations; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * @type(Daikon\ValueObject\ValueObjectInterface) |
||||||
| 16 | */ |
||||||
| 17 | trait ValueObjectCollectionTrait |
||||||
| 18 | { |
||||||
| 19 | use SupportsAnnotations; |
||||||
| 20 | |||||||
| 21 | /** @return static */ |
||||||
| 22 | public static function makeEmpty(): self |
||||||
| 23 | { |
||||||
| 24 | return new static; |
||||||
| 25 | } |
||||||
| 26 | |||||||
| 27 | public function isEmpty(): bool |
||||||
| 28 | { |
||||||
| 29 | return count($this) === 0; |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 30 | } |
||||||
| 31 | |||||||
| 32 | /** @param static $comparator */ |
||||||
| 33 | 4 | public function equals($comparator): bool |
|||||
| 34 | { |
||||||
| 35 | 4 | $this->assertInitialized(); |
|||||
|
0 ignored issues
–
show
It seems like
assertInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 36 | 4 | Assertion::isInstanceOf($comparator, static::class); |
|||||
| 37 | |||||||
| 38 | /** @var ValueObjectInterface $object */ |
||||||
| 39 | 4 | foreach ($this as $key => $object) { |
|||||
| 40 | 4 | $comparison = $comparator->get($key, null); |
|||||
|
0 ignored issues
–
show
It seems like
get() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 41 | 4 | if (!$comparison || !$object->equals($comparison)) { |
|||||
| 42 | 2 | return false; |
|||||
| 43 | } |
||||||
| 44 | } |
||||||
| 45 | |||||||
| 46 | 2 | return true; |
|||||
| 47 | } |
||||||
| 48 | |||||||
| 49 | /** |
||||||
| 50 | * @param null|iterable $state |
||||||
| 51 | * @return static |
||||||
| 52 | */ |
||||||
| 53 | 1 | public static function fromNative($state): self |
|||||
| 54 | { |
||||||
| 55 | 1 | Assertion::nullOrIsTraversable($state, 'State provided to '.static::class.' must be null or iterable.'); |
|||||
| 56 | // Override fromNative() to support multiple types, currently first seen type factory is used. |
||||||
| 57 | 1 | $typeFactory = current(static::inferTypeFactories()); |
|||||
| 58 | 1 | Assertion::isCallable($typeFactory, 'No valid type factory specified.'); |
|||||
| 59 | /** @var callable $typeFactory */ |
||||||
| 60 | 1 | $objects = []; |
|||||
| 61 | 1 | if (!is_null($state)) { |
|||||
| 62 | 1 | foreach ($state as $key => $data) { |
|||||
| 63 | 1 | $objects[$key] = $typeFactory($data); |
|||||
| 64 | } |
||||||
| 65 | } |
||||||
| 66 | |||||||
| 67 | 1 | return new static($objects); |
|||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | public function toNative(): array |
||||||
| 71 | { |
||||||
| 72 | $this->assertInitialized(); |
||||||
| 73 | $objects = []; |
||||||
| 74 | foreach ($this as $key => $object) { |
||||||
| 75 | $objects[$key] = $object->toNative(); |
||||||
| 76 | } |
||||||
| 77 | return $objects; |
||||||
| 78 | } |
||||||
| 79 | |||||||
| 80 | 2 | public function __toString(): string |
|||||
| 81 | { |
||||||
| 82 | 2 | $this->assertInitialized(); |
|||||
| 83 | 2 | $parts = []; |
|||||
| 84 | 2 | foreach ($this as $key => $object) { |
|||||
| 85 | 2 | $parts[] = $key.':'.(string)$object; |
|||||
| 86 | } |
||||||
| 87 | 2 | return implode(', ', $parts); |
|||||
| 88 | } |
||||||
| 89 | } |
||||||
| 90 |