|
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 |
|
protected function push(array &$data): void |
|
36
|
|
|
{ |
|
37
|
880 |
|
$this->results[] = &$data; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
880 |
|
public function mergeInheritanceNodes(bool $includeRole = false): void |
|
41
|
|
|
{ |
|
42
|
880 |
|
if ($this->parent === null) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
880 |
|
parent::mergeInheritanceNodes($includeRole); |
|
47
|
|
|
|
|
48
|
880 |
|
$roleField = $includeRole ? [LoaderInterface::ROLE_KEY => $this->discriminatorValue] : []; |
|
49
|
880 |
|
foreach ($this->results as $item) { |
|
50
|
880 |
|
if ($this->isEmptyKeys($this->innerKeys, $item)) { |
|
51
|
184 |
|
continue; |
|
52
|
|
|
} |
|
53
|
880 |
|
$this->parent->mergeData( |
|
54
|
880 |
|
$this->indexName, |
|
|
|
|
|
|
55
|
880 |
|
$this->intersectData($this->innerKeys, $item), |
|
56
|
880 |
|
$item + $roleField, |
|
57
|
|
|
static::OVERWRITE_DATA |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
880 |
|
$this->results = []; |
|
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
|
|
|
|