|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class representation of WebAPI auction field |
|
9
|
|
|
*/ |
|
10
|
|
|
class Field |
|
11
|
|
|
{ |
|
12
|
|
|
const DATE_REGEX = '/^\d{2}\-\d{2}\-\d{4}$/'; |
|
13
|
|
|
|
|
14
|
|
|
const VALUE_STRING = 'fvalueString'; |
|
15
|
|
|
const VALUE_INT = 'fvalueInt'; |
|
16
|
|
|
const VALUE_FLOAT = 'fvalueFloat'; |
|
17
|
|
|
const VALUE_IMAGE = 'fvalueImage'; |
|
18
|
|
|
const VALUE_DATETIME = 'fvalueDatetime'; |
|
19
|
|
|
const VALUE_DATE = 'fvalueDate'; |
|
20
|
|
|
const VALUE_RANGE_INT = 'fvalueRangeInt'; |
|
21
|
|
|
const VALUE_RANGE_FLOAT = 'fvalueRangeFloat'; |
|
22
|
|
|
const VALUE_RANGE_DATE = 'fvalueRangeDate'; |
|
23
|
|
|
|
|
24
|
|
|
const DEFAULT_STRING = ''; |
|
25
|
|
|
const DEFAULT_INT = 0; |
|
26
|
|
|
const DEFAULT_FLOAT = 0; |
|
27
|
|
|
const DEFAULT_IMAGE = ''; |
|
28
|
|
|
const DEFAULT_DATETIME = 0; |
|
29
|
|
|
const DEFAULT_DATE = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Allegro WebAPI fid |
|
33
|
|
|
* @var integer |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $fid = null; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* array of fValues of this Field |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $fValues = []; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param integer $fid WebAPI fid for given field |
|
47
|
|
|
* @param mixed $value value for given field |
|
48
|
|
|
* @param string $forceValueType value type to force (i.e. fvalueImage) |
|
49
|
|
|
*/ |
|
50
|
30 |
|
public function __construct($fid = 0, $value = null, $forceValueType = '') |
|
51
|
|
|
{ |
|
52
|
30 |
|
$this->setFid($fid); |
|
53
|
29 |
|
$this->fValues = $this->getDefaults(); |
|
54
|
|
|
|
|
55
|
|
|
//null value should result in field with default values |
|
56
|
29 |
|
if (is_null($value)) { |
|
57
|
8 |
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
//if value type was specified (useful for fvalueImage, fvalueDatetime etc.) |
|
61
|
21 |
|
if ($forceValueType) { |
|
62
|
3 |
|
$this->setValueForced($forceValueType, $value); |
|
63
|
2 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
//if no forced value type is given, autodetect it |
|
67
|
19 |
|
$this->setValueAutodetect($value); |
|
68
|
17 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Default values, "empty" WebAPI fields item |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
29 |
|
protected function getDefaults() |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
29 |
|
self::VALUE_STRING => self::DEFAULT_STRING, |
|
79
|
29 |
|
self::VALUE_INT => self::DEFAULT_INT, |
|
80
|
29 |
|
self::VALUE_FLOAT => self::DEFAULT_FLOAT, |
|
81
|
29 |
|
self::VALUE_IMAGE => self::DEFAULT_IMAGE, |
|
82
|
29 |
|
self::VALUE_DATETIME => self::DEFAULT_DATETIME, |
|
83
|
29 |
|
self::VALUE_DATE => self::DEFAULT_DATE, |
|
84
|
29 |
|
self::VALUE_RANGE_INT => [ |
|
85
|
29 |
|
self::VALUE_RANGE_INT . 'Min' => self::DEFAULT_INT, |
|
86
|
29 |
|
self::VALUE_RANGE_INT . 'Max' => self::DEFAULT_INT, |
|
87
|
29 |
|
], |
|
88
|
29 |
|
self::VALUE_RANGE_FLOAT => [ |
|
89
|
29 |
|
self::VALUE_RANGE_FLOAT . 'Min' => self::DEFAULT_FLOAT, |
|
90
|
29 |
|
self::VALUE_RANGE_FLOAT . 'Max' => self::DEFAULT_FLOAT, |
|
91
|
29 |
|
], |
|
92
|
29 |
|
self::VALUE_RANGE_DATE => [ |
|
93
|
29 |
|
self::VALUE_RANGE_DATE . 'Min' => self::DEFAULT_DATE, |
|
94
|
29 |
|
self::VALUE_RANGE_DATE . 'Max' => self::DEFAULT_DATE, |
|
95
|
29 |
|
], |
|
96
|
29 |
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set fid of this Field |
|
102
|
|
|
* @param integer $fid |
|
103
|
|
|
*/ |
|
104
|
30 |
|
public function setFid($fid) |
|
105
|
|
|
{ |
|
106
|
30 |
|
if (!is_integer($fid)) { |
|
107
|
1 |
|
throw new InvalidArgumentException('fid must be an integer, ' . gettype($fid) . ' given'); |
|
108
|
|
|
} |
|
109
|
29 |
|
$this->fid = $fid; |
|
110
|
29 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set value to fValue index of corresponding type |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
*/ |
|
117
|
19 |
|
protected function setValueAutodetect($value) |
|
118
|
|
|
{ |
|
119
|
19 |
|
if (is_integer($value)) { |
|
120
|
4 |
|
$this->fValues[self::VALUE_INT] = $value; |
|
121
|
19 |
|
} elseif (is_float($value)) { |
|
122
|
4 |
|
$this->fValues[self::VALUE_FLOAT] = $value; |
|
123
|
18 |
|
} elseif (is_string($value)) { |
|
124
|
9 |
|
$this->setValueStringAutodetect($value); |
|
125
|
17 |
|
} elseif (is_array($value)) { |
|
126
|
7 |
|
$this->setValueRangeAutodetect($value); |
|
127
|
6 |
|
} else { |
|
128
|
1 |
|
throw new InvalidArgumentException('Not supported value type: ' . gettype($value) . "; fid={$this->fid}"); |
|
129
|
|
|
} |
|
130
|
17 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Detect type of string value (date or normal string) |
|
135
|
|
|
* @param string $value value to detect type |
|
136
|
|
|
*/ |
|
137
|
9 |
|
protected function setValueStringAutodetect($value) |
|
138
|
|
|
{ |
|
139
|
9 |
|
if (preg_match(self::DATE_REGEX, $value)) { |
|
140
|
4 |
|
$this->fValues[self::VALUE_DATE] = $value; |
|
141
|
4 |
|
} else { |
|
142
|
8 |
|
$this->fValues[self::VALUE_STRING] = $value; |
|
143
|
|
|
} |
|
144
|
9 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Detect type of range passed as argument (int, float, date) |
|
149
|
|
|
* @param array $value value to detect type of |
|
|
|
|
|
|
150
|
|
|
*/ |
|
151
|
7 |
|
protected function setValueRangeAutodetect(array $range) |
|
152
|
|
|
{ |
|
153
|
7 |
|
if (count($range) !== 2) { |
|
154
|
1 |
|
throw new InvalidArgumentException('Range array must have exactly 2 elements'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
//make sure array has numeric keys |
|
158
|
6 |
|
$range = array_values($range); |
|
159
|
|
|
|
|
160
|
6 |
|
if ($this->isRangeFloat($range)) { |
|
161
|
2 |
|
$this->setRangeFloat($range); |
|
162
|
6 |
|
} elseif ($this->isRangeInt($range)) { |
|
163
|
2 |
|
$this->setRangeInt($range); |
|
164
|
4 |
|
} elseif ($this->isRangeDate($range)) { |
|
165
|
2 |
|
$this->setRangeDate($range); |
|
166
|
2 |
|
} |
|
167
|
6 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Sets float range values from given array |
|
172
|
|
|
* @param array $range array of two float values |
|
173
|
|
|
*/ |
|
174
|
2 |
|
protected function setRangeFloat(array $range) |
|
175
|
|
|
{ |
|
176
|
2 |
|
asort($range); |
|
177
|
2 |
|
$this->fValues[self::VALUE_RANGE_FLOAT] = array_combine( |
|
178
|
2 |
|
['fvalueRangeFloatMin', 'fvalueRangeFloatMax'], |
|
179
|
|
|
$range |
|
180
|
2 |
|
); |
|
181
|
2 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Sets int range values from given array |
|
186
|
|
|
* @param array $range array of two int values |
|
187
|
|
|
*/ |
|
188
|
2 |
|
protected function setRangeInt(array $range) |
|
189
|
|
|
{ |
|
190
|
2 |
|
asort($range); |
|
191
|
2 |
|
$this->fValues[self::VALUE_RANGE_INT] = array_combine( |
|
192
|
2 |
|
['fvalueRangeIntMin', 'fvalueRangeIntMax'], |
|
193
|
|
|
$range |
|
194
|
2 |
|
); |
|
195
|
2 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Sets date range values from given array |
|
200
|
|
|
* @param array $range array of two date values |
|
201
|
|
|
*/ |
|
202
|
2 |
|
protected function setRangeDate(array $range) |
|
203
|
|
|
{ |
|
204
|
|
|
usort($range, function($date1, $date2){ |
|
205
|
2 |
|
return strtotime($date1) - strtotime($date2); |
|
206
|
2 |
|
}); |
|
207
|
2 |
|
$this->fValues[self::VALUE_RANGE_DATE] = array_combine( |
|
208
|
2 |
|
['fvalueRangeDateMin', 'fvalueRangeDateMax'], |
|
209
|
|
|
$range |
|
210
|
2 |
|
); |
|
211
|
2 |
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Checks if given range is float |
|
216
|
|
|
* @param array $range range to check |
|
217
|
|
|
* @return boolean |
|
218
|
|
|
*/ |
|
219
|
6 |
|
protected function isRangeFloat(array $range) |
|
220
|
|
|
{ |
|
221
|
6 |
|
$floats = array_filter($range, 'is_float'); |
|
222
|
6 |
|
return (count($floats) == 2); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Checks if given range is int |
|
228
|
|
|
* @param array $range range to check |
|
229
|
|
|
* @return boolean |
|
230
|
|
|
*/ |
|
231
|
4 |
|
protected function isRangeInt(array $range) |
|
232
|
|
|
{ |
|
233
|
4 |
|
$ints = array_filter($range, 'is_int'); |
|
234
|
4 |
|
return (count($ints) == 2); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Checks if given range is date |
|
240
|
|
|
* @param array $range range to check |
|
241
|
|
|
* @return boolean |
|
242
|
|
|
*/ |
|
243
|
|
|
public function isRangeDate(array $range) |
|
244
|
|
|
{ |
|
245
|
2 |
|
$dates = array_filter($range, function($item){ |
|
246
|
2 |
|
return preg_match(self::DATE_REGEX, $item); |
|
247
|
2 |
|
}); |
|
248
|
|
|
|
|
249
|
2 |
|
return (count($dates) == 2); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Set value of arbitrary type |
|
255
|
|
|
* @param string $forceValueType type ('fvalueString', 'fvalueInt', ...) |
|
256
|
|
|
* @param mixed $value to set |
|
257
|
|
|
*/ |
|
258
|
3 |
|
protected function setValueForced($forceValueType, $value) |
|
259
|
|
|
{ |
|
260
|
3 |
|
if (!array_key_exists($forceValueType, $this->fValues)) { |
|
261
|
1 |
|
throw new InvalidArgumentException("Class " . get_class($this) . " does not have property: {$forceValueType}"); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
2 |
|
$this->fValues[$forceValueType] = $value; |
|
265
|
2 |
|
} |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Returns WebAPI representation of Field |
|
270
|
|
|
* @return array field |
|
271
|
|
|
*/ |
|
272
|
17 |
|
public function toArray() |
|
273
|
|
|
{ |
|
274
|
17 |
|
$this->fValues['fid'] = $this->fid; |
|
275
|
17 |
|
return $this->fValues; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Creates object from WebAPI representation of Field |
|
281
|
|
|
*/ |
|
282
|
8 |
|
public function fromArray(array $array) |
|
283
|
|
|
{ |
|
284
|
|
|
//recursive object to array :) |
|
285
|
8 |
|
$array = json_decode(json_encode($array), true); |
|
286
|
|
|
|
|
287
|
8 |
|
if (!array_key_exists('fid', $array)) { |
|
288
|
1 |
|
throw new InvalidArgumentException('Fid is required'); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
7 |
|
$this->setFid($array['fid']); |
|
292
|
7 |
|
unset($array['fid']); |
|
293
|
|
|
|
|
294
|
7 |
|
foreach ($array as $key => $value) { |
|
295
|
7 |
|
if (!array_key_exists($key, $this->fValues)) { |
|
296
|
1 |
|
throw new InvalidArgumentException("Unknown Field property: {$key}"); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
7 |
|
$this->fValues[$key] = $value; |
|
300
|
7 |
|
} |
|
301
|
6 |
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Return field fid |
|
306
|
|
|
* @return integer |
|
307
|
|
|
*/ |
|
308
|
6 |
|
public function getFid() |
|
309
|
|
|
{ |
|
310
|
6 |
|
return $this->fid; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Return first property that is different from its default value |
|
316
|
|
|
* @return mixed | null |
|
317
|
|
|
*/ |
|
318
|
17 |
|
public function getValue() |
|
319
|
|
|
{ |
|
320
|
17 |
|
$defaults = $this->getDefaults(); |
|
321
|
17 |
|
foreach ($this->fValues as $key => $fValue) { |
|
322
|
17 |
|
if ($fValue !== $defaults[$key]) { |
|
323
|
16 |
|
return is_array($fValue) ? array_values($fValue) : $fValue; |
|
324
|
|
|
} |
|
325
|
15 |
|
} |
|
326
|
|
|
|
|
327
|
|
|
//if all values are at defaults, we're unable to determine |
|
328
|
|
|
//which one was set without additional business logic involving |
|
329
|
|
|
//fids - especially if the defaults come from WebAPI (fromArray()) |
|
330
|
1 |
|
return null; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
|
|
334
|
|
|
} |
|
335
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.