AssociationPathsCollection::merge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\Associate\AssociationPath;
4
5
class AssociationPathsCollection
6
{
7
    /**
8
     * @var AssociationPath[]
9
     */
10
    protected $associationPaths;
11
12
    /**
13
     * @param AssociationPath[] $associationPaths
14
     */
15
    public function __construct(array $associationPaths = [])
16
    {
17
        $this->associationPaths = $this->sortUniqueAssociatedPaths($associationPaths);
18
    }
19
20
    /**
21
     * @return AssociationPath[]
22
     */
23
    public function getAllAssociationPaths(): array
24
    {
25
        return $this->associationPaths;
26
    }
27
28
    /**
29
     * @param self $anotherAssociationPathCollection
30
     *
31
     * @return self
32
     */
33
    public function merge(self $anotherAssociationPathCollection): self
34
    {
35
        return new self(array_merge(
36
            $this->associationPaths,
37
            $anotherAssociationPathCollection->associationPaths
38
        ));
39
    }
40
41
    /**
42
     * @param AssociationPath $associationPath
43
     *
44
     * @return AssociationPath|null
45
     */
46
    public function getParentAssociationPath(AssociationPath $associationPath)
47
    {
48
        foreach (array_reverse($this->associationPaths) as $anotherAssociationPath) {
49
            /* @var AssociationPath $anotherAssociationPath */
50
            if (
51
                $associationPath->getDepth() > $anotherAssociationPath->getDepth()
52
                && $associationPath->isPartiallyEqual($anotherAssociationPath)
53
            ) {
54
                return $anotherAssociationPath;
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param AssociationPath $associationPath
61
     *
62
     * @return bool
63
     */
64
    public function hasAssociationPathOrItsChild(AssociationPath $associationPath): bool
65
    {
66
        foreach (array_reverse($this->associationPaths) as $anotherAssociationPath) {
67
            /* @var AssociationPath $anotherAssociationPath */
68
            if (
69
                $associationPath->getDepth() <= $anotherAssociationPath->getDepth()
70
                && $associationPath->isPartiallyEqual($anotherAssociationPath)
71
            ) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * @param AssociationPath[] $associationPaths
81
     *
82
     * @return AssociationPath[]
83
     */
84
    protected function sortUniqueAssociatedPaths(array $associationPaths): array
85
    {
86
        if (empty($associationPaths)) {
87
            return [];
88
        }
89
90
        usort($associationPaths, [AssociationPath::class, 'compare']);
91
92
        $uniqueAssociationPaths = [reset($associationPaths)];
93
        $i = 1;
94
        $iMax = count($associationPaths);
95
        while ($i < $iMax) {
96
            if (!$associationPaths[$i - 1]->isEqual($associationPaths[$i])) {
97
                $uniqueAssociationPaths[] = $associationPaths[$i];
98
            }
99
100
            $i += 1;
101
        }
102
103
        return $uniqueAssociationPaths;
104
    }
105
}
106