Array and BackedEnum conversions. * * @param mixed $values Data array or Eloquent Model instance. * @return void * @throws ReflectionException */ public function fill(mixed $values = []): void { $data = $values instanceof Model ? $values->attributesToArray() : (array) $values; foreach (get_object_vars($this) as $property => $defaultValue) { $hasModelValue = $values instanceof Model && isset($values->{$property}); if (! array_key_exists($property, $data) && ! $hasModelValue) { continue; } $value = $values instanceof Model ? $values->{$property} : $data[$property]; $refProperty = new ReflectionProperty($this, $property); $type = $refProperty->getType(); if($type) { $typeName = null; if ($type instanceof \ReflectionNamedType) { $typeName = $type->getName(); } elseif ($type instanceof \ReflectionUnionType) { $typeName = (string) $type; } if ($typeName === 'array' && $value instanceof Collection) { $value = $value->toArray(); } if (($typeName === Collection::class || is_subclass_of($typeName, Collection::class)) && is_array($value)) { $value = collect($value); } if (enum_exists($typeName) && ! ($value instanceof BackedEnum) && $value !== null) { $value = $typeName::tryFrom((string) $value); } } // Assign hydrated value back to the Form property $this->{$property} = $value; } } /** * Dehydrate form properties into a clean array representation suitable for DTOs or Models. * Converts Collections to arrays and Enums to primitive values. * * @return array */ public function toModelArray(): array { $result = []; foreach (get_object_vars($this) as $property => $value) { if ($value instanceof Collection) { $result[$property] = $value->toArray(); } elseif ($value instanceof BackedEnum) { $result[$property] = $value->value; } else { $result[$property] = $value; } } return $result; } }