|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Definitions; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties\RecordsTrait; |
|
6
|
|
|
use Nip\Records\RecordManager; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Definition |
|
10
|
|
|
* @package ByTIC\Models\SmartProperties\Definitions |
|
11
|
|
|
*/ |
|
12
|
|
|
class Definition implements \Serializable |
|
13
|
|
|
{ |
|
14
|
|
|
use Definition\CanBuild; |
|
15
|
|
|
use Definition\HasName; |
|
16
|
|
|
use Definition\HasPlaces; |
|
17
|
|
|
use Definition\HasProperties; |
|
18
|
|
|
use Definition\HasPropertiesNamespaces; |
|
19
|
|
|
use Definition\Serializable; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var RecordManager|RecordsTrait |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $manager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $label = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $field; |
|
35
|
|
|
|
|
36
|
|
|
protected $defaultValue = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getField(): ?string |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->field; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $field |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setField($field) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->field = $field; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return RecordManager |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getManager() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->manager; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param RecordManager|RecordsTrait $manager |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setManager($manager) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->manager = $manager; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getLabel(): ?string |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->label === null) { |
|
76
|
|
|
$this->initLabel(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->label; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $label |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setLabel(string $label) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->label = $label; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function initLabel() |
|
91
|
|
|
{ |
|
92
|
|
|
$name = $this->getName(); |
|
93
|
|
|
if (empty($name)) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
$name = inflector()->pluralize($name); |
|
97
|
|
|
$this->setLabel($name); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDefaultValue(): ?string |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->defaultValue === null) { |
|
107
|
|
|
$this->initDefaultValue(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->defaultValue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param null $defaultValue |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
public function setDefaultValue($defaultValue) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->defaultValue = $defaultValue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
protected function initDefaultValue() |
|
123
|
|
|
{ |
|
124
|
|
|
$managerDefaultValue = $this->getDefaultValueFromManager(); |
|
125
|
|
|
if ($managerDefaultValue && $this->hasItem($managerDefaultValue)) { |
|
126
|
|
|
$defaultValue = $managerDefaultValue; |
|
127
|
|
|
} else { |
|
128
|
|
|
$keys = array_keys($this->getItems()); |
|
|
|
|
|
|
129
|
|
|
$defaultValue = reset($keys); |
|
130
|
|
|
} |
|
131
|
|
|
$this->setDefaultValue($defaultValue); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool|string |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getDefaultValueFromManager() |
|
138
|
|
|
{ |
|
139
|
|
|
$method = 'getDefault' . $this->getName(); |
|
140
|
|
|
if (method_exists($this->getManager(), $method)) { |
|
141
|
|
|
return $this->getManager()->{$method}(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param $name |
|
150
|
|
|
* |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getValues($name): array |
|
154
|
|
|
{ |
|
155
|
|
|
$return = []; |
|
156
|
|
|
$items = $this->getItems(); |
|
157
|
|
|
|
|
158
|
|
|
foreach ($items as $type) { |
|
159
|
|
|
$method = 'get' . ucfirst($name); |
|
160
|
|
|
if (method_exists($type, $method)) { |
|
161
|
|
|
$return[] = $type->$method(); |
|
162
|
|
|
} else { |
|
163
|
|
|
$return[] = $type->{$name}; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $return; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|