Skip to content

Commit fee4af7

Browse files
distantnativebastianallgeier
authored andcommitted
feat: Add FieldClass::factory($props) helper
1 parent d44eb06 commit fee4af7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Form/Field.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,11 @@ public static function factory(
279279
): static|FieldClass {
280280
$field = static::$types[$type] ?? null;
281281

282-
if (is_string($field) && class_exists($field) === true) {
283-
$attrs['siblings'] = $siblings;
284-
unset($attrs['type']);
285-
return new $field(...$attrs);
282+
if (
283+
is_string($field) &&
284+
is_subclass_of($field, FieldClass::class) === true
285+
) {
286+
return $field::factory($attrs, $siblings);
286287
}
287288

288289
return new static($type, $attrs, $siblings);

src/Form/FieldClass.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public function drawers(): array
107107
return [];
108108
}
109109

110+
/**
111+
* Creates a new field instance from a $props array
112+
* @since 6.0.0
113+
*/
114+
public static function factory(
115+
array $props,
116+
Fields|null $siblings = null
117+
): static {
118+
$props['siblings'] = $siblings;
119+
unset($props['type']);
120+
return new static(...$props);
121+
}
122+
110123
public function id(): string
111124
{
112125
return $this->name();

0 commit comments

Comments
 (0)