Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FieldStorage.php
1<?php
2
4
7
16class FieldStorage implements \ArrayAccess, \Iterator, \Countable
17{
18 private $fields = [];
19 private $originalFields = [];
20 private $changedFields = [];
21
22 private $typeCaster;
23
24 public function __construct(TypeCasterContract $typeCaster = null)
25 {
26 $this->typeCaster = $typeCaster ?? new NullTypeCaster();
27 }
28
29 public function initFields(array $fields): self
30 {
31 foreach ($fields as $name => $value)
32 {
33 $this->initField($name, $value);
34 }
35
36 return $this;
37 }
38
39 public function initField($name, $value): self
40 {
41 if ($this->typeCaster->has($name))
42 {
43 $value = $this->cast($name, $value);
44 $this->fields[$name] = $value;
45
46 unset($this->originalFields[$name], $this->changedFields[$name]);
47 }
48
49 return $this;
50 }
51
52 public function setFields(array $fields): self
53 {
54 foreach ($fields as $name => $value)
55 {
56 $this->setField($name, $value);
57 }
58
59 return $this;
60 }
61
62 public function setField($name, $value): self
63 {
64 if ($this->typeCaster->has($name))
65 {
66 $value = $this->cast($name, $value);
67
68 if ($this->markChanged($name, $value))
69 {
70 $this->fields[$name] = $value;
71 }
72 }
73
74 return $this;
75 }
76
77 public function hasField($name): bool
78 {
79 return isset($this->fields[$name]);
80 }
81
82 public function getField($name)
83 {
84 return $this->fields[$name] ?? null;
85 }
86
87 public function toArray(): array
88 {
89 return $this->fields;
90 }
91
92 public function isChanged($name): bool
93 {
94 return isset($this->changedFields[$name]);
95 }
96
97 public function clear(): void
98 {
99 $this->fields = [];
100 $this->clearChanged();
101 }
102
103 public function clearChanged(): void
104 {
105 $this->changedFields = [];
106 $this->originalFields = [];
107 }
108
109 public function getFields(): array
110 {
111 return $this->fields;
112 }
113
114 public function getOriginalFields(): array
115 {
116 return $this->originalFields;
117 }
118
119 public function getChangedFields(): array
120 {
121 return $this->changedFields;
122 }
123
124 public function hasChangedFields(): bool
125 {
126 return !empty($this->changedFields);
127 }
128
129 private function cast($name, $value)
130 {
131 return $this->typeCaster->cast($name, $value);
132 }
133
134 private function markChanged($name, $value): bool
135 {
136 $oldValue = $this->getField($name);
137
138 if ($oldValue !== $value || ($oldValue === null && $value === null))
139 {
140 $originalValue = null;
141
142 if (isset($this->originalFields[$name]) || array_key_exists($name, $this->originalFields))
143 {
144 $originalValue = $this->originalFields[$name];
145 }
146 elseif (isset($this->fields[$name]) || array_key_exists($name, $this->fields))
147 {
148 $originalValue = $this->fields[$name];
149 }
150
151 if ($originalValue === $value)
152 {
153 unset($this->changedFields[$name], $this->originalFields[$name]);
154
155 return true;
156 }
157
158 if (!isset($this->originalFields[$name]) || !array_key_exists($name, $this->originalFields))
159 {
160 $this->originalFields[$name] = $oldValue;
161 }
162
163 $this->changedFields[$name] = true;
164
165 return true;
166 }
167
168 return false;
169 }
170
174 #[\ReturnTypeWillChange]
175 public function current()
176 {
177 return current($this->fields);
178 }
179
183 public function next(): void
184 {
185 next($this->fields);
186 }
187
191 #[\ReturnTypeWillChange]
192 public function key()
193 {
194 return key($this->fields);
195 }
196
200 public function valid(): bool
201 {
202 return $this->key() !== null;
203 }
204
208 public function rewind(): void
209 {
210 reset($this->fields);
211 }
212
220 public function offsetExists($offset): bool
221 {
222 return isset($this->fields[$offset]) || array_key_exists($offset, $this->fields);
223 }
224
232 #[\ReturnTypeWillChange]
233 public function offsetGet($offset)
234 {
235 return $this->getField($offset);
236 }
237
244 public function offsetSet($offset, $value): void
245 {
246 $this->setField($offset, $value);
247 }
248
254 public function offsetUnset($offset): void
255 {
256 unset($this->fields[$offset], $this->originalFields[$offset], $this->changedFields[$offset]);
257 }
258
262 public function count(): int
263 {
264 return count($this->fields);
265 }
266}
__construct(TypeCasterContract $typeCaster=null)