1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\DataSync\Models\Database\Delta; |
13
|
|
|
|
14
|
|
|
use Yandex\DataSync\Exception\EmptyRecordFieldValueTypeException; |
15
|
|
|
use Yandex\Common\Model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class RecordFieldValue |
19
|
|
|
* |
20
|
|
|
* @category Yandex |
21
|
|
|
* @package DataSync |
22
|
|
|
* |
23
|
|
|
* @author Alexander Khaylo <[email protected]> |
24
|
|
|
* @created 02.03.16 13:16 |
25
|
|
|
*/ |
26
|
|
|
class RecordFieldValue extends Model |
27
|
|
|
{ |
28
|
|
|
const TYPE_BINARY = 'binary'; |
29
|
|
|
const TYPE_STRING = 'string'; |
30
|
|
|
const TYPE_DOUBLE = 'double'; |
31
|
|
|
/** |
32
|
|
|
* Array elements. For each item, you must explicitly specify the type. |
33
|
|
|
*/ |
34
|
|
|
const TYPE_LIST = 'list'; |
35
|
|
|
/** |
36
|
|
|
* Date and time in UTC. |
37
|
|
|
*/ |
38
|
|
|
const TYPE_DATETIME = 'datetime'; |
39
|
|
|
const TYPE_INTEGER = 'integer'; |
40
|
|
|
const TYPE_BOOLEAN = 'boolean'; |
41
|
|
|
/** |
42
|
|
|
* Is NaN value (floating point number). |
43
|
|
|
*/ |
44
|
|
|
const TYPE_NAN = 'nan'; |
45
|
|
|
/** |
46
|
|
|
* Is the value of negative infinity. |
47
|
|
|
*/ |
48
|
|
|
const TYPE_NINF = 'ninf'; |
49
|
|
|
/** |
50
|
|
|
* Is the value of infinity. |
51
|
|
|
*/ |
52
|
|
|
const TYPE_INF = 'inf'; |
53
|
|
|
/** |
54
|
|
|
* Is NULL. |
55
|
|
|
*/ |
56
|
|
|
const TYPE_NULL = 'null'; |
57
|
|
|
|
58
|
|
|
protected $value; |
59
|
|
|
|
60
|
|
|
protected $type; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get array from object |
64
|
|
|
* |
65
|
|
|
* @param array|object $data |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
7 |
|
protected function toArrayRecursive($data) |
70
|
|
|
{ |
71
|
7 |
|
$type = $data->getType(); |
72
|
6 |
|
if (is_array($data->getValue())) { |
73
|
|
|
$result = [ |
74
|
1 |
|
self::TYPE_LIST => [], |
75
|
|
|
'type' => self::TYPE_LIST |
76
|
1 |
|
]; |
77
|
1 |
|
foreach ($data->getValue() as $key => $value) { |
78
|
1 |
|
if ($value instanceof $this) { |
79
|
1 |
|
$result[self::TYPE_LIST][] = $data->toArrayRecursive($value); |
80
|
1 |
|
} |
81
|
1 |
|
} |
82
|
1 |
|
return $result; |
83
|
|
|
} else { |
84
|
|
|
return [ |
85
|
6 |
|
$type => $data->getValue(), |
86
|
6 |
|
'type' => $type, |
87
|
6 |
|
]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set from array |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
9 |
|
public function fromArray($data) |
99
|
|
|
{ |
100
|
9 |
|
if (empty($data)) { |
101
|
5 |
|
return $this; |
102
|
|
|
} |
103
|
4 |
|
if (isset($data['type']) && $data['type']) { |
104
|
4 |
|
$this->setType($data['type']); |
105
|
4 |
|
} |
106
|
4 |
|
if (isset($data[$this->getType()]) && $data[$this->getType()]) { |
107
|
4 |
|
$this->setValue($data[$this->getType()]); |
108
|
4 |
|
} |
109
|
4 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
* @throws EmptyRecordFieldValueTypeException |
115
|
|
|
*/ |
116
|
9 |
|
public function getType() |
117
|
|
|
{ |
118
|
9 |
|
if (!$this->type) { |
119
|
5 |
|
switch (true) { |
120
|
5 |
|
case is_int($this->value): |
121
|
1 |
|
$type = self::TYPE_INTEGER; |
122
|
1 |
|
break; |
123
|
5 |
|
case is_string($this->value): |
124
|
4 |
|
$type = self::TYPE_STRING; |
125
|
4 |
|
break; |
126
|
2 |
|
case is_bool($this->value): |
127
|
1 |
|
$type = self::TYPE_BOOLEAN; |
128
|
1 |
|
break; |
129
|
2 |
|
case is_float($this->value): |
130
|
1 |
|
$type = self::TYPE_DOUBLE; |
131
|
1 |
|
break; |
132
|
2 |
|
case is_null($this->value): |
133
|
1 |
|
$type = self::TYPE_NULL; |
134
|
1 |
|
break; |
135
|
2 |
|
case is_array($this->value): |
136
|
1 |
|
$type = self::TYPE_LIST; |
137
|
1 |
|
break; |
138
|
1 |
|
default: |
139
|
1 |
|
throw new EmptyRecordFieldValueTypeException('Type of Record Field Value is empty'); |
140
|
1 |
|
} |
141
|
4 |
|
$this->type = $type; |
142
|
4 |
|
} |
143
|
8 |
|
return $this->type; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $type |
148
|
|
|
*/ |
149
|
5 |
|
public function setType($type) |
150
|
|
|
{ |
151
|
5 |
|
$this->type = $type; |
152
|
5 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return mixed|$this[] |
156
|
|
|
*/ |
157
|
6 |
|
public function getValue() |
158
|
|
|
{ |
159
|
6 |
|
return $this->value; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param mixed $value |
164
|
|
|
*/ |
165
|
9 |
|
public function setValue($value) |
166
|
|
|
{ |
167
|
9 |
|
$this->value = $value; |
168
|
9 |
|
} |
169
|
|
|
} |
170
|
|
|
|