1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Models; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Arrilot\BitrixModels\Models\Traits\HidesAttributes; |
8
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
9
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
|
12
|
|
|
abstract class ArrayableModel implements ArrayAccess, Arrayable, Jsonable, IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
use HidesAttributes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ID of the model. |
18
|
|
|
* |
19
|
|
|
* @var null|int |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Array of model fields. |
25
|
|
|
* |
26
|
|
|
* @var null|array |
27
|
|
|
*/ |
28
|
|
|
public $fields; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array of accessors to append during array transformation. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $appends = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Array related models indexed by the relation names. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $related = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set method for ArrayIterator. |
46
|
|
|
* |
47
|
|
|
* @param $offset |
48
|
|
|
* @param $value |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function offsetSet($offset, $value) |
53
|
|
|
{ |
54
|
|
|
if (is_null($offset)) { |
55
|
|
|
$this->fields[] = $value; |
56
|
|
|
} else { |
57
|
|
|
$this->fields[$offset] = $value; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Exists method for ArrayIterator. |
63
|
|
|
* |
64
|
|
|
* @param $offset |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function offsetExists($offset) |
69
|
|
|
{ |
70
|
|
|
return $this->getAccessor($offset) ? true : isset($this->fields[$offset]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Unset method for ArrayIterator. |
75
|
|
|
* |
76
|
|
|
* @param $offset |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function offsetUnset($offset) |
81
|
|
|
{ |
82
|
|
|
unset($this->fields[$offset]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get method for ArrayIterator. |
87
|
|
|
* |
88
|
|
|
* @param $offset |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function offsetGet($offset) |
93
|
|
|
{ |
94
|
|
|
$fieldValue = isset($this->fields[$offset]) ? $this->fields[$offset] : null; |
95
|
|
|
$accessor = $this->getAccessor($offset); |
96
|
|
|
|
97
|
|
|
return $accessor ? $this->$accessor($fieldValue) : $fieldValue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get an iterator for fields. |
102
|
|
|
* |
103
|
|
|
* @return ArrayIterator |
104
|
|
|
*/ |
105
|
|
|
public function getIterator() |
106
|
|
|
{ |
107
|
|
|
return new ArrayIterator($this->fields); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get accessor method name if it exists. |
112
|
|
|
* |
113
|
|
|
* @param string $field |
114
|
|
|
* |
115
|
|
|
* @return string|false |
116
|
|
|
*/ |
117
|
|
|
private function getAccessor($field) |
118
|
|
|
{ |
119
|
|
|
$method = 'get'.camel_case($field).'Attribute'; |
120
|
|
|
|
121
|
|
|
return method_exists($this, $method) ? $method : false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add value to append. |
126
|
|
|
* |
127
|
|
|
* @param array|string $attributes |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function append($attributes) |
131
|
|
|
{ |
132
|
|
|
$this->appends = array_unique( |
133
|
|
|
array_merge($this->appends, is_string($attributes) ? func_get_args() : $attributes) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Setter for appends. |
141
|
|
|
* |
142
|
|
|
* @param array $appends |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setAppends(array $appends) |
146
|
|
|
{ |
147
|
|
|
$this->appends = $appends; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Cast model to array. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function toArray() |
158
|
|
|
{ |
159
|
|
|
$array = $this->fields; |
160
|
|
|
|
161
|
|
|
foreach ($this->appends as $accessor) { |
162
|
|
|
if (isset($this[$accessor])) { |
163
|
|
|
$array[$accessor] = $this[$accessor]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach ($this->related as $key => $value) { |
168
|
|
|
if (is_object($value) && method_exists($value, 'toArray')) { |
169
|
|
|
$array[$key] = $value->toArray(); |
170
|
|
|
} elseif (is_null($value) || $value === false) { |
171
|
|
|
$array[$key] = $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (count($this->getVisible()) > 0) { |
176
|
|
|
$array = array_intersect_key($array, array_flip($this->getVisible())); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (count($this->getHidden()) > 0) { |
180
|
|
|
$array = array_diff_key($array, array_flip($this->getHidden())); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $array; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Convert model to json. |
188
|
|
|
* |
189
|
|
|
* @param int $options |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function toJson($options = 0) |
194
|
|
|
{ |
195
|
|
|
return json_encode($this->toArray(), $options); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|