Code Duplication    Length = 73-75 lines in 2 locations

src/Entity/NestedEntityList.php 1 location

@@ 9-83 (lines=75) @@
6
use Daikon\Entity\ValueObject\ValueObjectInterface;
7
use Daikon\Entity\ValueObject\ValueObjectListInterface;
8
9
final class NestedEntityList implements ValueObjectListInterface
10
{
11
    use TypedListTrait;
12
13
    public static function makeEmpty(): self
14
    {
15
        return new self;
16
    }
17
18
    public static function wrap($entities): self
19
    {
20
        return new self($entities);
21
    }
22
23
    public static function fromNative($nativeValue): self
24
    {
25
        // @todo implement
26
    }
27
28
    public function toNative(): array
29
    {
30
        return $this->compositeVector->map(static function (ValueObjectInterface $entity): array {
31
            return $entity->toNative();
32
        })->toArray();
33
    }
34
35
    public function equals(ValueObjectInterface $otherList): bool
36
    {
37
        if (!$otherList instanceof self) {
38
            return false;
39
        }
40
        if (count($this) !== count($otherList)) {
41
            return false;
42
        }
43
        foreach ($this as $pos => $value) {
44
            if (!$value->equals($otherList->get($pos))) {
45
                return false;
46
            }
47
        }
48
        return true;
49
    }
50
51
    public function __toString(): string
52
    {
53
        $parts = [];
54
        foreach ($this as $nestedEntity) {
55
            $parts[] = (string)$nestedEntity;
56
        }
57
        return implode(",\n", $parts);
58
    }
59
60
    public function diff(ValueObjectListInterface $otherList): ValueObjectListInterface
61
    {
62
        $differentEntities = [];
63
        /* @var TypedEntityInterface $entity */
64
        foreach ($this as $pos => $entity) {
65
            if (!$otherList->has($pos)) {
66
                $differentEntities[] = $entity;
67
                continue;
68
            }
69
            /* @var TypedEntityInterface $otherEntity */
70
            $otherEntity = $otherList->get($pos);
71
            $diff = $entity->getValueObjectMap()->diff($otherEntity->getValueObjectMap());
72
            if (!$diff->isEmpty()) {
73
                $differentEntities[] = $entity;
74
            }
75
        }
76
        return new static($differentEntities);
77
    }
78
79
    private function __construct(array $entities = [])
80
    {
81
        $this->init($entities, TypedEntityInterface::class);
82
    }
83
}
84

src/Entity/RelatedEntityList.php 1 location

@@ 10-82 (lines=73) @@
7
use Daikon\Entity\ValueObject\ValueObjectInterface;
8
use Daikon\Entity\ValueObject\ValueObjectListInterface;
9
10
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