Update to Kirby 4.7.0

This commit is contained in:
Paul Nicoué 2025-04-21 18:57:21 +02:00
parent 02a9ab387c
commit ba25a9a198
509 changed files with 26604 additions and 14872 deletions

View file

@ -4,6 +4,7 @@ namespace Kirby\Form;
use Closure;
use Exception;
use Kirby\Cms\App;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Component;
@ -25,42 +26,32 @@ class Field extends Component
{
/**
* An array of all found errors
*
* @var array|null
*/
protected $errors;
protected array|null $errors = null;
/**
* Parent collection with all fields of the current form
*
* @var \Kirby\Form\Fields|null
*/
protected $formFields;
protected Fields|null $formFields;
/**
* Registry for all component mixins
*
* @var array
*/
public static $mixins = [];
public static array $mixins = [];
/**
* Registry for all component types
*
* @var array
*/
public static $types = [];
public static array $types = [];
/**
* Field constructor
*
* @param string $type
* @param array $attrs
* @param \Kirby\Form\Fields|null $formFields
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function __construct(string $type, array $attrs = [], ?Fields $formFields = null)
{
public function __construct(
string $type,
array $attrs = [],
Fields|null $formFields = null
) {
if (isset(static::$types[$type]) === false) {
throw new InvalidArgumentException([
'key' => 'field.type.missing',
@ -83,10 +74,8 @@ class Field extends Component
/**
* Returns field api call
*
* @return mixed
*/
public function api()
public function api(): mixed
{
if (
isset($this->options['api']) === true &&
@ -94,15 +83,14 @@ class Field extends Component
) {
return $this->options['api']->call($this);
}
return null;
}
/**
* Returns field data
*
* @param bool $default
* @return mixed
*/
public function data(bool $default = false)
public function data(bool $default = false): mixed
{
$save = $this->options['save'] ?? true;
@ -125,8 +113,6 @@ class Field extends Component
/**
* Default props and computed of the field
*
* @return array
*/
public static function defaults(): array
{
@ -141,7 +127,7 @@ class Field extends Component
/**
* Sets the focus on this field when the form loads. Only the first field with this label gets
*/
'autofocus' => function (bool $autofocus = null): bool {
'autofocus' => function (bool|null $autofocus = null): bool {
return $autofocus ?? false;
},
/**
@ -159,7 +145,7 @@ class Field extends Component
/**
* If `true`, the field is no longer editable and will not be saved
*/
'disabled' => function (bool $disabled = null): bool {
'disabled' => function (bool|null $disabled = null): bool {
return $disabled ?? false;
},
/**
@ -171,7 +157,7 @@ class Field extends Component
/**
* Optional icon that will be shown at the end of the field
*/
'icon' => function (string $icon = null) {
'icon' => function (string|null $icon = null) {
return $icon;
},
/**
@ -189,7 +175,7 @@ class Field extends Component
/**
* If `true`, the field has to be filled in correctly to be saved.
*/
'required' => function (bool $required = null): bool {
'required' => function (bool|null $required = null): bool {
return $required ?? false;
},
/**
@ -264,15 +250,43 @@ class Field extends Component
}
/**
* Creates a new field instance
*
* @param string $type
* @param array $attrs
* @param Fields|null $formFields
* @return static
* Returns optional dialog routes for the field
*/
public static function factory(string $type, array $attrs = [], ?Fields $formFields = null)
public function dialogs(): array
{
if (
isset($this->options['dialogs']) === true &&
$this->options['dialogs'] instanceof Closure
) {
return $this->options['dialogs']->call($this);
}
return [];
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
if (
isset($this->options['drawers']) === true &&
$this->options['drawers'] instanceof Closure
) {
return $this->options['drawers']->call($this);
}
return [];
}
/**
* Creates a new field instance
*/
public static function factory(
string $type,
array $attrs = [],
Fields|null $formFields = null
): static|FieldClass {
$field = static::$types[$type] ?? null;
if (is_string($field) && class_exists($field) === true) {
@ -285,18 +299,14 @@ class Field extends Component
/**
* Parent collection with all fields of the current form
*
* @return \Kirby\Form\Fields|null
*/
public function formFields(): ?Fields
public function formFields(): Fields|null
{
return $this->formFields;
}
/**
* Validates when run for the first time and returns any errors
*
* @return array
*/
public function errors(): array
{
@ -309,29 +319,31 @@ class Field extends Component
/**
* Checks if the field is empty
*
* @param mixed ...$args
* @return bool
*/
public function isEmpty(...$args): bool
public function isEmpty(mixed ...$args): bool
{
if (count($args) === 0) {
$value = $this->value();
} else {
$value = $args[0];
}
$value = match (count($args)) {
0 => $this->value(),
default => $args[0]
};
if (isset($this->options['isEmpty']) === true) {
return $this->options['isEmpty']->call($this, $value);
if ($empty = $this->options['isEmpty'] ?? null) {
return $empty->call($this, $value);
}
return in_array($value, [null, '', []], true);
}
/**
* Checks if the field is hidden
*/
public function isHidden(): bool
{
return ($this->options['hidden'] ?? false) === true;
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
@ -340,8 +352,6 @@ class Field extends Component
/**
* Checks if the field is required
*
* @return bool
*/
public function isRequired(): bool
{
@ -350,8 +360,6 @@ class Field extends Component
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
@ -360,20 +368,16 @@ class Field extends Component
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby()
public function kirby(): App
{
return $this->model()->kirby();
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model()
public function model(): mixed
{
return $this->model;
}
@ -385,31 +389,33 @@ class Field extends Component
* - The field is required
* - The field is currently empty
* - The field is not currently inactive because of a `when` rule
*
* @return bool
*/
protected function needsValue(): bool
{
// check simple conditions first
if ($this->save() === false || $this->isRequired() === false || $this->isEmpty() === false) {
if (
$this->save() === false ||
$this->isRequired() === false ||
$this->isEmpty() === false
) {
return false;
}
// check the data of the relevant fields if there is a `when` option
if (empty($this->when) === false && is_array($this->when) === true) {
$formFields = $this->formFields();
if (
empty($this->when) === false &&
is_array($this->when) === true &&
$formFields = $this->formFields()
) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
if ($formFields !== null) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
}
}
@ -420,8 +426,6 @@ class Field extends Component
/**
* Checks if the field is saveable
*
* @return bool
*/
public function save(): bool
{
@ -430,8 +434,6 @@ class Field extends Component
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
@ -439,6 +441,7 @@ class Field extends Component
unset($array['model']);
$array['hidden'] = $this->isHidden();
$array['saveable'] = $this->save();
$array['signature'] = md5(json_encode($array));
@ -452,8 +455,6 @@ class Field extends Component
/**
* Runs the validations defined for the field
*
* @return void
*/
protected function validate(): void
{
@ -501,10 +502,8 @@ class Field extends Component
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value()
public function value(): mixed
{
return $this->save() ? $this->value : null;
}

View file

@ -5,7 +5,9 @@ namespace Kirby\Form\Field;
use Kirby\Cms\App;
use Kirby\Cms\Block;
use Kirby\Cms\Blocks as BlocksCollection;
use Kirby\Cms\Fieldset;
use Kirby\Cms\Fieldsets;
use Kirby\Cms\ModelWithContent;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Form\FieldClass;
@ -22,15 +24,17 @@ class BlocksField extends FieldClass
use Max;
use Min;
protected $blocks;
protected $fieldsets;
protected $group;
protected $pretty;
protected $value = [];
protected Fieldsets $fieldsets;
protected string|null $group;
protected bool $pretty;
protected mixed $value = [];
public function __construct(array $params = [])
{
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? App::instance()->site());
$this->setFieldsets(
$params['fieldsets'] ?? null,
$params['model'] ?? App::instance()->site()
);
parent::__construct($params);
@ -41,8 +45,10 @@ class BlocksField extends FieldClass
$this->setPretty($params['pretty'] ?? false);
}
public function blocksToValues($blocks, $to = 'values'): array
{
public function blocksToValues(
array $blocks,
string $to = 'values'
): array {
$result = [];
$fields = [];
@ -54,53 +60,58 @@ class BlocksField extends FieldClass
$fields[$type] ??= $this->fields($block['type']);
// overwrite the block content with form values
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
$block['content'] = $this->form(
$fields[$type],
$block['content']
)->$to();
$result[] = $block;
// create id if not exists
$block['id'] ??= Str::uuid();
} catch (Throwable) {
$result[] = $block;
// skip invalid blocks
continue;
} finally {
$result[] = $block;
}
}
return $result;
}
public function fields(string $type)
public function fields(string $type): array
{
return $this->fieldset($type)->fields();
}
public function fieldset(string $type)
public function fieldset(string $type): Fieldset
{
if ($fieldset = $this->fieldsets->find($type)) {
return $fieldset;
}
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
throw new NotFoundException(
'The fieldset ' . $type . ' could not be found'
);
}
public function fieldsets()
public function fieldsets(): Fieldsets
{
return $this->fieldsets;
}
public function fieldsetGroups(): array|null
{
$fieldsetGroups = $this->fieldsets()->groups();
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
$groups = $this->fieldsets()->groups();
return empty($groups) === true ? null : $groups;
}
public function fill($value = null)
public function fill(mixed $value = null): void
{
$value = BlocksCollection::parse($value);
$blocks = BlocksCollection::factory($value);
$this->value = $this->blocksToValues($blocks->toArray());
$blocks = BlocksCollection::factory($value)->toArray();
$this->value = $this->blocksToValues($blocks);
}
public function form(array $fields, array $input = [])
public function form(array $fields, array $input = []): Form
{
return new Form([
'fields' => $fields,
@ -125,6 +136,30 @@ class BlocksField extends FieldClass
return $this->pretty;
}
/**
* Paste action for blocks:
* - generates new uuids for the blocks
* - filters only supported fieldsets
* - applies max limit if defined
*/
public function pasteBlocks(array $blocks): array
{
$blocks = $this->blocksToValues($blocks);
foreach ($blocks as $index => &$block) {
$block['id'] = Str::uuid();
// remove the block if it's not available
try {
$this->fieldset($block['type']);
} catch (Throwable) {
unset($blocks[$index]);
}
}
return array_values($blocks);
}
public function props(): array
{
return [
@ -144,23 +179,25 @@ class BlocksField extends FieldClass
return [
[
'pattern' => 'uuid',
'action' => fn () => ['uuid' => Str::uuid()]
'action' => fn (): array => ['uuid' => Str::uuid()]
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () use ($field) {
'action' => function () use ($field): array {
$request = App::instance()->request();
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
return $field->blocksToValues($blocks->toArray());
return $field->pasteBlocks($blocks->toArray());
}
],
[
'pattern' => 'fieldsets/(:any)',
'method' => 'GET',
'action' => function ($fieldsetType) use ($field) {
'action' => function (
string $fieldsetType
) use ($field): array {
$fields = $field->fields($fieldsetType);
$defaults = $field->form($fields, [])->data(true);
$content = $field->form($fields, $defaults)->values();
@ -174,22 +211,33 @@ class BlocksField extends FieldClass
[
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
'action' => function (
string $fieldsetType,
string $fieldName,
string|null $path = null
) use ($field) {
$fields = $field->fields($fieldsetType);
$field = $field->form($fields)->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
'data' => array_merge(
$this->data(),
['field' => $field]
)
]);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
}
],
];
}
public function store($value)
public function store(mixed $value): mixed
{
$blocks = $this->blocksToValues((array)$value, 'content');
@ -202,7 +250,7 @@ class BlocksField extends FieldClass
return $this->valueToJson($blocks, $this->pretty());
}
protected function setDefault($default = null)
protected function setDefault(mixed $default = null): void
{
// set id for blocks if not exists
if (is_array($default) === true) {
@ -214,23 +262,26 @@ class BlocksField extends FieldClass
parent::setDefault($default);
}
protected function setFieldsets($fieldsets, $model)
{
protected function setFieldsets(
string|array|null $fieldsets,
ModelWithContent $model
): void {
if (is_string($fieldsets) === true) {
$fieldsets = [];
}
$this->fieldsets = Fieldsets::factory($fieldsets, [
'parent' => $model
]);
$this->fieldsets = Fieldsets::factory(
$fieldsets,
['parent' => $model]
);
}
protected function setGroup(string $group = null)
protected function setGroup(string|null $group = null): void
{
$this->group = $group;
}
protected function setPretty(bool $pretty = false)
protected function setPretty(bool $pretty = false): void
{
$this->pretty = $pretty;
}
@ -262,21 +313,22 @@ class BlocksField extends FieldClass
foreach ($value as $block) {
$index++;
$blockType = $block['type'];
$type = $block['type'];
try {
$fieldset = $this->fieldset($blockType);
$blockFields = $fields[$blockType] ?? $fieldset->fields() ?? [];
$fieldset = $this->fieldset($type);
$blockFields = $fields[$type] ?? $fieldset->fields() ?? [];
} catch (Throwable) {
// skip invalid blocks
continue;
}
// store the fields for the next round
$fields[$blockType] = $blockFields;
$fields[$type] = $blockFields;
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$form = $this->form($blockFields, $block['content']);
foreach ($form->fields() as $field) {
$errors = $field->errors();
// rough first validation

View file

@ -14,19 +14,21 @@ use Throwable;
class LayoutField extends BlocksField
{
protected $layouts;
protected $settings;
protected array|null $layouts;
protected array|null $selector;
protected Fieldset|null $settings;
public function __construct(array $params)
{
$this->setModel($params['model'] ?? App::instance()->site());
$this->setLayouts($params['layouts'] ?? ['1/1']);
$this->setSelector($params['selector'] ?? null);
$this->setSettings($params['settings'] ?? null);
parent::__construct($params);
}
public function fill($value = null)
public function fill(mixed $value = null): void
{
$value = $this->valueFromJson($value);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
@ -44,7 +46,7 @@ class LayoutField extends BlocksField
$this->value = $layouts;
}
public function attrsForm(array $input = [])
public function attrsForm(array $input = []): Form
{
$settings = $this->settings();
@ -61,13 +63,71 @@ class LayoutField extends BlocksField
return $this->layouts;
}
/**
* Creates form values for each layout
*/
public function layoutsToValues(array $layouts): array
{
foreach ($layouts as &$layout) {
$layout['id'] ??= Str::uuid();
$layout['columns'] ??= [];
array_walk($layout['columns'], function (&$column) {
$column['id'] ??= Str::uuid();
$column['blocks'] = $this->blocksToValues($column['blocks'] ?? []);
});
}
return $layouts;
}
/**
* Paste action for layouts:
* - generates new uuids for layout, column and blocks
* - filters only supported layouts
* - filters only supported fieldsets
*/
public function pasteLayouts(array $layouts): array
{
$layouts = $this->layoutsToValues($layouts);
foreach ($layouts as $layoutIndex => &$layout) {
$layout['id'] = Str::uuid();
// remove the row if layout not available for the pasted layout field
$columns = array_column($layout['columns'], 'width');
if (in_array($columns, $this->layouts()) === false) {
unset($layouts[$layoutIndex]);
continue;
}
array_walk($layout['columns'], function (&$column) {
$column['id'] = Str::uuid();
array_walk($column['blocks'], function (&$block, $index) use ($column) {
$block['id'] = Str::uuid();
// remove the block if it's not available
try {
$this->fieldset($block['type']);
} catch (Throwable) {
unset($column['blocks'][$index]);
}
});
});
}
return $layouts;
}
public function props(): array
{
$settings = $this->settings();
return array_merge(parent::props(), [
'settings' => $settings?->toArray(),
'layouts' => $this->layouts()
'layouts' => $this->layouts(),
'selector' => $this->selector(),
'settings' => $settings?->toArray()
]);
}
@ -75,13 +135,15 @@ class LayoutField extends BlocksField
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field) {
'action' => function () use ($field): array {
$request = App::instance()->request();
$defaults = $field->attrsForm([])->data(true);
$input = $request->get('attrs') ?? [];
$defaults = $field->attrsForm($input)->data(true);
$attrs = $field->attrsForm($defaults)->values();
$columns = $request->get('columns') ?? ['1/1'];
@ -96,26 +158,53 @@ class LayoutField extends BlocksField
},
];
$routes[] = [
'pattern' => 'layout/paste',
'method' => 'POST',
'action' => function () use ($field): array {
$request = App::instance()->request();
$value = Layouts::parse($request->get('json'));
$layouts = Layouts::factory($value);
return $field->pasteLayouts($layouts->toArray());
}
];
$routes[] = [
'pattern' => 'fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $fieldName, string $path = null) use ($field) {
'action' => function (
string $fieldName,
string|null $path = null
) use ($field): array {
$form = $field->attrsForm();
$field = $form->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => array_merge($this->data(), ['field' => $field])
'data' => array_merge(
$this->data(),
['field' => $field]
)
]);
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
}
];
return $routes;
}
protected function setDefault($default = null)
public function selector(): array|null
{
return $this->selector;
}
protected function setDefault(mixed $default = null): void
{
// set id for layouts, columns and blocks within layout if not exists
if (is_array($default) === true) {
@ -141,7 +230,7 @@ class LayoutField extends BlocksField
parent::setDefault($default);
}
protected function setLayouts(array $layouts = [])
protected function setLayouts(array $layouts = []): void
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
@ -149,7 +238,15 @@ class LayoutField extends BlocksField
);
}
protected function setSettings($settings = null)
/**
* Layout selector's styles such as size (`small`, `medium`, `large` or `huge`) and columns
*/
protected function setSelector(array|null $selector = null): void
{
$this->selector = $selector;
}
protected function setSettings(array|string|null $settings = null): void
{
if (empty($settings) === true) {
$this->settings = null;
@ -165,12 +262,12 @@ class LayoutField extends BlocksField
$this->settings = Fieldset::factory($settings);
}
public function settings()
public function settings(): Fieldset|null
{
return $this->settings;
}
public function store($value)
public function store(mixed $value): mixed
{
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
@ -204,7 +301,9 @@ class LayoutField extends BlocksField
$layoutIndex++;
// validate settings form
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
$form = $this->attrsForm($layout['attrs'] ?? []);
foreach ($form->fields() as $field) {
$errors = $field->errors();
if (empty($errors) === false) {
@ -237,7 +336,9 @@ class LayoutField extends BlocksField
$fields[$blockType] = $blockFields;
// overwrite the content with the serialized form
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
$form = $this->form($blockFields, $block['content']);
foreach ($form->fields() as $field) {
$errors = $field->errors();
// rough first validation

View file

@ -27,117 +27,27 @@ abstract class FieldClass
{
use HasSiblings;
/**
* @var string|null
*/
protected $after;
/**
* @var bool
*/
protected $autofocus;
/**
* @var string|null
*/
protected $before;
/**
* @var mixed
*/
protected $default;
/**
* @var bool
*/
protected $disabled;
/**
* @var string|null
*/
protected $help;
/**
* @var string|null
*/
protected $icon;
/**
* @var string|null
*/
protected $label;
/**
* @var \Kirby\Cms\ModelWithContent
*/
protected $model;
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $params;
/**
* @var string|null
*/
protected $placeholder;
/**
* @var bool
*/
protected $required;
/**
* @var \Kirby\Form\Fields
*/
protected $siblings;
/**
* @var bool
*/
protected $translate;
/**
* @var mixed
*/
protected $value;
/**
* @var array|null
*/
protected $when;
/**
* @var string|null
*/
protected $width;
/**
* @param string $param
* @param array $args
* @return mixed
*/
public function __call(string $param, array $args)
{
if (isset($this->$param) === true) {
return $this->$param;
}
return $this->params[$param] ?? null;
}
/**
* @param array $params
*/
public function __construct(array $params = [])
{
$this->params = $params;
protected string|null $after;
protected bool $autofocus;
protected string|null $before;
protected mixed $default;
protected bool $disabled;
protected string|null $help;
protected string|null $icon;
protected string|null $label;
protected ModelWithContent $model;
protected string|null $name;
protected string|null $placeholder;
protected bool $required;
protected Fields $siblings;
protected bool $translate;
protected mixed $value = null;
protected array|null $when;
protected string|null $width;
public function __construct(
protected array $params = []
) {
$this->setAfter($params['after'] ?? null);
$this->setAutofocus($params['autofocus'] ?? false);
$this->setBefore($params['before'] ?? null);
@ -160,33 +70,30 @@ abstract class FieldClass
}
}
/**
* @return string|null
*/
public function __call(string $param, array $args): mixed
{
if (isset($this->$param) === true) {
return $this->$param;
}
return $this->params[$param] ?? null;
}
public function after(): string|null
{
return $this->stringTemplate($this->after);
}
/**
* @return array
*/
public function api(): array
{
return $this->routes();
}
/**
* @return bool
*/
public function autofocus(): bool
{
return $this->autofocus;
}
/**
* @return string|null
*/
public function before(): string|null
{
return $this->stringTemplate($this->before);
@ -199,11 +106,8 @@ abstract class FieldClass
* Returns the field data
* in a format to be stored
* in Kirby's content fields
*
* @param bool $default
* @return mixed
*/
public function data(bool $default = false)
public function data(bool $default = false): mixed
{
return $this->store($this->value($default));
}
@ -211,10 +115,8 @@ abstract class FieldClass
/**
* Returns the default value for the field,
* which will be used when a page/file/user is created
*
* @return mixed
*/
public function default()
public function default(): mixed
{
if (is_string($this->default) === false) {
return $this->default;
@ -223,21 +125,33 @@ abstract class FieldClass
return $this->stringTemplate($this->default);
}
/**
* Returns optional dialog routes for the field
*/
public function dialogs(): array
{
return [];
}
/**
* If `true`, the field is no longer editable and will not be saved
*
* @return bool
*/
public function disabled(): bool
{
return $this->disabled;
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
return [];
}
/**
* Runs all validations and returns an array of
* error messages
*
* @return array
*/
public function errors(): array
{
@ -246,19 +160,14 @@ abstract class FieldClass
/**
* Setter for the value
*
* @param mixed $value
* @return void
*/
public function fill($value = null)
public function fill(mixed $value = null): void
{
$this->value = $value;
}
/**
* Optional help text below the field
*
* @return string|null
*/
public function help(): string|null
{
@ -271,79 +180,57 @@ abstract class FieldClass
return null;
}
/**
* @param string|array|null $param
* @return string|null
*/
protected function i18n($param = null): string|null
protected function i18n(string|array|null $param = null): string|null
{
return empty($param) === false ? I18n::translate($param, $param) : null;
}
/**
* Optional icon that will be shown at the end of the field
*
* @return string|null
*/
public function icon(): string|null
{
return $this->icon;
}
/**
* @return string
*/
public function id(): string
{
return $this->name();
}
/**
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->isEmptyValue($this->value());
}
/**
* @param mixed $value
* @return bool
*/
public function isEmptyValue($value = null): bool
public function isEmptyValue(mixed $value = null): bool
{
return in_array($value, [null, '', []], true);
}
public function isHidden(): bool
{
return false;
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return $this->isValid() === false;
}
/**
* @return bool
*/
public function isRequired(): bool
{
return $this->required;
}
/**
* @return bool
*/
public function isSaveable(): bool
{
return true;
@ -351,8 +238,6 @@ abstract class FieldClass
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
@ -361,38 +246,32 @@ abstract class FieldClass
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby()
public function kirby(): App
{
return $this->model->kirby();
}
/**
* The field label can be set as string or associative array with translations
*
* @return string
*/
public function label(): string
{
return $this->stringTemplate($this->label ?? Str::ucfirst($this->name()));
return $this->stringTemplate(
$this->label ?? Str::ucfirst($this->name())
);
}
/**
* Returns the parent model
*
* @return mixed
*/
public function model()
public function model(): ModelWithContent
{
return $this->model;
}
/**
* Returns the field name
*
* @return string
*/
public function name(): string
{
@ -406,8 +285,6 @@ abstract class FieldClass
* - The field is required
* - The field is currently empty
* - The field is not currently inactive because of a `when` rule
*
* @return bool
*/
protected function needsValue(): bool
{
@ -421,20 +298,20 @@ abstract class FieldClass
}
// check the data of the relevant fields if there is a `when` option
if (empty($this->when) === false && is_array($this->when) === true) {
$formFields = $this->siblings();
if (
empty($this->when) === false &&
is_array($this->when) === true &&
$formFields = $this->siblings()
) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
if ($formFields !== null) {
foreach ($this->when as $field => $value) {
$field = $formFields->get($field);
$inputValue = $field?->value() ?? '';
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
// if the input data doesn't match the requested `when` value,
// that means that this field is not required and can be saved
// (*all* `when` conditions must be met for this field to be required)
if ($inputValue !== $value) {
return false;
}
}
}
@ -445,8 +322,6 @@ abstract class FieldClass
/**
* Returns all original params for the field
*
* @return array
*/
public function params(): array
{
@ -455,8 +330,6 @@ abstract class FieldClass
/**
* Optional placeholder value that will be shown when the field is empty
*
* @return string|null
*/
public function placeholder(): string|null
{
@ -466,8 +339,6 @@ abstract class FieldClass
/**
* Define the props that will be sent to
* the Vue component
*
* @return array
*/
public function props(): array
{
@ -478,6 +349,7 @@ abstract class FieldClass
'default' => $this->default(),
'disabled' => $this->isDisabled(),
'help' => $this->help(),
'hidden' => $this->isHidden(),
'icon' => $this->icon(),
'label' => $this->label(),
'name' => $this->name(),
@ -493,8 +365,6 @@ abstract class FieldClass
/**
* If `true`, the field has to be filled in correctly to be saved.
*
* @return bool
*/
public function required(): bool
{
@ -503,8 +373,6 @@ abstract class FieldClass
/**
* Routes for the field API
*
* @return array
*/
public function routes(): array
{
@ -514,176 +382,108 @@ abstract class FieldClass
/**
* @deprecated 3.5.0
* @todo remove when the general field class setup has been refactored
* @return bool
*/
public function save()
public function save(): bool
{
return $this->isSaveable();
}
/**
* @param array|string|null $after
* @return void
*/
protected function setAfter($after = null)
protected function setAfter(array|string|null $after = null): void
{
$this->after = $this->i18n($after);
}
/**
* @param bool $autofocus
* @return void
*/
protected function setAutofocus(bool $autofocus = false)
protected function setAutofocus(bool $autofocus = false): void
{
$this->autofocus = $autofocus;
}
/**
* @param array|string|null $before
* @return void
*/
protected function setBefore($before = null)
protected function setBefore(array|string|null $before = null): void
{
$this->before = $this->i18n($before);
}
/**
* @param mixed $default
* @return void
*/
protected function setDefault($default = null)
protected function setDefault(mixed $default = null): void
{
$this->default = $default;
}
/**
* @param bool $disabled
* @return void
*/
protected function setDisabled(bool $disabled = false)
protected function setDisabled(bool $disabled = false): void
{
$this->disabled = $disabled;
}
/**
* @param array|string|null $help
* @return void
*/
protected function setHelp($help = null)
protected function setHelp(array|string|null $help = null): void
{
$this->help = $this->i18n($help);
}
/**
* @param string|null $icon
* @return void
*/
protected function setIcon(string|null $icon = null)
protected function setIcon(string|null $icon = null): void
{
$this->icon = $icon;
}
/**
* @param array|string|null $label
* @return void
*/
protected function setLabel($label = null)
protected function setLabel(array|string|null $label = null): void
{
$this->label = $this->i18n($label);
}
/**
* @param \Kirby\Cms\ModelWithContent $model
* @return void
*/
protected function setModel(ModelWithContent $model)
protected function setModel(ModelWithContent $model): void
{
$this->model = $model;
}
/**
* @param string|null $name
* @return void
*/
protected function setName(string $name = null)
protected function setName(string|null $name = null): void
{
$this->name = $name;
}
/**
* @param array|string|null $placeholder
* @return void
*/
protected function setPlaceholder($placeholder = null)
protected function setPlaceholder(array|string|null $placeholder = null): void
{
$this->placeholder = $this->i18n($placeholder);
}
/**
* @param bool $required
* @return void
*/
protected function setRequired(bool $required = false)
protected function setRequired(bool $required = false): void
{
$this->required = $required;
}
/**
* @param \Kirby\Form\Fields|null $siblings
* @return void
*/
protected function setSiblings(?Fields $siblings = null)
protected function setSiblings(Fields|null $siblings = null): void
{
$this->siblings = $siblings ?? new Fields([$this]);
}
/**
* @param bool $translate
* @return void
*/
protected function setTranslate(bool $translate = true)
protected function setTranslate(bool $translate = true): void
{
$this->translate = $translate;
}
/**
* Setter for the when condition
*
* @param mixed $when
* @return void
*/
protected function setWhen($when = null)
protected function setWhen(array|null $when = null): void
{
$this->when = $when;
}
/**
* Setter for the field width
*
* @param string|null $width
* @return void
*/
protected function setWidth(string $width = null)
protected function setWidth(string|null $width = null): void
{
$this->width = $width;
}
/**
* Returns all sibling fields
*
* @return \Kirby\Form\Fields
*/
protected function siblingsCollection()
protected function siblingsCollection(): Fields
{
return $this->siblings;
}
/**
* Parses a string template in the given value
*
* @param string|null $string
* @return string|null
*/
protected function stringTemplate(string|null $string = null): string|null
{
@ -697,19 +497,14 @@ abstract class FieldClass
/**
* Converts the given value to a value
* that can be stored in the text file
*
* @param mixed $value
* @return mixed
*/
public function store($value)
public function store(mixed $value): mixed
{
return $value;
}
/**
* Should the field be translatable?
*
* @return bool
*/
public function translate(): bool
{
@ -718,8 +513,6 @@ abstract class FieldClass
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
@ -733,8 +526,6 @@ abstract class FieldClass
/**
* Returns the field type
*
* @return string
*/
public function type(): string
{
@ -743,8 +534,6 @@ abstract class FieldClass
/**
* Runs the validations defined for the field
*
* @return array
*/
protected function validate(): array
{
@ -782,8 +571,7 @@ abstract class FieldClass
/**
* Defines all validation rules
*
* @return array
* @codeCoverageIgnore
*/
protected function validations(): array
{
@ -793,10 +581,8 @@ abstract class FieldClass
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value(bool $default = false)
public function value(bool $default = false): mixed
{
if ($this->isSaveable() === false) {
return null;
@ -809,11 +595,7 @@ abstract class FieldClass
return $this->value;
}
/**
* @param mixed $value
* @return array
*/
protected function valueFromJson($value): array
protected function valueFromJson(mixed $value): array
{
try {
return Data::decode($value, 'json');
@ -822,22 +604,15 @@ abstract class FieldClass
}
}
/**
* @param mixed $value
* @return array
*/
protected function valueFromYaml($value): array
protected function valueFromYaml(mixed $value): array
{
return Data::decode($value, 'yaml');
}
/**
* @param array|null $value
* @param bool $pretty
* @return string
*/
protected function valueToJson(array $value = null, bool $pretty = false): string
{
protected function valueToJson(
array|null $value = null,
bool $pretty = false
): string {
$constants = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
if ($pretty === true) {
@ -847,19 +622,13 @@ abstract class FieldClass
return json_encode($value, $constants);
}
/**
* @param array|null $value
* @return string
*/
protected function valueToYaml(array $value = null): string
protected function valueToYaml(array|null $value = null): string
{
return Data::encode($value, 'yaml');
}
/**
* Conditions when the field will be shown
*
* @return array|null
*/
public function when(): array|null
{
@ -869,8 +638,6 @@ abstract class FieldClass
/**
* Returns the width of the field in
* the Panel grid
*
* @return string
*/
public function width(): string
{

View file

@ -21,9 +21,7 @@ class Fields extends Collection
* This takes care of validation and of setting
* the collection prop on each object correctly.
*
* @param string $name
* @param object|array $field
* @return void
*/
public function __set(string $name, $field): void
{
@ -40,11 +38,8 @@ class Fields extends Collection
* Converts the fields collection to an
* array and also does that for every
* included field.
*
* @param \Closure|null $map
* @return array
*/
public function toArray(Closure $map = null): array
public function toArray(Closure|null $map = null): array
{
$array = [];

View file

@ -4,9 +4,11 @@ namespace Kirby\Form;
use Closure;
use Kirby\Cms\App;
use Kirby\Cms\Model;
use Kirby\Cms\File;
use Kirby\Cms\ModelWithContent;
use Kirby\Data\Data;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use Throwable;
@ -26,29 +28,21 @@ class Form
{
/**
* An array of all found errors
*
* @var array|null
*/
protected $errors;
protected array|null $errors = null;
/**
* Fields in the form
*
* @var \Kirby\Form\Fields|null
*/
protected $fields;
protected Fields|null $fields;
/**
* All values of form
*
* @var array
*/
protected $values = [];
protected array $values = [];
/**
* Form constructor
*
* @param array $props
*/
public function __construct(array $props)
{
@ -80,15 +74,12 @@ class Form
// inject the name
$props['name'] = $name = strtolower($name);
// check if the field is disabled
$disabled = $props['disabled'] ?? false;
// check if the field is disabled and
// overwrite the field value if not set
if ($disabled === true) {
$props['value'] = $values[$name] ?? null;
} else {
$props['value'] = $input[$name] ?? $values[$name] ?? null;
}
$props['value'] = match ($props['disabled'] ?? false) {
true => $values[$name] ?? null,
default => $input[$name] ?? $values[$name] ?? null
};
try {
$field = Field::factory($props['type'], $props, $this->fields);
@ -117,8 +108,6 @@ class Form
/**
* Returns the data required to write to the content file
* Doesn't include default and null values
*
* @return array
*/
public function content(): array
{
@ -129,8 +118,6 @@ class Form
* Returns data for all fields in the form
*
* @param false $defaults
* @param bool $includeNulls
* @return array
*/
public function data($defaults = false, bool $includeNulls = true): array
{
@ -153,8 +140,6 @@ class Form
/**
* An array of all found errors
*
* @return array
*/
public function errors(): array
{
@ -178,17 +163,16 @@ class Form
/**
* Shows the error with the field
*
* @param \Throwable $exception
* @param array $props
* @return \Kirby\Form\Field
*/
public static function exceptionField(Throwable $exception, array $props = [])
{
public static function exceptionField(
Throwable $exception,
array $props = []
): Field {
$message = $exception->getMessage();
if (App::instance()->option('debug') === true) {
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
$message .= ' in file: ' . $exception->getFile();
$message .= ' line: ' . $exception->getLine();
}
$props = array_merge($props, [
@ -204,11 +188,9 @@ class Form
* Get the field object by name
* and handle nested fields correctly
*
* @param string $name
* @throws \Kirby\Exception\NotFoundException
* @return \Kirby\Form\Field
*/
public function field(string $name)
public function field(string $name): Field|FieldClass
{
$form = $this;
$fieldNames = Str::split($name, '+');
@ -223,9 +205,11 @@ class Form
if ($count !== $index) {
$form = $field->form();
}
} else {
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
continue;
}
throw new NotFoundException('The field "' . $fieldName . '" could not be found');
}
// it can get this error only if $name is an empty string as $name = ''
@ -238,21 +222,16 @@ class Form
/**
* Returns form fields
*
* @return \Kirby\Form\Fields|null
*/
public function fields()
public function fields(): Fields|null
{
return $this->fields;
}
/**
* @param \Kirby\Cms\Model $model
* @param array $props
* @return static
*/
public static function for(Model $model, array $props = [])
{
public static function for(
ModelWithContent $model,
array $props = []
): static {
// get the original model data
$original = $model->content($props['language'] ?? null)->toArray();
$values = $props['values'] ?? [];
@ -270,7 +249,10 @@ class Form
$props['model'] = $model;
// search for the blueprint
if (method_exists($model, 'blueprint') === true && $blueprint = $model->blueprint()) {
if (
method_exists($model, 'blueprint') === true &&
$blueprint = $model->blueprint()
) {
$props['fields'] = $blueprint->fields();
}
@ -289,18 +271,14 @@ class Form
/**
* Checks if the form is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return empty($this->errors()) === false;
return $this->isValid() === false;
}
/**
* Checks if the form is valid
*
* @return bool
*/
public function isValid(): bool
{
@ -310,17 +288,15 @@ class Form
/**
* Disables fields in secondary languages when
* they are configured to be untranslatable
*
* @param array $fields
* @param string|null $language
* @return array
*/
protected static function prepareFieldsForLanguage(array $fields, string|null $language = null): array
{
protected static function prepareFieldsForLanguage(
array $fields,
string|null $language = null
): array {
$kirby = App::instance(null, true);
// only modify the fields if we have a valid Kirby multilang instance
if (!$kirby || $kirby->multilang() === false) {
if ($kirby?->multilang() !== true) {
return $fields;
}
@ -343,29 +319,20 @@ class Form
* Converts the data of fields to strings
*
* @param false $defaults
* @return array
*/
public function strings($defaults = false): array
{
$strings = [];
foreach ($this->data($defaults) as $key => $value) {
if ($value === null) {
$strings[$key] = null;
} elseif (is_array($value) === true) {
$strings[$key] = Data::encode($value, 'yaml');
} else {
$strings[$key] = $value;
return A::map(
$this->data($defaults),
fn ($value) => match (true) {
is_array($value) => Data::encode($value, 'yaml'),
default => $value
}
}
return $strings;
);
}
/**
* Converts the form to a plain array
*
* @return array
*/
public function toArray(): array
{
@ -380,8 +347,6 @@ class Form
/**
* Returns form values
*
* @return array
*/
public function values(): array
{

View file

@ -4,9 +4,9 @@ namespace Kirby\Form\Mixin;
trait EmptyState
{
protected $empty;
protected string|null $empty;
protected function setEmpty($empty = null)
protected function setEmpty(string|array|null $empty = null): void
{
$this->empty = $this->i18n($empty);
}

View file

@ -4,14 +4,14 @@ namespace Kirby\Form\Mixin;
trait Max
{
protected $max;
protected int|null $max;
public function max(): int|null
{
return $this->max;
}
protected function setMax(int $max = null)
protected function setMax(int|null $max = null)
{
$this->max = $max;
}

View file

@ -4,14 +4,14 @@ namespace Kirby\Form\Mixin;
trait Min
{
protected $min;
protected int|null $min;
public function min(): int|null
{
return $this->min;
}
protected function setMin(int $min = null)
protected function setMin(int|null $min = null)
{
$this->min = $min;
}

View file

@ -1,205 +0,0 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\App;
use Kirby\Toolkit\I18n;
/**
* Foundation for the Options query
* classes, that are used to generate
* options arrays for select fields,
* radio boxes, checkboxes and more.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @deprecated 3.8.0 Use `Kirby\Option\Options` instead
*/
class Options
{
/**
* Returns the classes of predefined Kirby objects
*
* @return array
*/
protected static function aliases(): array
{
return [
'Kirby\Cms\File' => 'file',
'Kirby\Toolkit\Obj' => 'arrayItem',
'Kirby\Cms\Block' => 'block',
'Kirby\Cms\Page' => 'page',
'Kirby\Cms\StructureObject' => 'structureItem',
'Kirby\Cms\User' => 'user',
];
}
/**
* Brings options through api
*
* @param $api
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function api($api, $model = null): array
{
$model ??= App::instance()->site();
$fetch = null;
$text = null;
$value = null;
if (is_array($api) === true) {
$fetch = $api['fetch'] ?? null;
$text = $api['text'] ?? null;
$value = $api['value'] ?? null;
$url = $api['url'] ?? null;
} else {
$url = $api;
}
$optionsApi = new OptionsApi([
'data' => static::data($model),
'fetch' => $fetch,
'url' => $url,
'text' => $text,
'value' => $value
]);
return $optionsApi->options();
}
/**
* @param \Kirby\Cms\Model $model
* @return array
*/
protected static function data($model): array
{
$kirby = $model->kirby();
// default data setup
$data = [
'kirby' => $kirby,
'site' => $kirby->site(),
'users' => $kirby->users(),
];
// add the model by the proper alias
foreach (static::aliases() as $className => $alias) {
if ($model instanceof $className) {
$data[$alias] = $model;
}
}
return $data;
}
/**
* Brings options by supporting both api and query
*
* @param $options
* @param array $props
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function factory($options, array $props = [], $model = null): array
{
$options = match ($options) {
'api' => static::api($props['api'], $model),
'query' => static::query($props['query'], $model),
'pages' => static::query('site.index', $model),
'children',
'grandChildren',
'siblings',
'index',
'files',
'images',
'documents',
'videos',
'audio',
'code',
'archives' => static::query('page.' . $options, $model),
default => $options
};
if (is_array($options) === false) {
return [];
}
$result = [];
foreach ($options as $key => $option) {
if (is_array($option) === false || isset($option['value']) === false) {
$option = [
'value' => is_int($key) ? $option : $key,
'text' => $option
];
}
// fallback for the text
$option['text'] ??= $option['value'];
// translate the option text
if (is_array($option['text']) === true) {
$option['text'] = I18n::translate($option['text'], $option['text']);
}
// add the option to the list
$result[] = $option;
}
return $result;
}
/**
* Brings options with query
*
* @param $query
* @param \Kirby\Cms\Model|null $model
* @return array
*/
public static function query($query, $model = null): array
{
$model ??= App::instance()->site();
// default text setup
$text = [
'arrayItem' => '{{ arrayItem.value }}',
'block' => '{{ block.type }}: {{ block.id }}',
'file' => '{{ file.filename }}',
'page' => '{{ page.title }}',
'structureItem' => '{{ structureItem.title }}',
'user' => '{{ user.username }}',
];
// default value setup
$value = [
'arrayItem' => '{{ arrayItem.value }}',
'block' => '{{ block.id }}',
'file' => '{{ file.id }}',
'page' => '{{ page.id }}',
'structureItem' => '{{ structureItem.id }}',
'user' => '{{ user.email }}',
];
// resolve array query setup
if (is_array($query) === true) {
$text = $query['text'] ?? $text;
$value = $query['value'] ?? $value;
$query = $query['fetch'] ?? null;
}
$optionsQuery = new OptionsQuery([
'aliases' => static::aliases(),
'data' => static::data($model),
'query' => $query,
'text' => $text,
'value' => $value
]);
return $optionsQuery->options();
}
}

View file

@ -1,244 +0,0 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\Nest;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Http\Remote;
use Kirby\Http\Url;
use Kirby\Query\Query;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Str;
/**
* The OptionsApi class handles fetching options
* from any REST API with valid JSON data.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @deprecated 3.8.0 Use `Kirby\Option\OptionsApi` instead
*/
class OptionsApi
{
use Properties;
/**
* @var array
*/
protected $data;
/**
* @var string|null
*/
protected $fetch;
/**
* @var array|string|null
*/
protected $options;
/**
* @var string
*/
protected $text = '{{ item.value }}';
/**
* @var string
*/
protected $url;
/**
* @var string
*/
protected $value = '{{ item.key }}';
/**
* OptionsApi constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setProperties($props);
}
/**
* @return array
*/
public function data(): array
{
return $this->data;
}
/**
* @return mixed
*/
public function fetch()
{
return $this->fetch;
}
/**
* @param string $field
* @param array $data
* @return string
*/
protected function field(string $field, array $data): string
{
$value = $this->$field();
return Str::safeTemplate($value, $data);
}
/**
* @return array
* @throws \Exception
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function options(): array
{
if (is_array($this->options) === true) {
return $this->options;
}
if (Url::isAbsolute($this->url()) === true) {
// URL, request via cURL
$data = Remote::get($this->url())->json();
} else {
// local file, get contents locally
// ensure the file exists before trying to load it as the
// file_get_contents() warnings need to be suppressed
if (is_file($this->url()) !== true) {
throw new Exception('Local file ' . $this->url() . ' was not found');
}
$content = @file_get_contents($this->url());
if (is_string($content) !== true) {
throw new Exception('Unexpected read error'); // @codeCoverageIgnore
}
if (empty($content) === true) {
return [];
}
$data = json_decode($content, true);
}
if (is_array($data) === false) {
throw new InvalidArgumentException('Invalid options format');
}
$result = (new Query($this->fetch()))->resolve(Nest::create($data));
$options = [];
foreach ($result as $item) {
$data = array_merge($this->data(), ['item' => $item]);
$options[] = [
'text' => $this->field('text', $data),
'value' => $this->field('value', $data),
];
}
return $options;
}
/**
* @param array $data
* @return $this
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param string|null $fetch
* @return $this
*/
protected function setFetch(string|null $fetch = null)
{
$this->fetch = $fetch;
return $this;
}
/**
* @param array|string|null $options
* @return $this
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param string $text
* @return $this
*/
protected function setText(string|null $text = null)
{
$this->text = $text;
return $this;
}
/**
* @param string $url
* @return $this
*/
protected function setUrl(string $url)
{
$this->url = $url;
return $this;
}
/**
* @param string|null $value
* @return $this
*/
protected function setValue(string|null $value = null)
{
$this->value = $value;
return $this;
}
/**
* @return string
*/
public function text(): string
{
return $this->text;
}
/**
* @return array
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function toArray(): array
{
return $this->options();
}
/**
* @return string
*/
public function url(): string
{
return Str::template($this->url, $this->data());
}
/**
* @return string
*/
public function value(): string
{
return $this->value;
}
}

View file

@ -1,273 +0,0 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\Field;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Query\Query;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use Kirby\Toolkit\Properties;
use Kirby\Toolkit\Str;
/**
* Option Queries are run against any set
* of data. In case of Kirby, you can query
* pages, files, users or structures to create
* options out of them.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @deprecated 3.8.0 Use `Kirby\Option\OptionsQuery` instead
*/
class OptionsQuery
{
use Properties;
/**
* @var array
*/
protected $aliases = [];
/**
* @var array
*/
protected $data;
/**
* @var array|string|null
*/
protected $options;
/**
* @var string
*/
protected $query;
/**
* @var mixed
*/
protected $text;
/**
* @var mixed
*/
protected $value;
/**
* OptionsQuery constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setProperties($props);
}
/**
* @return array
*/
public function aliases(): array
{
return $this->aliases;
}
/**
* @return array
*/
public function data(): array
{
return $this->data;
}
/**
* @param string $object
* @param string $field
* @param array $data
* @return string
* @throws \Kirby\Exception\NotFoundException
*/
protected function template(string $object, string $field, array $data)
{
$value = $this->$field();
if (is_array($value) === true) {
if (isset($value[$object]) === false) {
throw new NotFoundException('Missing "' . $field . '" definition');
}
$value = $value[$object];
}
return Str::safeTemplate($value, $data);
}
/**
* @return array
*/
public function options(): array
{
if (is_array($this->options) === true) {
return $this->options;
}
$data = $this->data();
$query = new Query($this->query());
$result = $query->resolve($data);
$result = $this->resultToCollection($result);
$options = [];
foreach ($result as $item) {
$alias = $this->resolve($item);
$data = array_merge($data, [$alias => $item]);
$options[] = [
'text' => $this->template($alias, 'text', $data),
'value' => $this->template($alias, 'value', $data)
];
}
return $this->options = $options;
}
/**
* @return string
*/
public function query(): string
{
return $this->query;
}
/**
* @param $object
* @return mixed|string|null
*/
public function resolve($object)
{
// fast access
if ($alias = ($this->aliases[get_class($object)] ?? null)) {
return $alias;
}
// slow but precise resolving
foreach ($this->aliases as $className => $alias) {
if ($object instanceof $className) {
return $alias;
}
}
return 'item';
}
/**
* @param $result
* @throws \Kirby\Exception\InvalidArgumentException
*/
protected function resultToCollection($result)
{
if (is_array($result)) {
foreach ($result as $key => $item) {
if (is_scalar($item) === true) {
$result[$key] = new Obj([
'key' => new Field(null, 'key', $key),
'value' => new Field(null, 'value', $item),
]);
}
}
$result = new Collection($result);
}
if ($result instanceof Collection === false) {
throw new InvalidArgumentException('Invalid query result data');
}
return $result;
}
/**
* @param array|null $aliases
* @return $this
*/
protected function setAliases(array|null $aliases = null)
{
$this->aliases = $aliases;
return $this;
}
/**
* @param array $data
* @return $this
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param array|string|null $options
* @return $this
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param string $query
* @return $this
*/
protected function setQuery(string $query)
{
$this->query = $query;
return $this;
}
/**
* @param mixed $text
* @return $this
*/
protected function setText($text)
{
$this->text = $text;
return $this;
}
/**
* @param mixed $value
* @return $this
*/
protected function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function text()
{
return $this->text;
}
public function toArray(): array
{
return $this->options();
}
/**
* @return mixed
*/
public function value()
{
return $this->value;
}
}

View file

@ -20,8 +20,6 @@ class Validations
* Validates if the field value is boolean
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function boolean($field, $value): bool
@ -40,12 +38,9 @@ class Validations
/**
* Validates if the field value is valid date
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function date($field, $value): bool
public static function date(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::date($value) !== true) {
@ -61,12 +56,9 @@ class Validations
/**
* Validates if the field value is valid email
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function email($field, $value): bool
public static function email(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::email($value) === false) {
@ -82,14 +74,14 @@ class Validations
/**
* Validates if the field value is maximum
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function max($field, $value): bool
public static function max(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false && $field->max() !== null) {
if (
$field->isEmpty($value) === false &&
$field->max() !== null
) {
if (V::max($value, $field->max()) === false) {
throw new InvalidArgumentException(
V::message('max', $value, $field->max())
@ -103,14 +95,14 @@ class Validations
/**
* Validates if the field value is max length
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function maxlength($field, $value): bool
public static function maxlength(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false && $field->maxlength() !== null) {
if (
$field->isEmpty($value) === false &&
$field->maxlength() !== null
) {
if (V::maxLength($value, $field->maxlength()) === false) {
throw new InvalidArgumentException(
V::message('maxlength', $value, $field->maxlength())
@ -124,14 +116,14 @@ class Validations
/**
* Validates if the field value is minimum
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function min($field, $value): bool
public static function min(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false && $field->min() !== null) {
if (
$field->isEmpty($value) === false &&
$field->min() !== null
) {
if (V::min($value, $field->min()) === false) {
throw new InvalidArgumentException(
V::message('min', $value, $field->min())
@ -145,14 +137,14 @@ class Validations
/**
* Validates if the field value is min length
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function minlength($field, $value): bool
public static function minlength(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false && $field->minlength() !== null) {
if (
$field->isEmpty($value) === false &&
$field->minlength() !== null
) {
if (V::minLength($value, $field->minlength()) === false) {
throw new InvalidArgumentException(
V::message('minlength', $value, $field->minlength())
@ -166,18 +158,22 @@ class Validations
/**
* Validates if the field value matches defined pattern
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function pattern($field, $value): bool
public static function pattern(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false && $field->pattern() !== null) {
if (V::match($value, '/' . $field->pattern() . '/i') === false) {
throw new InvalidArgumentException(
V::message('match')
);
if ($field->isEmpty($value) === false) {
if ($pattern = $field->pattern()) {
// ensure that that pattern needs to match the whole
// input value from start to end, not just a partial match
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern#overview
$pattern = '^(?:' . $pattern . ')$';
if (V::match($value, '/' . $pattern . '/i') === false) {
throw new InvalidArgumentException(
V::message('match')
);
}
}
}
@ -187,14 +183,15 @@ class Validations
/**
* Validates if the field value is required
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function required($field, $value): bool
public static function required(Field|FieldClass $field, mixed $value): bool
{
if ($field->isRequired() === true && $field->save() === true && $field->isEmpty($value) === true) {
if (
$field->isRequired() === true &&
$field->save() === true &&
$field->isEmpty($value) === true
) {
throw new InvalidArgumentException([
'key' => 'validation.required'
]);
@ -206,12 +203,9 @@ class Validations
/**
* Validates if the field value is in defined options
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function option($field, $value): bool
public static function option(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
@ -229,12 +223,9 @@ class Validations
/**
* Validates if the field values is in defined options
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function options($field, $value): bool
public static function options(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
$values = array_column($field->options(), 'value');
@ -253,12 +244,9 @@ class Validations
/**
* Validates if the field value is valid time
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function time($field, $value): bool
public static function time(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::time($value) !== true) {
@ -274,12 +262,9 @@ class Validations
/**
* Validates if the field value is valid url
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function url($field, $value): bool
public static function url(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmpty($value) === false) {
if (V::url($value) === false) {