Passed
Push — master ( 804000...66add2 )
by Radosław
02:28
created

Field::isRangeInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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_INTEGER = '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
21
    const DEFAULT_STRING = '';
22
    const DEFAULT_INT = 0;
23
    const DEFAULT_FLOAT = 0;
24
    const DEFAULT_IMAGE = '';
25
    const DEFAULT_DATETIME = 0;
26
    const DEFAULT_DATE = '';
27
28
    /**
29
     * Allegro WebAPI fid
30
     * @var integer
31
     */
32
    protected $fid = null;
33
34
    /**
35
     * String value of given field
36
     * @var string
37
     */
38
    protected $fvalueString = self::DEFAULT_STRING;
39
40
    /**
41
     * Integer value of given field
42
     * @var integer
43
     */
44
    protected $fvalueInt = self::DEFAULT_INT;
45
46
47
    /**
48
     * Float value of given field
49
     * @var float
50
     */
51
    protected $fvalueFloat = self::DEFAULT_FLOAT;
52
53
    /**
54
     * Image (image file content)
55
     * @var mixed
56
     */
57
    protected $fvalueImage = self::DEFAULT_IMAGE;
58
59
    /**
60
     * Unix time
61
     * @var float
62
     */
63
    protected $fvalueDatetime = self::DEFAULT_DATETIME;
64
65
    /**
66
     * Date (dd-mm-yyyy)
67
     * @var string
68
     */
69
    protected $fvalueDate = self::DEFAULT_DATE;
70
71
    /**
72
     * Integer range
73
     * @var array
74
     */
75
    protected $fvalueRangeInt = [
76
        'fvalueRangeIntMin' => self::DEFAULT_INT,
77
        'fvalueRangeIntMax' => self::DEFAULT_INT,
78
    ];
79
80
    /**
81
     * Float range
82
     * @var array
83
     */
84
    protected $fvalueRangeFloat = [
85
        'fvalueRangeFloatMin' => self::DEFAULT_FLOAT,
86
        'fvalueRangeFloatMax' => self::DEFAULT_FLOAT,
87
    ];
88
89
    /**
90
     * Date range
91
     * @var array
92
     */
93
    protected $fvalueRangeDate = [
94
        'fvalueRangeDateMin' => self::DEFAULT_DATE,
95
        'fvalueRangeDateMax' => self::DEFAULT_DATE,
96
    ];
97
98
    /**
99
     * @param integer $fid WebAPI fid for given field
100
     * @param mixed $value value for given field
101
     * @param string $forceValueType value type to force (i.e. fvalueImage)
102
     */
103 12
    public function __construct($fid, $value = null, $forceValueType = '')
104
    {
105 12
        if (!is_integer($fid)) {
106 1
            throw new Exception('fid must be an integer, ' . gettype($fid) . ' given');
107
        }
108 11
        $this->fid = $fid;
109
110
        //if value type was specified (useful for fvalueImage, fvalueDatetime etc.)
111 11
        if ($forceValueType) {
112 2
            $this->setValueForced($forceValueType, $value);
113 1
            return;
114
        }
115
116
        //if no forced value type is given, autodetect it
117 9
        $this->setValueAutodetect($value);
118 8
    }
119
120
121 9
    protected function setValueAutodetect($value)
122
    {
123 9
        if (is_integer($value)) {
124 1
            $this->fvalueInt = $value;
125 9
        } elseif (is_float($value)) {
126 1
            $this->fvalueFloat = $value;
127 8
        } elseif (is_string($value)) {
128 4
            $this->setValueStringAutodetect($value);
129 7
        } elseif (is_array($value)) {
130 2
            $this->setValueRangeAutodetect($value);
131 2
        } else {
132 1
            throw new Exception('Not supported value type: ' . gettype($value) . "; fid={$this->fid}");
133
        }
134 8
    }
135
136
137
    /**
138
     * Detect type of string value (date or normal string)
139
     * @param string $value value to detect type
140
     */
141 4
    protected function setValueStringAutodetect($value)
142
    {
143 4
        if (preg_match('/^\d{2}\-\d{2}\-\d{4}$/', $value)) {
144 1
            $this->fvalueDate = $value;
145 1
        } else {
146 3
            $this->fvalueString = $value;
147
        }
148 4
    }
149
150
    /**
151
     * Detect type of range passed as argument (int, float, date)
152
     * @param array $value value to detect type
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
153
     */
154 2
    protected function setValueRangeAutodetect(array $range)
155
    {
156 2
        if (count($range) !== 2) {
157
            throw new Exception('Range array must have exactly 2 elements');
158
        }
159
160
        //make sure array has numeric keys
161 2
        $range = array_values($range);
162
163 2
        asort($range);
164
165 2
        if ($this->isRangeFloat($range)) {
166 1
            $this->fvalueRangeFloat = array_combine(
167 1
                ['fvalueRangeFloatMin', 'fvalueRangeFloatMax'],
168
                $range
169 1
            );
170 2
        } elseif($this->isRangeInt($range)) {
171 1
            $this->fvalueRangeInt = array_combine(
172 1
                ['fvalueRangeIntMin', 'fvalueRangeIntMax'],
173
                $range
174 1
            );
175 1
        }
176 2
    }
177
178
179
    /**
180
     * Checks if given range is float
181
     * @param  array   $range range to check
182
     * @return boolean
183
     */
184 2
    protected function isRangeFloat(array $range)
185
    {
186 2
        $floats = array_filter($range, 'is_float');
187 2
        return (count($floats) == 2);
188
    }
189
190
191
    /**
192
     * Checks if given range is int
193
     * @param  array   $range range to check
194
     * @return boolean
195
     */
196 1
    protected function isRangeInt(array $range)
197
    {
198 1
        $ints = array_filter($range, 'is_int');
199 1
        return (count($ints) == 2);
200
    }
201
202
203
204 2
    protected function setValueForced($forceValueType, $value)
205
    {
206 2
        if (!property_exists($this, $forceValueType)) {
207 1
            throw new Exception("Class " . get_class($this) . " does not have property: {$forceValueType}");
208
        }
209
210 1
        $this->{$forceValueType} = $value;
211 1
    }
212
213
214
    /**
215
     * Returns WebAPI representation of Field
216
     * @return array field
217
     */
218 8
    public function toArray()
219
    {
220
        return [
221 8
            'fid' => $this->fid,
222 8
            'fvalueString' => $this->fvalueString,
223 8
            'fvalueInt' => $this->fvalueInt,
224 8
            'fvalueFloat' => $this->fvalueFloat,
225 8
            'fvalueImage' => $this->fvalueImage,
226 8
            'fvalueDatetime' => $this->fvalueDatetime,
227 8
            'fvalueDate' => $this->fvalueDate,
228 8
            'fvalueRangeInt' => $this->fvalueRangeInt,
229 8
            'fvalueRangeFloat' => $this->fvalueRangeFloat,
230 8
            'fvalueRangeDate' => $this->fvalueRangeDate,
231 8
        ];
232
    }
233
234
235
    /**
236
     * Creates object from WebAPI representation of Field
237
     */
238
    public function fromArray(array $array)
239
    {
240
        //recursive object to array :)
241
        $array = json_decode(json_encode($array), true);
242
243
        foreach($array as $key => $value) {
244
            if (!property_exists($this, $key)) {
245
                throw new Exception("Unknown Field property: {$key}");
246
            }
247
248
            $this->{$key} = $value;
249
        }
250
    }
251
252
253 1
    public function getFid()
254
    {
255 1
        return $this->fid;
256
    }
257
258
259
    /**
260
     * Return first property that is different from its default value
261
     * @return mixed | null
262
     */
263 7
    public function getValue()
264
    {
265 7
        if ($this->fvalueString !== self::DEFAULT_STRING) {
266 1
            return $this->fvalueString;
267
        }
268
269 6
        if ($this->fvalueInt !== self::DEFAULT_INT) {
270 1
            return $this->fvalueInt;
271
        }
272
273 5
        if ($this->fvalueFloat !== self::DEFAULT_FLOAT) {
274 1
            return $this->fvalueFloat;
275
        }
276
277 4
        if ($this->fvalueImage !== self::DEFAULT_IMAGE) {
278
            return base64_decode($this->fvalueImage);
279
        }
280
281 4
        if ($this->fvalueDatetime !== self::DEFAULT_DATETIME) {
282 1
            return $this->fvalueDatetime;
283
        }
284
285 3
        if ($this->fvalueDate !== self::DEFAULT_DATE) {
286 1
            return $this->fvalueDate;
287
        }
288
289 2
        $rangeValue = $this->getRangeValue();
290 2
        if (!is_null($rangeValue)) {
291 2
            return $rangeValue;
292
        }
293
294
        //if all values are at defaults, we're unable to determine
295
        //which one was set without additional business logic involving
296
        //fids - especially if the defaults come from WebAPI (fromArray())
297
        return null;
298
    }
299
300
301 2
    protected function getRangeValue()
302
    {
303 2
        $rangeFloat = array_values($this->fvalueRangeFloat);
304 2
        if ($rangeFloat !== array_fill(0, 2, self::DEFAULT_FLOAT)) {
305 1
            return $rangeFloat;
306
        }
307
308 1
        $rangeInt = array_values($this->fvalueRangeInt);
309 1
        if ($rangeInt !== array_fill(0, 2, self::DEFAULT_INT)) {
310 1
            return $rangeInt;
311
        }
312
313
        //@TODO date ranges
314
        return null;
315
    }
316
317
318
}
319