Completed
Push — master ( d238ea...d39d8c )
by Radosław
02:06
created

Field::setValueForced()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 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
    public function __construct($fid, $value = null, $forceValueType = '')
85
    {
86
        $this->fid = $fid;
87
88
        //if value type was specified (useful for fvalueImage, fvalueDatetime etc.)
89
        if ($forceValueType) {
90
            $this->setValueForced($forceValueType, $value);
91
            return;
92
        }
93
94
        //if no forced value type is given, autodetect it
95
        if (is_integer($value)) {
96
            $this->fvalueInt = $value;
97
        } elseif (is_float($value)) {
98
            $this->fvalueFloat = $value;
99
        } elseif (is_string($value)) {
100
            $this->setValueStringAutodetect($value);
101
        } elseif (is_array($value) && count($value) == 2) {
102
            $this->setValueRangeAutodetect($value);
103
        } else {
104
            throw new Exception('Not supported value type: ' . gettype($value) . "; fid={$fid}");
105
        }
106
107
    }
108
109
110
    /**
111
     * Detect type of string value (date or normal string)
112
     * @param string $value value to detect type
113
     */
114
    protected function setValueStringAutodetect($value)
115
    {
116
        if (preg_match('/^\d{2}\-\d{2}\-\d{4}$/', $value)) {
117
            $this->fvalueDate = $value;
118
        } else {
119
            $this->fvalueString = $value;
120
        }
121
    }
122
123
    /**
124
     * Detect type of range passed as argument (int, float, date)
125
     * @param array $value value to detect type
126
     */
127
    protected function setValueRangeAutodetect(array $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
130
    }
131
132
133
    protected function setValueForced($forceValueType, $value)
134
    {
135
        if (!property_exists($this, $forceValueType)) {
136
            throw new Exception("Class " . get_class($this) . " does not have property: {$forceValueType}");
137
        }
138
139
        $this->{$forceValueType} = $value;
140
    }
141
142
    /**
143
     * Returns WebAPI representation of Field
144
     * @return array field
145
     */
146
    public function toArray()
147
    {
148
        return [
149
            'fid' => $this->fid,
150
            'fvalueString' => $this->fvalueString,
151
            'fvalueInt' => $this->fvalueInt,
152
            'fvalueFloat' => $this->fvalueFloat,
153
            'fvalueImage' => $this->fvalueImage,
154
            'fvalueDatetime' => $this->fvalueDatetime,
155
            'fvalueDate' => $this->fvalueDate,
156
            'fvalueRangeInt' => $this->fvalueRangeInt,
157
            'fvalueRangeFloat' => $this->fvalueRangeFloat,
158
            'fvalueRangeDate' => $this->fvalueRangeDate,
159
        ];
160
    }
161
162
163
}
164