1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\ORM\Parser; |
||
6 | |||
7 | use Cycle\ORM\Select\LoaderInterface; |
||
8 | |||
9 | /** |
||
10 | * @internal |
||
11 | */ |
||
12 | abstract class AbstractMergeNode extends AbstractNode |
||
13 | { |
||
14 | protected const OVERWRITE_DATA = false; |
||
15 | |||
16 | protected array $results = []; |
||
17 | |||
18 | /** |
||
19 | * @param string[] $columns |
||
20 | * @param string[] $primaryKeys |
||
21 | * @param string[] $innerKeys Inner relation keys (for example user_id) |
||
22 | * @param string[]|null $outerKeys Outer (parent) relation keys (for example id = parent.id) |
||
23 | */ |
||
24 | 880 | public function __construct( |
|
25 | private string $discriminatorValue, |
||
26 | array $columns, |
||
27 | array $primaryKeys, |
||
28 | protected array $innerKeys, |
||
29 | ?array $outerKeys, |
||
30 | ) { |
||
31 | 880 | parent::__construct($columns, $outerKeys); |
|
32 | 880 | $this->setDuplicateCriteria($primaryKeys); |
|
33 | } |
||
34 | |||
35 | 880 | public function mergeInheritanceNodes(bool $includeRole = false): void |
|
36 | { |
||
37 | 880 | if ($this->parent === null) { |
|
38 | return; |
||
39 | } |
||
40 | 880 | ||
41 | parent::mergeInheritanceNodes($includeRole); |
||
42 | 880 | ||
43 | $roleField = $includeRole ? [LoaderInterface::ROLE_KEY => $this->discriminatorValue] : []; |
||
44 | foreach ($this->results as $item) { |
||
45 | if ($this->isEmptyKeys($this->innerKeys, $item)) { |
||
46 | 880 | continue; |
|
47 | } |
||
48 | 880 | $this->parent->mergeData( |
|
49 | 880 | $this->indexName, |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | 880 | $this->intersectData($this->innerKeys, $item), |
|
51 | 184 | $item + $roleField, |
|
52 | static::OVERWRITE_DATA, |
||
53 | 880 | ); |
|
54 | 880 | } |
|
55 | 880 | $this->results = []; |
|
56 | 880 | } |
|
57 | |||
58 | protected function push(array &$data): void |
||
59 | { |
||
60 | 880 | $this->results[] = &$data; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @psalm-pure |
||
65 | */ |
||
66 | 880 | private function isEmptyKeys(array $keys, array $data): bool |
|
67 | { |
||
68 | 880 | foreach ($keys as $key) { |
|
69 | 880 | if (isset($data[$key])) { |
|
70 | 880 | return false; |
|
71 | } |
||
72 | } |
||
73 | 184 | return true; |
|
74 | } |
||
75 | } |
||
76 |