1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\Entity\Entity; |
4
|
|
|
|
5
|
|
|
use Daikon\DataStructure\TypedListTrait; |
6
|
|
|
use Daikon\Entity\Assert\Assertion; |
7
|
|
|
use Daikon\Entity\ValueObject\ValueObjectInterface; |
8
|
|
|
use Daikon\Entity\ValueObject\ValueObjectListInterface; |
9
|
|
|
|
10
|
|
View Code Duplication |
final class RelatedEntityList implements ValueObjectListInterface |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
use TypedListTrait; |
13
|
|
|
|
14
|
|
|
public static function makeEmpty(): self |
15
|
|
|
{ |
16
|
|
|
return new static; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function wrap($entities): self |
20
|
|
|
{ |
21
|
|
|
return new self($entities); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function fromNative($nativeValue): self |
25
|
|
|
{ |
26
|
|
|
// @todo implement |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function toNative(): array |
30
|
|
|
{ |
31
|
|
|
return $this->compositeVector->map(static function (ValueObjectInterface $entity): array { |
32
|
|
|
return $entity->toNative(); |
33
|
|
|
})->toArray(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function equals(ValueObjectInterface $otherList): bool |
37
|
|
|
{ |
38
|
|
|
Assertion::isInstanceOf($otherList, static::class); |
39
|
|
|
if (count($this) !== count($otherList)) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
foreach ($this as $pos => $value) { |
43
|
|
|
if (!$value->equals($otherList->get($pos))) { |
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function __toString(): string |
51
|
|
|
{ |
52
|
|
|
$parts = []; |
53
|
|
|
foreach ($this as $nestedEntity) { |
54
|
|
|
$parts[] = (string)$nestedEntity; |
55
|
|
|
} |
56
|
|
|
return implode(",\n", $parts); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function diff(ValueObjectListInterface $otherList): ValueObjectListInterface |
60
|
|
|
{ |
61
|
|
|
$differentEntities = []; |
62
|
|
|
/* @var TypedEntityInterface $entity */ |
63
|
|
|
foreach ($this as $pos => $entity) { |
64
|
|
|
if (!$otherList->has($pos)) { |
65
|
|
|
$differentEntities[] = $entity; |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
/* @var TypedEntityInterface $otherEntity */ |
69
|
|
|
$otherEntity = $otherList->get($pos); |
70
|
|
|
$diff = $entity->getValueObjectMap()->diff($otherEntity->getValueObjectMap()); |
71
|
|
|
if (!$diff->isEmpty()) { |
72
|
|
|
$differentEntities[] = $entity; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return new static($differentEntities); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function __construct(array $entities = []) |
79
|
|
|
{ |
80
|
|
|
$this->init($entities, EntityRelationInterface::class); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.