|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\Models; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\Models\Exceptions\EntityException; |
|
10
|
|
|
use Spiral\Models\Prototypes\AbstractEntity; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This is analog of DataEntity based on constant arrays to define mutator and accessors. |
|
14
|
|
|
* |
|
15
|
|
|
* @see DataEntity |
|
16
|
|
|
*/ |
|
17
|
|
|
class StaticDateEntity extends AbstractEntity |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* List of fields must be hidden from publicFields() method. |
|
21
|
|
|
* |
|
22
|
|
|
* @see publicFields() |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
const HIDDEN = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set of fields allowed to be filled using setFields() method. |
|
30
|
|
|
* |
|
31
|
|
|
* @see setFields() |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
const FILLABLE = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List of fields not allowed to be filled by setFields() method. Replace with and empty array |
|
39
|
|
|
* to allow all fields. |
|
40
|
|
|
* |
|
41
|
|
|
* By default all entity fields are settable! Opposite behaviour has to be described in entity |
|
42
|
|
|
* child implementations. |
|
43
|
|
|
* |
|
44
|
|
|
* @see setFields() |
|
45
|
|
|
* |
|
46
|
|
|
* @var array|string |
|
47
|
|
|
*/ |
|
48
|
|
|
const SECURED = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @see setField() |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
const SETTERS = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @see getField() |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
const GETTERS = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Accessor used to mock field data and filter every request thought itself. |
|
66
|
|
|
* |
|
67
|
|
|
* @see getField() |
|
68
|
|
|
* @see setField() |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
const ACCESSORS = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function isPublic(string $field): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return !in_array($field, static::HIDDEN); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if field can be set using setFields() method. |
|
84
|
|
|
* |
|
85
|
|
|
* @see setField() |
|
86
|
|
|
* @see $fillable |
|
87
|
|
|
* @see $secured |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $field |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function isFillable(string $field): bool |
|
94
|
|
|
{ |
|
95
|
|
|
if (!empty(static::FILLABLE)) { |
|
96
|
|
|
return in_array($field, static::FILLABLE); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (static::SECURED === '*') { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return !in_array($field, static::SECURED); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Check and return name of mutator (getter, setter, accessor) associated with specific field. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $field |
|
110
|
|
|
* @param string $mutator Mutator type (setter, getter, accessor). |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed|null |
|
113
|
|
|
* |
|
114
|
|
|
* @throws EntityException |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getMutator(string $field, string $mutator) |
|
117
|
|
|
{ |
|
118
|
|
|
$target = []; |
|
119
|
|
|
switch ($mutator) { |
|
120
|
|
|
case self::MUTATOR_ACCESSOR: |
|
121
|
|
|
$target = static::ACCESSORS; |
|
122
|
|
|
break; |
|
123
|
|
|
case self::MUTATOR_GETTER: |
|
124
|
|
|
$target = static::GETTERS; |
|
125
|
|
|
break; |
|
126
|
|
|
case self::MUTATOR_SETTER: |
|
127
|
|
|
$target = static::SETTERS; |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (isset($target[$field])) { |
|
132
|
|
|
return $target[$field]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
} |