|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine ORM |
|
5
|
|
|
* |
|
6
|
|
|
* Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern. |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine ORM |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @file RelationLoader.php |
|
33
|
|
|
* |
|
34
|
|
|
* The RelationLoader class |
|
35
|
|
|
* |
|
36
|
|
|
* @package Platine\Orm\Relation |
|
37
|
|
|
* @author Platine Developers Team |
|
38
|
|
|
* @copyright Copyright (c) 2020 |
|
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
40
|
|
|
* @link https://www.platine-php.com |
|
41
|
|
|
* @version 1.0.0 |
|
42
|
|
|
* @filesource |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
declare(strict_types=1); |
|
46
|
|
|
|
|
47
|
|
|
namespace Platine\Orm\Relation; |
|
48
|
|
|
|
|
49
|
|
|
use Platine\Orm\Entity; |
|
50
|
|
|
use Platine\Orm\Mapper\DataMapper; |
|
51
|
|
|
use Platine\Orm\Mapper\Proxy; |
|
52
|
|
|
use Platine\Orm\Query\EntityQuery; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @class RelationLoader |
|
56
|
|
|
* @package Platine\Orm\Relation |
|
57
|
|
|
* @template TEntity as Entity |
|
58
|
|
|
*/ |
|
59
|
|
|
class RelationLoader |
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @var EntityQuery<TEntity> |
|
64
|
|
|
*/ |
|
65
|
|
|
protected EntityQuery $query; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @var bool |
|
70
|
|
|
*/ |
|
71
|
|
|
protected bool $inverse = false; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @var ForeignKey |
|
76
|
|
|
*/ |
|
77
|
|
|
protected ForeignKey $foreignKey; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Whether it's the has many relation |
|
81
|
|
|
* @var bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected bool $hasMany = false; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The result entity query results |
|
87
|
|
|
* @var null|TEntity[] |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $results = []; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @var array<int, mixed> |
|
94
|
|
|
*/ |
|
95
|
|
|
protected array $keys = []; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Create new instance |
|
99
|
|
|
* @param EntityQuery<TEntity> $query |
|
100
|
|
|
* @param ForeignKey $foreignKey |
|
101
|
|
|
* @param bool $inverse |
|
102
|
|
|
* @param bool $hasMany |
|
103
|
|
|
* @param bool $immediate |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct( |
|
106
|
|
|
EntityQuery $query, |
|
107
|
|
|
ForeignKey $foreignKey, |
|
108
|
|
|
bool $inverse, |
|
109
|
|
|
bool $hasMany, |
|
110
|
|
|
bool $immediate |
|
111
|
|
|
) { |
|
112
|
|
|
$this->query = $query; |
|
113
|
|
|
$this->foreignKey = $foreignKey; |
|
114
|
|
|
$this->inverse = $inverse; |
|
115
|
|
|
$this->hasMany = $hasMany; |
|
116
|
|
|
|
|
117
|
|
|
if ($immediate) { |
|
118
|
|
|
$this->loadResults(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the result for the given data mapper |
|
124
|
|
|
* @param DataMapper<TEntity> $mapper |
|
125
|
|
|
* @return null|TEntity|TEntity[] |
|
|
|
|
|
|
126
|
|
|
*/ |
|
127
|
|
|
public function getResult(DataMapper $mapper): Entity|array|null |
|
128
|
|
|
{ |
|
129
|
|
|
$results = $this->loadResults(); |
|
130
|
|
|
|
|
131
|
|
|
if ($this->inverse) { |
|
132
|
|
|
$check = $this->foreignKey->extractValue($mapper->getRawColumns(), true); |
|
133
|
|
|
} else { |
|
134
|
|
|
$check = $this->foreignKey->getValue($mapper->getRawColumns(), true); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($this->hasMany) { |
|
138
|
|
|
$all = []; |
|
139
|
|
|
foreach ($this->keys as $index => $item) { |
|
140
|
|
|
if ($item === $check) { |
|
141
|
|
|
$all[] = $results[$index]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $all; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
foreach ($this->keys as $index => $item) { |
|
149
|
|
|
if ($item === $check) { |
|
150
|
|
|
return $results[$index]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Load the results |
|
159
|
|
|
* @return TEntity[] |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function loadResults(): array |
|
162
|
|
|
{ |
|
163
|
|
|
if (empty($this->results)) { |
|
164
|
|
|
$this->results = $this->query->all(); |
|
165
|
|
|
|
|
166
|
|
|
$this->keys = []; |
|
167
|
|
|
|
|
168
|
|
|
$proxy = Proxy::instance(); |
|
169
|
|
|
|
|
170
|
|
|
foreach ($this->results as $result) { |
|
171
|
|
|
if ($this->inverse) { |
|
172
|
|
|
$this->keys[] = $this->foreignKey->getValue( |
|
173
|
|
|
$proxy->getEntityColumns($result), |
|
174
|
|
|
true |
|
175
|
|
|
); |
|
176
|
|
|
} else { |
|
177
|
|
|
$this->keys[] = $this->foreignKey->extractValue( |
|
178
|
|
|
$proxy->getEntityColumns($result), |
|
179
|
|
|
true |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
return $this->results; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths