Passed
Push — master ( 96610e...18fd6e )
by Radosław
02:18
created

Field::setValueStringAutodetect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
class Field
6
{
7
    const VALUE_IMAGE = 'fvalueImage';
8
9
    /**
10
     * Allegro WebAPI fid
11
     * @var integer
12
     */
13
    protected $fid = null;
14
15
    /**
16
     * String value of given field
17
     * @var string
18
     */
19
    protected $fvalueString = '';
20
21
    /**
22
     * Integer value of given field
23
     * @var integer
24
     */
25
    protected $fvalueInt = 0;
26
27
28
    /**
29
     * Float value of given field
30
     * @var float
31
     */
32
    protected $fvalueFloat = 0;
33
34
    /**
35
     * Image (image file content)
36
     * @var mixed
37
     */
38
    protected $fvalueImage = 0;
39
40
    /**
41
     * Unix time
42
     * @var float
43
     */
44
    protected $fvalueDatetime = 0;
45
46
    /**
47
     * Date (dd-mm-yyyy)
48
     * @var string
49
     */
50
    protected $fvalueDate = '';
51
52
    /**
53
     * Integer range
54
     * @var array
55
     */
56
    protected $fvalueRangeInt = [
57
        'fvalueRangeIntMin' => 0,
58
        'fvalueRangeIntMax' => 0,
59
    ];
60
61
    /**
62
     * Float range
63
     * @var array
64
     */
65
    protected $fvalueRangeFloat = [
66
        'fvalueRangeFloatMin' => 0,
67
        'fvalueRangeFloatMax' => 0,
68
    ];
69
70
    /**
71
     * Date range
72
     * @var array
73
     */
74
    protected $fvalueRangeDate = [
75
        'fvalueRangeDateMin' => '',
76
        'fvalueRangeDateMax' => '',
77
    ];
78
79
    /**
80
     * @param integer $fid WebAPI fid for given field
81
     * @param mixed $value value for given field
82
     * @param string $forceValueType value type to force (i.e. fvalueImage)
83
     */
84 5
    public function __construct($fid, $value = null, $forceValueType = '')
85
    {
86 5
        if (!is_integer($fid)) {
87 1
            throw new Exception('fid must be an integer, ' . gettype($fid) . ' given');
88
        }
89 4
        $this->fid = $fid;
90
91
        //if value type was specified (useful for fvalueImage, fvalueDatetime etc.)
92 4
        if ($forceValueType) {
93
            $this->setValueForced($forceValueType, $value);
94
            return;
95
        }
96
97
        //if no forced value type is given, autodetect it
98 4
        $this->setValueAutodetect($value);
99 4
    }
100
101
102 4
    protected function setValueAutodetect($value)
103
    {
104 4
        if (is_integer($value)) {
105 1
            $this->fvalueInt = $value;
106 4
        } elseif (is_float($value)) {
107 1
            $this->fvalueFloat = $value;
108 3
        } elseif (is_string($value)) {
109 2
            $this->setValueStringAutodetect($value);
110 2
        } elseif (is_array($value)) {
111
            $this->setValueRangeAutodetect($value);
112
        } else {
113
            throw new Exception('Not supported value type: ' . gettype($value) . "; fid={$this->fid}");
114
        }
115 4
    }
116
117
118
    /**
119
     * Detect type of string value (date or normal string)
120
     * @param string $value value to detect type
121
     */
122 2
    protected function setValueStringAutodetect($value)
123
    {
124 2
        if (preg_match('/^\d{2}\-\d{2}\-\d{4}$/', $value)) {
125 1
            $this->fvalueDate = $value;
126 1
        } else {
127 1
            $this->fvalueString = $value;
128
        }
129 2
    }
130
131
    /**
132
     * Detect type of range passed as argument (int, float, date)
133
     * @param array $value value to detect type
134
     */
135
    protected function setValueRangeAutodetect(array $value)
136
    {
137
        if (count($value) !== 2) {
138
            throw new Exception('Range array must have exactly 2 elements');
139
        }
140
    }
141
142
143
    protected function setValueForced($forceValueType, $value)
144
    {
145
        if (!property_exists($this, $forceValueType)) {
146
            throw new Exception("Class " . get_class($this) . " does not have property: {$forceValueType}");
147
        }
148
149
        $this->{$forceValueType} = $value;
150
    }
151
152
    /**
153
     * Returns WebAPI representation of Field
154
     * @return array field
155
     */
156 4
    public function toArray()
157
    {
158
        return [
159 4
            'fid' => $this->fid,
160 4
            'fvalueString' => $this->fvalueString,
161 4
            'fvalueInt' => $this->fvalueInt,
162 4
            'fvalueFloat' => $this->fvalueFloat,
163 4
            'fvalueImage' => $this->fvalueImage,
164 4
            'fvalueDatetime' => $this->fvalueDatetime,
165 4
            'fvalueDate' => $this->fvalueDate,
166 4
            'fvalueRangeInt' => $this->fvalueRangeInt,
167 4
            'fvalueRangeFloat' => $this->fvalueRangeFloat,
168 4
            'fvalueRangeDate' => $this->fvalueRangeDate,
169 4
        ];
170
    }
171
172
173
}
174