1 | <?php |
||
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) |
||
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) |
||
118 | |||
119 | /** |
||
120 | * Retorna o parâmetro de ignorar a propriedade. |
||
121 | * @return boolean |
||
122 | */ |
||
123 | public function ignore() |
||
127 | |||
128 | /** |
||
129 | * Define o parâmetro de ignorar a propriedade |
||
130 | * @param boolean $ignore |
||
131 | */ |
||
132 | public function setIgnore($ignore) |
||
136 | |||
137 | /** |
||
138 | * Retorna o valor do parâmetro de ignorar a propriedade |
||
139 | * @return boolean |
||
140 | */ |
||
141 | public function ignoreIfNull() |
||
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) |
||
168 | |||
169 | /** |
||
170 | * Verifica se contém um alias na configuração |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function hasAlias() |
||
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) |
||
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getType() |
||
203 | { |
||
206 | |||
207 | /** |
||
208 | * @param string $type |
||
209 | */ |
||
210 | public function setType($type) |
||
214 | } |