Skip to content

Commit db2ea0a

Browse files
distantnativebastianallgeier
authored andcommitted
refact!: FieldClass with named props
1 parent d76873b commit db2ea0a

File tree

10 files changed

+277
-226
lines changed

10 files changed

+277
-226
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
@@ -31,20 +31,27 @@ class BlocksField extends FieldClass
3131
protected bool $pretty;
3232
protected mixed $value = [];
3333

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

41-
parent::__construct($params);
48+
parent::__construct(...$props);
4249

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

5057
public function blocksToValues(
@@ -133,7 +140,7 @@ public function isEmpty(): bool
133140

134141
public function group(): string
135142
{
136-
return $this->group;
143+
return $this->group ?? 'blocks';
137144
}
138145

139146
public function pretty(): bool

src/Form/Field/EntriesField.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,21 @@ class EntriesField extends FieldClass
3333
protected bool $sortable = true;
3434
protected mixed $value = [];
3535

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

4753
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: 41 additions & 52 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
/**
@@ -26,6 +28,7 @@ abstract class FieldClass
2628
use Mixin\Api;
2729
use Mixin\Autofocus;
2830
use Mixin\Before;
31+
use Mixin\Disabled;
2932
use Mixin\Help;
3033
use Mixin\Icon;
3134
use Mixin\Label;
@@ -37,42 +40,54 @@ abstract class FieldClass
3740
use Mixin\When;
3841
use Mixin\Width;
3942

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

6984
public function __call(string $param, array $args): mixed
7085
{
71-
if (isset($this->$param) === true) {
86+
if (property_exists($this, $param) === true) {
7287
return $this->$param;
7388
}
7489

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

7893
/**
@@ -83,14 +98,6 @@ public function dialogs(): array
8398
return [];
8499
}
85100

86-
/**
87-
* If `true`, the field is no longer editable and will not be saved
88-
*/
89-
public function disabled(): bool
90-
{
91-
return $this->disabled;
92-
}
93-
94101
/**
95102
* Returns optional drawer routes for the field
96103
*/
@@ -104,11 +111,6 @@ public function id(): string
104111
return $this->name();
105112
}
106113

107-
public function isDisabled(): bool
108-
{
109-
return $this->disabled;
110-
}
111-
112114
public function isHidden(): bool
113115
{
114116
return false;
@@ -122,14 +124,6 @@ public function name(): string
122124
return $this->name ?? $this->type();
123125
}
124126

125-
/**
126-
* Returns all original params for the field
127-
*/
128-
public function params(): array
129-
{
130-
return $this->params;
131-
}
132-
133127
/**
134128
* Define the props that will be sent to
135129
* the Vue component
@@ -167,11 +161,6 @@ public function reset(): static
167161
return $this;
168162
}
169163

170-
protected function setDisabled(bool $disabled = false): void
171-
{
172-
$this->disabled = $disabled;
173-
}
174-
175164
protected function setName(string|null $name = null): void
176165
{
177166
$this->name = strtolower($name ?? $this->type());

src/Form/Mixin/Disabled.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Kirby\Form\Mixin;
4+
5+
trait Disabled
6+
{
7+
protected bool $disabled = false;
8+
9+
/**
10+
* If `true`, the field is no longer editable and will not be saved
11+
*/
12+
public function disabled(): bool
13+
{
14+
return $this->disabled;
15+
}
16+
17+
public function isDisabled(): bool
18+
{
19+
return $this->disabled;
20+
}
21+
22+
protected function setDisabled(bool $disabled = false): void
23+
{
24+
$this->disabled = $disabled;
25+
}
26+
}

src/Form/Mixin/Validation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ protected function setRequired(bool $required = false): void
107107
$this->required = $required;
108108
}
109109

110+
/**
111+
* Runs all validations
112+
* @since 6.0.0
113+
*/
114+
public function validate(): void
115+
{
116+
$this->errors();
117+
}
118+
110119
/**
111120
* Defines all validation rules
112121
*/

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)