1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Properties\Definitions; |
4
|
|
|
|
5
|
|
|
use ByTIC\Common\Records\Traits\HasSmartProperties\RecordsTrait; |
|
|
|
|
6
|
|
|
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as Property; |
7
|
|
|
use Exception; |
8
|
|
|
use Nip\Records\RecordManager; |
9
|
|
|
use RecursiveDirectoryIterator; |
10
|
|
|
use RecursiveIteratorIterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Definition |
14
|
|
|
* @package ByTIC\Models\SmartProperties\Properties\Definitions |
15
|
|
|
*/ |
16
|
|
|
class Definition |
17
|
|
|
{ |
18
|
|
|
use Traits\HasItemsDirectoryTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var RecordManager|RecordsTrait |
22
|
|
|
*/ |
23
|
|
|
protected $manager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $name = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $label = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $field; |
39
|
|
|
|
40
|
|
|
protected $items = null; |
41
|
|
|
|
42
|
|
|
protected $defaultValue = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $name |
46
|
|
|
* |
47
|
|
|
* @return Property |
48
|
|
|
* @throws Exception |
49
|
|
|
*/ |
50
|
7 |
|
public function getItem($name) |
51
|
|
|
{ |
52
|
7 |
|
$items = $this->getItems(); |
53
|
7 |
|
if (! $this->hasItem($name)) { |
54
|
|
|
throw new Exception( |
55
|
|
|
'Bad Item [' . $name . '] for smart property |
56
|
|
|
[' . $this->getManager()->getController() . '::' . $this->getName() . '] |
57
|
|
|
[' . implode(',', array_keys($items)) . ']' |
|
|
|
|
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
7 |
|
return $items[$name]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return null|Property[] |
66
|
|
|
*/ |
67
|
11 |
|
public function getItems() |
68
|
|
|
{ |
69
|
11 |
|
if ($this->items == null) { |
70
|
11 |
|
$this->initItems(); |
71
|
|
|
} |
72
|
|
|
|
73
|
11 |
|
return $this->items; |
74
|
|
|
} |
75
|
|
|
|
76
|
11 |
|
public function initItems() |
77
|
|
|
{ |
78
|
11 |
|
$names = $this->getItemsNames(); |
79
|
11 |
|
$this->items = []; |
80
|
11 |
|
foreach ($names as $name) { |
81
|
11 |
|
if (! $this->isAbstractItemName($name)) { |
82
|
11 |
|
$object = $this->newStatus($name); |
83
|
11 |
|
$this->items[$object->getName()] = $object; |
84
|
|
|
} |
85
|
|
|
} |
86
|
11 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
12 |
|
public function getItemsNames() |
92
|
|
|
{ |
93
|
12 |
|
$names = $this->getItemsNamesFromManager(); |
94
|
|
|
|
95
|
12 |
|
return $names ? $names : $this->getItemsNamesFromFiles(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array|boolean |
100
|
|
|
*/ |
101
|
12 |
|
protected function getItemsNamesFromManager() |
102
|
|
|
{ |
103
|
12 |
|
$methodName = 'get' . $this->getName() . 'Names'; |
104
|
12 |
|
if (method_exists($this->getManager(), $methodName)) { |
105
|
|
|
return $this->getManager()->$methodName(); |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
17 |
|
public function getName() |
115
|
|
|
{ |
116
|
17 |
|
if ($this->name === null) { |
117
|
17 |
|
$this->initName(); |
118
|
|
|
} |
119
|
|
|
|
120
|
17 |
|
return $this->name; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param mixed $name |
125
|
|
|
*/ |
126
|
17 |
|
public function setName($name) |
127
|
|
|
{ |
128
|
17 |
|
$this->name = $name; |
129
|
17 |
|
} |
130
|
|
|
|
131
|
17 |
|
protected function initName() |
132
|
|
|
{ |
133
|
17 |
|
$name = inflector()->classify($this->getField()); |
134
|
17 |
|
$this->setName($name); |
135
|
17 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
17 |
|
public function getField() |
141
|
|
|
{ |
142
|
17 |
|
return $this->field; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $field |
147
|
|
|
*/ |
148
|
16 |
|
public function setField($field) |
149
|
|
|
{ |
150
|
16 |
|
$this->field = $field; |
151
|
16 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return RecordManager |
155
|
|
|
*/ |
156
|
14 |
|
public function getManager() |
157
|
|
|
{ |
158
|
14 |
|
return $this->manager; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param RecordManager|RecordsTrait $manager |
163
|
|
|
*/ |
164
|
16 |
|
public function setManager($manager) |
165
|
|
|
{ |
166
|
16 |
|
$this->manager = $manager; |
167
|
16 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
12 |
|
protected function getItemsNamesFromFiles() |
173
|
|
|
{ |
174
|
12 |
|
$directory = $this->getItemsDirectory(); |
175
|
12 |
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getItemsDirectory())); |
176
|
12 |
|
$names = []; |
177
|
12 |
|
foreach ($files as $file) { |
178
|
12 |
|
if ($file->isDir()) { |
179
|
12 |
|
continue; |
180
|
|
|
} |
181
|
12 |
|
$name = str_replace($directory, '', $file->getPathname()); |
182
|
12 |
|
$name = str_replace('.php', '', $name); |
183
|
12 |
|
$names[] = trim($name, DIRECTORY_SEPARATOR . '\\'); |
184
|
|
|
} |
185
|
|
|
|
186
|
12 |
|
return array_unique($names); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
13 |
|
public function getLabel() |
194
|
|
|
{ |
195
|
13 |
|
if ($this->label === null) { |
196
|
13 |
|
$this->initLabel(); |
197
|
|
|
} |
198
|
|
|
|
199
|
13 |
|
return $this->label; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $label |
204
|
|
|
*/ |
205
|
13 |
|
public function setLabel($label) |
206
|
|
|
{ |
207
|
13 |
|
$this->label = $label; |
208
|
13 |
|
} |
209
|
|
|
|
210
|
13 |
|
protected function initLabel() |
211
|
|
|
{ |
212
|
13 |
|
$name = inflector()->pluralize($this->getName()); |
213
|
13 |
|
$this->setLabel($name); |
214
|
13 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $name |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
11 |
|
public function isAbstractItemName($name) |
222
|
|
|
{ |
223
|
11 |
|
if (in_array($name, ['Abstract', 'Generic'])) { |
224
|
|
|
return true; |
225
|
|
|
} |
226
|
11 |
|
if (strpos($name, 'Abstract') === 0) { |
227
|
11 |
|
return true; |
228
|
|
|
} |
229
|
11 |
|
if (strpos($name, '\Abstract') !== false) { |
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
11 |
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $type |
238
|
|
|
* |
239
|
|
|
* @return Property |
240
|
|
|
*/ |
241
|
11 |
|
public function newStatus($type = null) |
242
|
|
|
{ |
243
|
11 |
|
$className = $this->getItemClass($type); |
|
|
|
|
244
|
11 |
|
$object = new $className(); |
245
|
|
|
/** @var Property $object */ |
246
|
11 |
|
$object->setManager($this->getManager()); |
247
|
11 |
|
$object->setField($this->getField()); |
248
|
|
|
|
249
|
11 |
|
return $object; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param null $type |
|
|
|
|
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
11 |
|
public function getItemClass($type = null) |
258
|
|
|
{ |
259
|
11 |
|
$type = $type ? $type : $this->getDefaultValue(); |
|
|
|
|
260
|
|
|
|
261
|
11 |
|
return $this->getPropertyItemsRootNamespace() . inflector()->classify($type); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
3 |
|
public function getDefaultValue() |
268
|
|
|
{ |
269
|
3 |
|
if ($this->defaultValue === null) { |
270
|
3 |
|
$this->initDefaultValue(); |
271
|
|
|
} |
272
|
|
|
|
273
|
3 |
|
return $this->defaultValue; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param null $defaultValue |
|
|
|
|
278
|
|
|
*/ |
279
|
3 |
|
public function setDefaultValue($defaultValue) |
280
|
|
|
{ |
281
|
3 |
|
$this->defaultValue = $defaultValue; |
282
|
3 |
|
} |
283
|
|
|
|
284
|
3 |
|
protected function initDefaultValue() |
285
|
|
|
{ |
286
|
3 |
|
$managerDefaultValue = $this->getDefaultValueFromManager(); |
287
|
3 |
|
if ($managerDefaultValue && $this->hasItem($managerDefaultValue)) { |
288
|
|
|
$defaultValue = $managerDefaultValue; |
289
|
|
|
} else { |
290
|
3 |
|
$keys = array_keys($this->getItems()); |
|
|
|
|
291
|
3 |
|
$defaultValue = reset($keys); |
292
|
|
|
} |
293
|
3 |
|
$this->setDefaultValue($defaultValue); |
|
|
|
|
294
|
3 |
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return bool|string |
298
|
|
|
*/ |
299
|
3 |
|
protected function getDefaultValueFromManager() |
300
|
|
|
{ |
301
|
3 |
|
$method = 'getDefault' . $this->getName(); |
302
|
3 |
|
if (method_exists($this->getManager(), $method)) { |
303
|
|
|
return $this->getManager()->{$method}(); |
304
|
|
|
} |
305
|
|
|
|
306
|
3 |
|
return false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param $name |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
7 |
|
public function hasItem($name) |
315
|
|
|
{ |
316
|
7 |
|
$items = $this->getItems(); |
317
|
|
|
|
318
|
7 |
|
return isset($items[$name]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
11 |
|
protected function getPropertyItemsRootNamespace() |
325
|
|
|
{ |
326
|
11 |
|
$method = 'get' . $this->getName() . 'ItemsRootNamespace'; |
327
|
11 |
|
if (method_exists($this->getManager(), $method)) { |
328
|
|
|
return $this->getManager()->{$method}(); |
329
|
|
|
} |
330
|
|
|
|
331
|
11 |
|
return $this->getManager()->getModelNamespace() . $this->getLabel() . '\\'; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param $name |
336
|
|
|
* |
337
|
|
|
* @return array |
338
|
|
|
*/ |
339
|
2 |
|
public function getValues($name) |
340
|
|
|
{ |
341
|
2 |
|
$return = []; |
342
|
2 |
|
$items = $this->getItems(); |
343
|
|
|
|
344
|
2 |
|
foreach ($items as $type) { |
345
|
2 |
|
$method = 'get' . ucfirst($name); |
346
|
2 |
|
if (method_exists($type, $method)) { |
347
|
2 |
|
$return[] = $type->$method(); |
348
|
|
|
} else { |
349
|
|
|
$return[] = $type->{$name}; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
2 |
|
return $return; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
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