1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Configuration; |
4
|
|
|
|
5
|
|
|
use PhpDocReader\Reader; |
6
|
|
|
use Silk\Model\MappableModelInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PropertyConfiguration |
10
|
|
|
* |
11
|
|
|
* Objeto responsável por armazenar as configurações de um determinado objeto, |
12
|
|
|
* de modo a permitir uma leitura simplificada de como um determinado código deve |
13
|
|
|
* operar sobre uma propriedade de acordo com os parâmetros estabelecidos. |
14
|
|
|
* |
15
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
16
|
|
|
* @package Silk\Configuration |
17
|
|
|
*/ |
18
|
|
|
class PropertyConfiguration |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
private $value; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $ignore; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $ignoreIfNull; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $alias; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $type; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constrói uma configuração |
52
|
|
|
* @param $property |
53
|
|
|
* @param $object |
54
|
|
|
*/ |
55
|
|
|
public function __construct(\ReflectionProperty $property, $object) |
56
|
|
|
{ |
57
|
|
|
$property->setAccessible(true); |
58
|
|
|
$c = $property->class; |
59
|
|
|
$p = $property->getName(); |
60
|
|
|
|
61
|
|
|
$config = Reader::getConfig($c, $p); |
62
|
|
|
|
63
|
|
|
$this->setName($property->getName()); |
64
|
|
|
$this->setValue($property->getValue($object)); |
65
|
|
|
|
66
|
|
|
if (array_key_exists('alias', $config)) |
67
|
|
|
$this->setAlias($config['alias']); |
68
|
|
|
|
69
|
|
|
if (array_key_exists('ignore', $config)) |
70
|
|
|
$this->setIgnore($config['ignore']); |
71
|
|
|
|
72
|
|
|
if (array_key_exists('ignoreIfNull', $config)) |
73
|
|
|
$this->setIgnoreIfNull($config['ignoreIfNull']); |
74
|
|
|
|
75
|
|
|
if (array_key_exists('type', $config)) |
76
|
|
|
$this->setType($config['type']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retorna o nome da propriedade |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getName() |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Define o nome da propriedade |
90
|
|
|
* @param string $name |
91
|
|
|
*/ |
92
|
|
|
public function setName($name) |
93
|
|
|
{ |
94
|
|
|
$this->name = $name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getValue() |
101
|
|
|
{ |
102
|
|
|
if ($this->value instanceof MappableModelInterface) { |
103
|
|
|
return $this->value->getId(); |
104
|
|
|
} |
105
|
|
|
else if($this->value instanceof \DateTime){ |
106
|
|
|
return $this->value->format('Y-m-d'); |
107
|
|
|
} |
108
|
|
|
return $this->value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed $value |
113
|
|
|
*/ |
114
|
|
|
public function setValue($value) |
115
|
|
|
{ |
116
|
|
|
$this->value = $value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retorna o parâmetro de ignorar a propriedade. |
121
|
|
|
* @return boolean |
122
|
|
|
*/ |
123
|
|
|
public function ignore() |
124
|
|
|
{ |
125
|
|
|
return $this->ignore; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Define o parâmetro de ignorar a propriedade |
130
|
|
|
* @param boolean $ignore |
131
|
|
|
*/ |
132
|
|
|
public function setIgnore($ignore) |
133
|
|
|
{ |
134
|
|
|
$this->ignore = $ignore; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Retorna o valor do parâmetro de ignorar a propriedade |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
|
|
public function ignoreIfNull() |
142
|
|
|
{ |
143
|
|
|
return $this->ignoreIfNull; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Verifica se o valor deve ser ignorado. |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function shouldIgnoreIfNull() { |
151
|
|
|
if ($this->ignoreIfNull() && is_null($this->getValue())) |
152
|
|
|
{ |
153
|
|
|
return true; |
154
|
|
|
} else |
155
|
|
|
{ |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Define o parâmetro de ignorar a propriedade |
162
|
|
|
* @param boolean $ignoreIfNull |
163
|
|
|
*/ |
164
|
|
|
public function setIgnoreIfNull($ignoreIfNull) |
165
|
|
|
{ |
166
|
|
|
$this->ignoreIfNull = $ignoreIfNull; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Verifica se contém um alias na configuração |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function hasAlias() |
174
|
|
|
{ |
175
|
|
|
return (is_null($this->alias) === false); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Retorna o alias da configuração |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getAlias() |
183
|
|
|
{ |
184
|
|
|
if (!empty($this->alias)) |
185
|
|
|
return $this->alias; |
186
|
|
|
|
187
|
|
|
return $this->name; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Define o alias da configuração |
192
|
|
|
* @param string $alias |
193
|
|
|
*/ |
194
|
|
|
public function setAlias($alias) |
195
|
|
|
{ |
196
|
|
|
$this->alias = $alias; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function getType() |
203
|
|
|
{ |
204
|
|
|
return $this->type; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $type |
209
|
|
|
*/ |
210
|
|
|
public function setType($type) |
211
|
|
|
{ |
212
|
|
|
$this->type = $type; |
213
|
|
|
} |
214
|
|
|
} |