Bitrix-D7 22.2
Загрузка...
Поиск...
Не найдено
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 public function current()
225 {
226 return current($this->values);
227 }
228
232 public function next()
233 {
234 return next($this->values);
235 }
236
240 public function key()
241 {
242 return key($this->values);
243 }
244
248 public function valid()
249 {
250 $key = $this->key();
251 return ($key != null);
252 }
253
257 public function rewind()
258 {
259 return reset($this->values);
260 }
261
269 public function offsetExists($offset)
270 {
271 return isset($this->values[$offset]) || array_key_exists($offset, $this->values);
272 }
273
281 public function offsetGet($offset)
282 {
283 return $this->get($offset);
284 }
285
292 public function offsetSet($offset, $value)
293 {
294 $this->set($offset, $value);
295 }
296
302 public function offsetUnset($offset)
303 {
304 unset($this->values[$offset]);
305 if (isset($this->changedValues[$offset]))
306 unset($this->changedValues[$offset]);
307 }
308
312 public function count()
313 {
314 return count($this->values);
315 }
316
323 public function createClone(\SplObjectStorage $cloneEntity)
324 {
325 if ($this->isClone() && $cloneEntity->contains($this))
326 {
327 return $cloneEntity[$this];
328 }
329
330 $fieldsClone = clone $this;
331 $fieldsClone->isClone = true;
332
333 if (!$cloneEntity->contains($this))
334 {
335 $cloneEntity[$this] = $fieldsClone;
336 }
337
338 return $fieldsClone;
339 }
340
344 public function isClone()
345 {
346 return $this->isClone;
347 }
348}
isMarkedCustom(string $field)
Definition: fields.php:66
resetValues(array $values)
Definition: fields.php:155
createClone(\SplObjectStorage $cloneEntity)
Definition: fields.php:323
__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:292
setValues(array $values)
Definition: fields.php:146