1 | <?php |
||
11 | class Field |
||
12 | { |
||
13 | const DATE_REGEX = '/^\d{2}\-\d{2}\-\d{4}$/'; |
||
14 | |||
15 | const VALUE_STRING = 'fvalueString'; |
||
16 | const VALUE_INT = 'fvalueInt'; |
||
17 | const VALUE_FLOAT = 'fvalueFloat'; |
||
18 | const VALUE_IMAGE = 'fvalueImage'; |
||
19 | const VALUE_DATETIME = 'fvalueDatetime'; |
||
20 | const VALUE_DATE = 'fvalueDate'; |
||
21 | const VALUE_RANGE_INT = 'fvalueRangeInt'; |
||
22 | const VALUE_RANGE_FLOAT = 'fvalueRangeFloat'; |
||
23 | const VALUE_RANGE_DATE = 'fvalueRangeDate'; |
||
24 | |||
25 | const DEFAULT_STRING = ''; |
||
26 | const DEFAULT_INT = 0; |
||
27 | const DEFAULT_FLOAT = 0; |
||
28 | const DEFAULT_IMAGE = ''; |
||
29 | const DEFAULT_DATETIME = 0; |
||
30 | const DEFAULT_DATE = ''; |
||
31 | |||
32 | /** |
||
33 | * Allegro WebAPI fid |
||
34 | * @var integer |
||
35 | */ |
||
36 | protected $fid = null; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * array of fValues of this Field |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $fValues = []; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * @param integer $fid WebAPI fid for given field |
||
48 | * @param mixed $value value for given field |
||
49 | * @param string $forceValueType value type to force (i.e. fvalueImage) |
||
50 | */ |
||
51 | 30 | public function __construct($fid = 0, $value = null, $forceValueType = '') |
|
70 | |||
71 | |||
72 | /** |
||
73 | * Default values, "empty" WebAPI fields item |
||
74 | * @return array |
||
75 | */ |
||
76 | 29 | protected function getDefaults() |
|
99 | |||
100 | |||
101 | /** |
||
102 | * Set fid of this Field |
||
103 | * @param integer $fid |
||
104 | */ |
||
105 | 30 | public function setFid($fid) |
|
112 | |||
113 | |||
114 | /** |
||
115 | * Set value to fValue index of corresponding type |
||
116 | * @param mixed $value |
||
117 | */ |
||
118 | 19 | protected function setValueAutodetect($value) |
|
132 | |||
133 | |||
134 | /** |
||
135 | * Detect type of string value (date or normal string) |
||
136 | * @param string $value value to detect type |
||
137 | */ |
||
138 | 9 | protected function setValueStringAutodetect($value) |
|
146 | |||
147 | |||
148 | /** |
||
149 | * Detect type of range passed as argument (int, float, date) |
||
150 | * @param array $range range to detect type of |
||
151 | */ |
||
152 | 7 | protected function setValueRangeAutodetect(array $range) |
|
169 | |||
170 | |||
171 | /** |
||
172 | * Sets float range values from given array |
||
173 | * @param array $range array of two float values |
||
174 | */ |
||
175 | 2 | protected function setRangeFloat(array $range) |
|
183 | |||
184 | |||
185 | /** |
||
186 | * Sets int range values from given array |
||
187 | * @param array $range array of two int values |
||
188 | */ |
||
189 | 2 | protected function setRangeInt(array $range) |
|
197 | |||
198 | |||
199 | /** |
||
200 | * Sets date range values from given array |
||
201 | * @param array $range array of two date values |
||
202 | */ |
||
203 | 2 | protected function setRangeDate(array $range) |
|
213 | |||
214 | |||
215 | /** |
||
216 | * Checks if given range is float |
||
217 | * @param array $range range to check |
||
218 | * @return boolean |
||
219 | */ |
||
220 | 6 | protected function isRangeFloat(array $range) |
|
225 | |||
226 | |||
227 | /** |
||
228 | * Checks if given range is int |
||
229 | * @param array $range range to check |
||
230 | * @return boolean |
||
231 | */ |
||
232 | 4 | protected function isRangeInt(array $range) |
|
237 | |||
238 | |||
239 | /** |
||
240 | * Checks if given range is date |
||
241 | * @param array $range range to check |
||
242 | * @return boolean |
||
243 | */ |
||
244 | public function isRangeDate(array $range) |
||
252 | |||
253 | |||
254 | /** |
||
255 | * Set value of arbitrary type |
||
256 | * @param string $forceValueType type ('fvalueString', 'fvalueInt', ...) |
||
257 | * @param mixed $value to set |
||
258 | */ |
||
259 | 3 | protected function setValueForced($forceValueType, $value) |
|
267 | |||
268 | |||
269 | /** |
||
270 | * Returns WebAPI representation of Field |
||
271 | * @return array field |
||
272 | */ |
||
273 | 17 | public function toArray() |
|
278 | |||
279 | |||
280 | /** |
||
281 | * Creates object from WebAPI representation of Field |
||
282 | */ |
||
283 | 8 | public function fromArray(array $array) |
|
303 | |||
304 | |||
305 | /** |
||
306 | * Return field fid |
||
307 | * @return integer |
||
308 | */ |
||
309 | 6 | public function getFid() |
|
313 | |||
314 | |||
315 | /** |
||
316 | * Return first property that is different from its default value |
||
317 | * @return mixed | null |
||
318 | */ |
||
319 | 17 | public function getValue() |
|
333 | |||
334 | |||
335 | } |
||
336 |