Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fields.php
1<?php
3
4class Fields
5 implements \ArrayAccess, \Iterator, \Countable
6{
8 protected $values = array();
9
11 protected $changedValues = array();
12
14 private $originalValues = array();
15
17 private $customFields = array();
18
20 protected $isClone = false;
21
22
23 public function __construct(array $values = null)
24 {
25 if ($values !== null)
26 $this->values = $values;
27 }
28
33 public function isChanged($name)
34 {
35 return isset($this->changedValues[$name]);
36 }
37
44 public function get($name)
45 {
46 if (isset($this->values[$name]))
47 {
48 return $this->values[$name];
49 }
50
51 return null;
52 }
53
57 public function markCustom(string $field)
58 {
59 $this->customFields[$field] = true;
60 }
61
66 public function isMarkedCustom(string $field) : bool
67 {
68 if (!isset($this->customFields[$field]))
69 {
70 return false;
71 }
72
73 return $this->customFields[$field];
74 }
75
79 public function unmarkCustom(string $field)
80 {
81 $this->customFields[$field] = false;
82 }
83
89 public function set($name, $value)
90 {
91 if ($this->markChanged($name, $value))
92 {
93 $this->values[$name] = $value;
94 return true;
95 }
96
97 return false;
98 }
99
107 public function init($name, $value)
108 {
109 $this->values[$name] = $value;
110 return true;
111 }
112
113
114 public function clear()
115 {
116 $this->values = array();
117 $this->clearChanged();
118 }
119
120 public function clearChanged()
121 {
122 $this->changedValues = array();
123 $this->originalValues = array();
124 }
125
129 public function getValues()
130 {
131 return $this->values;
132 }
133
138 public function getOriginalValues()
139 {
140 return $this->originalValues;
141 }
142
146 public function setValues(array $values)
147 {
148 foreach ($values as $name => $value)
149 $this->set($name, $value);
150 }
151
155 public function resetValues(array $values)
156 {
157 $this->values = array();
158 if ($values !== null)
159 $this->values = $values;
160 }
161
167 protected function markChanged($name, $value)
168 {
169 $originalValuesIndex = array();
170 if (!empty($this->originalValues))
171 {
172 foreach(array_keys($this->originalValues) as $originalKey)
173 {
174 $originalValuesIndex[$originalKey] = true;
175 }
176 }
177
178 $oldValue = $this->get($name);
179 if ($oldValue != $value || ($oldValue === null && $value !== null))
180 {
181 if (!isset($originalValuesIndex[$name]))
182 {
183 $this->originalValues[$name] = $this->get($name);
184 }
185 elseif ($this->originalValues[$name] == $value)
186 {
187 unset($this->changedValues[$name]);
188 unset($this->originalValues[$name]);
189 return true;
190 }
191
192 $this->changedValues[$name] = true;
193 return true;
194 }
195
196 return false;
197 }
198
202 public function getChangedKeys()
203 {
204 return array_keys($this->changedValues);
205 }
206
210 public function getChangedValues()
211 {
212 $r = array();
213 foreach ($this->values as $k => $v)
214 {
215 if (isset($this->changedValues[$k]))
216 $r[$k] = $v;
217 }
218 return $r;
219 }
220
224 #[\ReturnTypeWillChange]
225 public function current()
226 {
227 return current($this->values);
228 }
229
233 public function next(): void
234 {
235 next($this->values);
236 }
237
241 #[\ReturnTypeWillChange]
242 public function key()
243 {
244 return key($this->values);
245 }
246
250 public function valid(): bool
251 {
252 $key = $this->key();
253 return ($key != null);
254 }
255
259 public function rewind(): void
260 {
261 reset($this->values);
262 }
263
271 public function offsetExists($offset): bool
272 {
273 return isset($this->values[$offset]) || array_key_exists($offset, $this->values);
274 }
275
283 #[\ReturnTypeWillChange]
284 public function offsetGet($offset)
285 {
286 return $this->get($offset);
287 }
288
295 public function offsetSet($offset, $value): void
296 {
297 $this->set($offset, $value);
298 }
299
305 public function offsetUnset($offset): void
306 {
307 unset($this->values[$offset]);
308 if (isset($this->changedValues[$offset]))
309 unset($this->changedValues[$offset]);
310 }
311
315 public function count(): int
316 {
317 return count($this->values);
318 }
319
326 public function createClone(\SplObjectStorage $cloneEntity): Fields
327 {
328 if ($this->isClone() && $cloneEntity->contains($this))
329 {
330 return $cloneEntity[$this];
331 }
332
333 $fieldsClone = clone $this;
334 $fieldsClone->isClone = true;
335
336 if (!$cloneEntity->contains($this))
337 {
338 $cloneEntity[$this] = $fieldsClone;
339 }
340
341 return $fieldsClone;
342 }
343
347 public function isClone()
348 {
349 return $this->isClone;
350 }
351}
isMarkedCustom(string $field)
Definition fields.php:66
resetValues(array $values)
Definition fields.php:155
createClone(\SplObjectStorage $cloneEntity)
Definition fields.php:326
__construct(array $values=null)
Definition fields.php:23
unmarkCustom(string $field)
Definition fields.php:79
markCustom(string $field)
Definition fields.php:57
markChanged($name, $value)
Definition fields.php:167
offsetSet($offset, $value)
Definition fields.php:295
setValues(array $values)
Definition fields.php:146