1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pulsar; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ICanBoogie\Inflector; |
7
|
|
|
|
8
|
|
|
final class Property implements ArrayAccess |
9
|
|
|
{ |
10
|
|
|
const IMMUTABLE = 'immutable'; |
11
|
|
|
const MUTABLE_CREATE_ONLY = 'mutable_create_only'; |
12
|
|
|
const MUTABLE = 'mutable'; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
/** @var string|null */ |
18
|
|
|
private $type = null; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $mutable = self::MUTABLE; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $null = false; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
private $required = false; |
28
|
|
|
|
29
|
|
|
/** @var callable|string|null */ |
30
|
|
|
private $validate = null; |
31
|
|
|
|
32
|
|
|
/** @var mixed|null */ |
33
|
|
|
private $default = null; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
private $hasDefault; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $encrypted = false; |
40
|
|
|
|
41
|
|
|
/** @var bool */ |
42
|
|
|
private $persisted = true; |
43
|
|
|
|
44
|
|
|
/** @var string|null */ |
45
|
|
|
private $relation; |
46
|
|
|
|
47
|
|
|
/** @var string|null */ |
48
|
|
|
private $relation_type; |
49
|
|
|
|
50
|
|
|
/** @var string|null */ |
51
|
|
|
private $foreign_key; |
52
|
|
|
|
53
|
|
|
/** @var string|null */ |
54
|
|
|
private $local_key; |
55
|
|
|
|
56
|
|
|
/** @var string|null */ |
57
|
|
|
private $pivot_tablename; |
58
|
|
|
|
59
|
|
|
public function __construct(array $values = [], string $name = '') |
60
|
|
|
{ |
61
|
|
|
$this->name = $name; |
62
|
|
|
foreach ($values as $k => $v) { |
63
|
|
|
$this->$k = $v; |
64
|
|
|
} |
65
|
|
|
$this->hasDefault = array_key_exists('default', $values); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getName(): string |
69
|
|
|
{ |
70
|
|
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the humanized name of this property. |
75
|
|
|
*/ |
76
|
|
|
public function getTitle(Model $model): string |
77
|
|
|
{ |
78
|
|
|
// look up the property from the translator first |
79
|
|
|
if ($translator = $model->getErrors()->getTranslator()) { |
80
|
|
|
$k = 'pulsar.properties.'.$model::modelName().'.'.$this->name; |
81
|
|
|
$title = $translator->translate($k); |
82
|
|
|
if ($title != $k) { |
83
|
|
|
return $title; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// otherwise just attempt to title-ize the property name |
88
|
|
|
return Inflector::get()->titleize($this->name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getType(): ?string |
92
|
|
|
{ |
93
|
|
|
return $this->type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isMutable(): bool |
97
|
|
|
{ |
98
|
|
|
return self::MUTABLE == $this->mutable; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isMutableCreateOnly(): bool |
102
|
|
|
{ |
103
|
|
|
return self::MUTABLE_CREATE_ONLY == $this->mutable; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isImmutable(): bool |
107
|
|
|
{ |
108
|
|
|
return self::IMMUTABLE == $this->mutable; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isNullable(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isRequired(): bool |
117
|
|
|
{ |
118
|
|
|
return $this->required; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return callable|string|null |
123
|
|
|
*/ |
124
|
|
|
public function getValidationRules() |
125
|
|
|
{ |
126
|
|
|
return $this->validate; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getDefault() |
133
|
|
|
{ |
134
|
|
|
return $this->default; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function hasDefault(): bool |
138
|
|
|
{ |
139
|
|
|
return $this->hasDefault; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isPersisted(): bool |
143
|
|
|
{ |
144
|
|
|
return $this->persisted; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function isEncrypted(): bool |
148
|
|
|
{ |
149
|
|
|
return $this->encrypted; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getForeignModelClass(): ?string |
153
|
|
|
{ |
154
|
|
|
return $this->relation; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getRelationshipType(): ?string |
158
|
|
|
{ |
159
|
|
|
return $this->relation_type; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getForeignKey(): ?string |
163
|
|
|
{ |
164
|
|
|
return $this->foreign_key; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getLocalKey(): ?string |
168
|
|
|
{ |
169
|
|
|
return $this->local_key; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getPivotTablename(): ?string |
173
|
|
|
{ |
174
|
|
|
return $this->pivot_tablename; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function toArray(): array |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
|
|
'type' => $this->type, |
181
|
|
|
'mutable' => $this->mutable, |
182
|
|
|
'null' => $this->null, |
183
|
|
|
'required' => $this->required, |
184
|
|
|
'validate' => $this->validate, |
185
|
|
|
'default' => $this->default, |
186
|
|
|
'persisted' => $this->persisted, |
187
|
|
|
'encrypted' => $this->encrypted, |
188
|
|
|
'relation' => $this->relation, |
189
|
|
|
'relation_type' => $this->relation_type, |
190
|
|
|
'foreign_key' => $this->foreign_key, |
191
|
|
|
'local_key' => $this->local_key, |
192
|
|
|
'pivot_tablename' => $this->getPivotTablename(), |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function offsetExists($offset) |
197
|
|
|
{ |
198
|
|
|
return property_exists($this, $offset) && $this->$offset !== null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function offsetGet($offset) |
202
|
|
|
{ |
203
|
|
|
if (!property_exists($this, $offset)) { |
204
|
|
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->$offset; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function offsetSet($offset, $value) |
211
|
|
|
{ |
212
|
|
|
throw new \RuntimeException('Modifying a model property is not allowed.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function offsetUnset($offset) |
216
|
|
|
{ |
217
|
|
|
throw new \RuntimeException('Modifying a model property is not allowed.'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|