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