1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Definitions\Definition; |
4
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic; |
6
|
|
|
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as Property; |
7
|
|
|
use ByTIC\Models\SmartProperties\Properties\PropertiesFactory; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait HasProperties |
12
|
|
|
* @package ByTIC\Models\SmartProperties\Definitions\Definition |
13
|
|
|
*/ |
14
|
|
|
trait HasProperties |
15
|
|
|
{ |
16
|
|
|
protected $items = null; |
17
|
|
|
protected $itemsAliases = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $name |
21
|
|
|
* |
22
|
|
|
* @return Property |
23
|
|
|
* @throws Exception |
24
|
|
|
*/ |
25
|
|
|
public function getItem($name): Property |
26
|
|
|
{ |
27
|
|
|
$items = $this->getItems(); |
28
|
|
|
if (!$this->hasItem($name)) { |
29
|
|
|
throw new Exception( |
30
|
|
|
'Bad Item [' . $name . '] for smart property |
31
|
|
|
[' . $this->getManager()->getController() . '::' . $this->getName() . '] |
|
|
|
|
32
|
|
|
[' . implode(',', array_keys($items)) . ']' |
|
|
|
|
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
if (isset($this->itemsAliases[$name])) { |
36
|
|
|
$name = $this->itemsAliases[$name]; |
37
|
|
|
} |
38
|
|
|
return $items[$name]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return null|Property[] |
43
|
|
|
*/ |
44
|
|
|
public function getItems(): ?array |
45
|
|
|
{ |
46
|
|
|
$this->checkBuild(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
if ($this->items == null) { |
49
|
|
|
$this->initItems(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->items; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function initItems() |
56
|
|
|
{ |
57
|
|
|
$names = $this->getPlaces(); |
|
|
|
|
58
|
|
|
$this->items = []; |
59
|
|
|
foreach ($names as $name) { |
60
|
|
|
$object = $this->newProperty($name); |
61
|
|
|
$this->addItem($object); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $type |
67
|
|
|
* |
68
|
|
|
* @return Property |
69
|
|
|
*/ |
70
|
|
|
public function newProperty($type = null): Property |
71
|
|
|
{ |
72
|
|
|
$type = $type ?: $this->getDefaultValue(); |
|
|
|
|
73
|
|
|
return PropertiesFactory::forDefinition($this, $type); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param null $type |
|
|
|
|
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getPropertyClass($type = null): string |
82
|
|
|
{ |
83
|
|
|
$type = $type ?: $this->getDefaultValue(); |
|
|
|
|
84
|
|
|
$type = str_replace(DIRECTORY_SEPARATOR, '\\', $type); |
85
|
|
|
|
86
|
|
|
return $this->getPropertyItemsRootNamespace() . inflector()->classify($type); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Generic $object |
91
|
|
|
*/ |
92
|
|
|
public function addItem(Property $object) |
93
|
|
|
{ |
94
|
|
|
$this->items[$object->getName()] = $object; |
95
|
|
|
$this->addPlace($object->getName()); |
|
|
|
|
96
|
|
|
$aliases = $object->getAliases(); |
97
|
|
|
foreach ($aliases as $alias) { |
98
|
|
|
$this->itemsAliases[$alias] = $object->getName(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $name |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function hasItem($name): bool |
107
|
|
|
{ |
108
|
|
|
$items = $this->getItems(); |
109
|
|
|
|
110
|
|
|
return isset($items[$name]) || isset($this->itemsAliases[$name]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|