Skip to content

Commit ea7205c

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

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
@@ -106,6 +106,19 @@ public function drawers(): array
106106
return [];
107107
}
108108

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

0 commit comments

Comments
 (0)