Passed
Push — master ( a5c74c...eed2cc )
by Radosław
02:38
created

Field::getFid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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_IMAGE = 'fvalueImage';
11
12
    const DEFAULT_STRING = '';
13
    const DEFAULT_INT = 0;
14
    const DEFAULT_FLOAT = 0;
15
    const DEFAULT_IMAGE = '';
16
    const DEFAULT_DATETIME = 0;
17
    const DEFAULT_DATE = '';
18
19
    /**
20
     * Allegro WebAPI fid
21
     * @var integer
22
     */
23
    protected $fid = null;
24
25
    /**
26
     * String value of given field
27
     * @var string
28
     */
29
    protected $fvalueString = self::DEFAULT_STRING;
30
31
    /**
32
     * Integer value of given field
33
     * @var integer
34
     */
35
    protected $fvalueInt = self::DEFAULT_INT;
36
37
38
    /**
39
     * Float value of given field
40
     * @var float
41
     */
42
    protected $fvalueFloat = self::DEFAULT_FLOAT;
43
44
    /**
45
     * Image (image file content)
46
     * @var mixed
47
     */
48
    protected $fvalueImage = self::DEFAULT_IMAGE;
49
50
    /**
51
     * Unix time
52
     * @var float
53
     */
54
    protected $fvalueDatetime = self::DEFAULT_DATETIME;
55
56
    /**
57
     * Date (dd-mm-yyyy)
58
     * @var string
59
     */
60
    protected $fvalueDate = self::DEFAULT_DATE;
61
62
    /**
63
     * Integer range
64
     * @var array
65
     */
66
    protected $fvalueRangeInt = [
67
        'fvalueRangeIntMin' => self::DEFAULT_INT,
68
        'fvalueRangeIntMax' => self::DEFAULT_INT,
69
    ];
70
71
    /**
72
     * Float range
73
     * @var array
74
     */
75
    protected $fvalueRangeFloat = [
76
        'fvalueRangeFloatMin' => self::DEFAULT_FLOAT,
77
        'fvalueRangeFloatMax' => self::DEFAULT_FLOAT,
78
    ];
79
80
    /**
81
     * Date range
82
     * @var array
83
     */
84
    protected $fvalueRangeDate = [
85
        'fvalueRangeDateMin' => self::DEFAULT_DATE,
86
        'fvalueRangeDateMax' => self::DEFAULT_DATE,
87
    ];
88
89
    /**
90
     * @param integer $fid WebAPI fid for given field
91
     * @param mixed $value value for given field
92
     * @param string $forceValueType value type to force (i.e. fvalueImage)
93
     */
94 10
    public function __construct($fid, $value = null, $forceValueType = '')
95
    {
96 10
        if (!is_integer($fid)) {
97 1
            throw new Exception('fid must be an integer, ' . gettype($fid) . ' given');
98
        }
99 9
        $this->fid = $fid;
100
101
        //if value type was specified (useful for fvalueImage, fvalueDatetime etc.)
102 9
        if ($forceValueType) {
103 2
            $this->setValueForced($forceValueType, $value);
104 1
            return;
105
        }
106
107
        //if no forced value type is given, autodetect it
108 7
        $this->setValueAutodetect($value);
109 6
    }
110
111
112 7
    protected function setValueAutodetect($value)
113
    {
114 7
        if (is_integer($value)) {
115 1
            $this->fvalueInt = $value;
116 7
        } elseif (is_float($value)) {
117 1
            $this->fvalueFloat = $value;
118 6
        } elseif (is_string($value)) {
119 4
            $this->setValueStringAutodetect($value);
120 5
        } elseif (is_array($value)) {
121
            $this->setValueRangeAutodetect($value);
122
        } else {
123 1
            throw new Exception('Not supported value type: ' . gettype($value) . "; fid={$this->fid}");
124
        }
125 6
    }
126
127
128
    /**
129
     * Detect type of string value (date or normal string)
130
     * @param string $value value to detect type
131
     */
132 4
    protected function setValueStringAutodetect($value)
133
    {
134 4
        if (preg_match('/^\d{2}\-\d{2}\-\d{4}$/', $value)) {
135 1
            $this->fvalueDate = $value;
136 1
        } else {
137 3
            $this->fvalueString = $value;
138
        }
139 4
    }
140
141
    /**
142
     * Detect type of range passed as argument (int, float, date)
143
     * @param array $value value to detect type
144
     */
145
    protected function setValueRangeAutodetect(array $value)
146
    {
147
        if (count($value) !== 2) {
148
            throw new Exception('Range array must have exactly 2 elements');
149
        }
150
    }
151
152
153 2
    protected function setValueForced($forceValueType, $value)
154
    {
155 2
        if (!property_exists($this, $forceValueType)) {
156 1
            throw new Exception("Class " . get_class($this) . " does not have property: {$forceValueType}");
157
        }
158
159 1
        $this->{$forceValueType} = $value;
160 1
    }
161
162
163
    /**
164
     * Returns WebAPI representation of Field
165
     * @return array field
166
     */
167 6
    public function toArray()
168
    {
169
        return [
170 6
            'fid' => $this->fid,
171 6
            'fvalueString' => $this->fvalueString,
172 6
            'fvalueInt' => $this->fvalueInt,
173 6
            'fvalueFloat' => $this->fvalueFloat,
174 6
            'fvalueImage' => $this->fvalueImage,
175 6
            'fvalueDatetime' => $this->fvalueDatetime,
176 6
            'fvalueDate' => $this->fvalueDate,
177 6
            'fvalueRangeInt' => $this->fvalueRangeInt,
178 6
            'fvalueRangeFloat' => $this->fvalueRangeFloat,
179 6
            'fvalueRangeDate' => $this->fvalueRangeDate,
180 6
        ];
181
    }
182
183
184
    /**
185
     * Creates object from WebAPI representation of Field
186
     */
187
    public function fromArray(array $array)
188
    {
189
        //recursive object to array :)
190
        $array = json_decode(json_encode($array), true);
191
192
        foreach($array as $key => $value) {
193
            if (!property_exists($this, $key)) {
194
                throw new Exception("Unknown Field property: {$key}");
195
            }
196
            $this->{$key} = $value;
197
        }
198
    }
199
200
201 1
    public function getFid()
202
    {
203 1
        return $this->fid;
204
    }
205
206
207
    /**
208
     * Return first property that is different from its default value
209
     * @return mixed | null
210
     */
211 5
    public function getValue()
212
    {
213 5
        if ($this->fvalueString !== self::DEFAULT_STRING) {
214 1
            return $this->fvalueString;
215
        }
216
217 4
        if ($this->fvalueInt !== self::DEFAULT_INT) {
218 1
            return $this->fvalueInt;
219
        }
220
221 3
        if ($this->fvalueFloat !== self::DEFAULT_FLOAT) {
222 1
            return $this->fvalueFloat;
223
        }
224
225 2
        if ($this->fvalueImage !== self::DEFAULT_IMAGE) {
226
            return base64_decode($this->fvalueImage);
227
        }
228
229 2
        if ($this->fvalueDatetime !== self::DEFAULT_DATETIME) {
230 1
            return $this->fvalueDatetime;
231
        }
232
233 1
        if ($this->fvalueDate !== self::DEFAULT_DATE) {
234 1
            return $this->fvalueDate;
235
        }
236
237
        //@TODO ranges
238
239
        //no clue what value type it was, all are defaults, so let's return null
240
        return null;
241
    }
242
243
}
244