Skip to content

Commit d44eb06

Browse files
distantnativebastianallgeier
authored andcommitted
refact!: FieldClass with named props
1 parent c5ba308 commit d44eb06

File tree

9 files changed

+250
-206
lines changed

9 files changed

+250
-206
lines changed

src/Form/Field.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ public static function factory(
281281

282282
if (is_string($field) && class_exists($field) === true) {
283283
$attrs['siblings'] = $siblings;
284-
return new $field($attrs);
284+
unset($attrs['type']);
285+
return new $field(...$attrs);
285286
}
286287

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

src/Form/Field/BlocksField.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,27 @@ class BlocksField extends FieldClass
3232
protected string|null $group;
3333
protected mixed $value = [];
3434

35-
public function __construct(array $params = [])
36-
{
35+
public function __construct(
36+
array|string|null $empty = null,
37+
array|string|null $fieldsets = null,
38+
string|null $group = null,
39+
int|null $max = null,
40+
int|null $min = null,
41+
bool $pretty = false,
42+
...$props
43+
) {
3744
$this->setFieldsets(
38-
$params['fieldsets'] ?? null,
39-
$params['model'] ?? App::instance()->site()
45+
$fieldsets,
46+
$props['model'] ?? App::instance()->site()
4047
);
4148

42-
parent::__construct($params);
49+
parent::__construct(...$props);
4350

44-
$this->setEmpty($params['empty'] ?? null);
45-
$this->setGroup($params['group'] ?? 'blocks');
46-
$this->setMax($params['max'] ?? null);
47-
$this->setMin($params['min'] ?? null);
48-
$this->setPretty($params['pretty'] ?? false);
51+
$this->setEmpty($empty);
52+
$this->setGroup($group);
53+
$this->setMax($max);
54+
$this->setMin($min);
55+
$this->setPretty($pretty);
4956
}
5057

5158
public function blocksToValues(
@@ -134,7 +141,7 @@ public function isEmpty(): bool
134141

135142
public function group(): string
136143
{
137-
return $this->group;
144+
return $this->group ?? 'blocks';
138145
}
139146

140147
/**

src/Form/Field/EntriesField.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@ class EntriesField extends FieldClass
3434
protected Form $form;
3535
protected mixed $value = [];
3636

37-
public function __construct(array $params = [])
38-
{
39-
parent::__construct($params);
40-
41-
$this->setEmpty($params['empty'] ?? null);
42-
$this->setField($params['field'] ?? null);
43-
$this->setMax($params['max'] ?? null);
44-
$this->setMin($params['min'] ?? null);
45-
$this->setSortable($params['sortable'] ?? true);
37+
public function __construct(
38+
array|string|null $empty = null,
39+
array|string|null $field = null,
40+
int|null $max = null,
41+
int|null $min = null,
42+
bool $sortable = true,
43+
...$props
44+
) {
45+
parent::__construct(...$props);
46+
47+
$this->setEmpty($empty);
48+
$this->setField($field);
49+
$this->setMax($max);
50+
$this->setMin($min);
51+
$this->setSortable($sortable);
4652
}
4753

4854
public function field(): array

src/Form/Field/LayoutField.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Kirby\Cms\Fieldset;
88
use Kirby\Cms\Layout;
99
use Kirby\Cms\Layouts;
10+
use Kirby\Cms\ModelWithContent;
1011
use Kirby\Data\Data;
1112
use Kirby\Data\Json;
1213
use Kirby\Exception\InvalidArgumentException;
@@ -16,18 +17,21 @@
1617

1718
class LayoutField extends BlocksField
1819
{
19-
protected array|null $layouts;
20-
protected array|null $selector;
2120
protected Fieldset|null $settings;
2221

23-
public function __construct(array $params)
24-
{
25-
$this->setModel($params['model'] ?? App::instance()->site());
26-
$this->setLayouts($params['layouts'] ?? ['1/1']);
27-
$this->setSelector($params['selector'] ?? null);
28-
$this->setSettings($params['settings'] ?? null);
29-
30-
parent::__construct($params);
22+
public function __construct(
23+
protected array $layouts = ['1/1'],
24+
ModelWithContent|null $model = null,
25+
protected array|null $selector = null,
26+
array|string|null $settings = null,
27+
...$props
28+
) {
29+
$this->setModel($model ?? App::instance()->site());
30+
$this->setLayouts($layouts);
31+
$this->setSelector($selector);
32+
$this->setSettings($settings);
33+
34+
parent::__construct(...$props);
3135
}
3236

3337
/**

src/Form/Field/StatsField.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@
1717
*/
1818
class StatsField extends FieldClass
1919
{
20-
/**
21-
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
22-
*/
23-
protected array|string $reports;
24-
25-
/**
26-
* The size of the report cards. Available sizes: `tiny`, `small`, `medium`, `large`
27-
*/
28-
protected string $size;
29-
3020
/**
3121
* Cache for the Stats UI component
3222
*/
3323
protected Stats $stats;
3424

35-
public function __construct(array $params)
36-
{
37-
parent::__construct($params);
38-
39-
$this->reports = $params['reports'] ?? [];
40-
$this->size = $params['size'] ?? 'large';
25+
public function __construct(
26+
/**
27+
* Array or query string for reports.
28+
* Each report needs a `label` and `value` and can have
29+
* additional `info`, `link`, `icon` and `theme` settings.
30+
*/
31+
protected array|string $reports = [],
32+
/**
33+
* The size of the report cards.
34+
* Available sizes: `tiny`, `small`, `medium`, `large`
35+
*/
36+
protected string $size = 'large',
37+
...$props
38+
) {
39+
parent::__construct(...$props);
4140
}
4241

4342
public function hasValue(): bool
@@ -58,9 +57,9 @@ public function size(): string
5857
public function stats(): Stats
5958
{
6059
return $this->stats ??= Stats::from(
61-
model: $this->model,
60+
model: $this->model,
6261
reports: $this->reports,
63-
size: $this->size
62+
size: $this->size
6463
);
6564
}
6665

src/Form/FieldClass.php

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Kirby\Form;
44

55
use Kirby\Cms\HasSiblings;
6+
use Kirby\Cms\ModelWithContent;
7+
use Kirby\Exception\NotFoundException;
68
use Kirby\Toolkit\HasI18n;
79

810
/**
@@ -39,40 +41,54 @@ abstract class FieldClass
3941
use Mixin\When;
4042
use Mixin\Width;
4143

42-
protected Fields $siblings;
43-
4444
public function __construct(
45-
protected array $params = []
45+
array|string|null $after = null,
46+
bool $autofocus = false,
47+
array|string|null $before = null,
48+
mixed $default = null,
49+
bool $disabled = false,
50+
array|string|null $help = null,
51+
string|null $icon = null,
52+
array|string|null $label = null,
53+
ModelWithContent|null $model = null,
54+
protected string|null $name = null,
55+
array|string|null $placeholder = null,
56+
bool $required = false,
57+
protected Fields|null $siblings = null,
58+
bool $translate = true,
59+
$value = null,
60+
array|null $when = null,
61+
string|null $width = null
4662
) {
47-
$this->setAfter($params['after'] ?? null);
48-
$this->setAutofocus($params['autofocus'] ?? false);
49-
$this->setBefore($params['before'] ?? null);
50-
$this->setDefault($params['default'] ?? null);
51-
$this->setDisabled($params['disabled'] ?? false);
52-
$this->setHelp($params['help'] ?? null);
53-
$this->setIcon($params['icon'] ?? null);
54-
$this->setLabel($params['label'] ?? null);
55-
$this->setModel($params['model'] ?? null);
56-
$this->setName($params['name'] ?? null);
57-
$this->setPlaceholder($params['placeholder'] ?? null);
58-
$this->setRequired($params['required'] ?? false);
59-
$this->setSiblings($params['siblings'] ?? null);
60-
$this->setTranslate($params['translate'] ?? true);
61-
$this->setWhen($params['when'] ?? null);
62-
$this->setWidth($params['width'] ?? null);
63-
64-
if (array_key_exists('value', $params) === true) {
65-
$this->fill($params['value']);
63+
$this->setAfter($after);
64+
$this->setAutofocus($autofocus);
65+
$this->setBefore($before);
66+
$this->setDefault($default);
67+
$this->setDisabled($disabled);
68+
$this->setHelp($help);
69+
$this->setIcon($icon);
70+
$this->setLabel($label);
71+
$this->setModel($model);
72+
$this->setName($name);
73+
$this->setPlaceholder($placeholder);
74+
$this->setRequired($required);
75+
$this->setSiblings($siblings);
76+
$this->setTranslate($translate);
77+
$this->setWhen($when);
78+
$this->setWidth($width);
79+
80+
if ($value !== null) {
81+
$this->fill($value);
6682
}
6783
}
6884

6985
public function __call(string $param, array $args): mixed
7086
{
71-
if (isset($this->$param) === true) {
87+
if (property_exists($this, $param) === true) {
7288
return $this->$param;
7389
}
7490

75-
return $this->params[$param] ?? null;
91+
throw new NotFoundException(message: 'Method or option "' . $param . '" does not exist for field type "' . $this->type() . '"');
7692
}
7793

7894
/**
@@ -101,14 +117,6 @@ public function isHidden(): bool
101117
return false;
102118
}
103119

104-
/**
105-
* Returns all original params for the field
106-
*/
107-
public function params(): array
108-
{
109-
return $this->params;
110-
}
111-
112120
/**
113121
* Define the props that will be sent to
114122
* the Vue component

src/Form/Mixin/Validation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ protected function setRequired(bool $required): void
100100
$this->required = $required;
101101
}
102102

103+
/**
104+
* Runs all validations
105+
* @since 6.0.0
106+
*/
107+
public function validate(): void
108+
{
109+
$this->errors();
110+
}
111+
103112
/**
104113
* Defines all validation rules
105114
*/

tests/Form/Field/EntriesFieldTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ public static function validationsProvider(): array
363363
public function testValidations($type, $value, $expected): void
364364
{
365365
$field = $this->field('entries', [
366-
'value' => [
367-
$value
368-
],
366+
'value' => [$value],
369367
'field' => $type,
370368
'required' => true
371369
]);

0 commit comments

Comments
 (0)