19 private $originalFields = [];
20 private $changedFields = [];
31 foreach ($fields as $name => $value)
41 if ($this->typeCaster->has($name))
43 $value = $this->cast($name, $value);
44 $this->fields[$name] = $value;
46 unset($this->originalFields[$name], $this->changedFields[$name]);
54 foreach ($fields as $name => $value)
64 if ($this->typeCaster->has($name))
66 $value = $this->cast($name, $value);
68 if ($this->markChanged($name, $value))
70 $this->fields[$name] = $value;
79 return isset($this->fields[$name]);
84 return $this->fields[$name] ??
null;
94 return isset($this->changedFields[$name]);
105 $this->changedFields = [];
106 $this->originalFields = [];
111 return $this->fields;
116 return $this->originalFields;
121 return $this->changedFields;
126 return !empty($this->changedFields);
129 private function cast($name, $value)
131 return $this->typeCaster->cast($name, $value);
134 private function markChanged($name, $value): bool
138 if ($oldValue !== $value || ($oldValue ===
null && $value ===
null))
140 $originalValue =
null;
142 if (isset($this->originalFields[$name]) || array_key_exists($name, $this->originalFields))
144 $originalValue = $this->originalFields[$name];
146 elseif (isset($this->fields[$name]) || array_key_exists($name, $this->fields))
148 $originalValue = $this->fields[$name];
151 if ($originalValue === $value)
153 unset($this->changedFields[$name], $this->originalFields[$name]);
158 if (!isset($this->originalFields[$name]) || !array_key_exists($name, $this->originalFields))
160 $this->originalFields[$name] = $oldValue;
163 $this->changedFields[$name] =
true;
174 #[\ReturnTypeWillChange]
191 #[\ReturnTypeWillChange]
194 return key($this->fields);
202 return $this->
key() !==
null;
210 reset($this->fields);
222 return isset($this->fields[$offset]) || array_key_exists($offset, $this->fields);
232 #[\ReturnTypeWillChange]
256 unset($this->fields[$offset], $this->originalFields[$offset], $this->changedFields[$offset]);
264 return count($this->fields);