Upgrade to 3.4.4

This commit is contained in:
Bastian Allgeier
2020-10-06 10:23:02 +02:00
parent c091f04115
commit 0b80361a79
53 changed files with 976 additions and 83 deletions

View File

@@ -3,7 +3,6 @@
namespace Kirby\Form;
use Exception;
use Kirby\Cms\Model;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Component;
@@ -51,6 +50,14 @@ class Field extends Component
*/
public static $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)
{
if (isset(static::$types[$type]) === false) {
@@ -71,6 +78,8 @@ class Field extends Component
}
/**
* Returns field api call
*
* @return mixed
*/
public function api()
@@ -81,10 +90,12 @@ class Field extends Component
}
/**
* @param mixed $default
* Returns field data
*
* @param bool $default
* @return mixed
*/
public function data($default = false)
public function data(bool $default = false)
{
$save = $this->options['save'] ?? true;
@@ -103,6 +114,11 @@ class Field extends Component
}
}
/**
* Default props and computed of the field
*
* @return array
*/
public static function defaults(): array
{
return [
@@ -232,11 +248,21 @@ class Field extends Component
];
}
/**
* Parent collection with all fields of the current form
*
* @return \Kirby\Form\Fields|null
*/
public function formFields(): ?Fields
{
return $this->formFields;
}
/**
* Validates when run for the first time and returns any errors
*
* @return array
*/
public function errors(): array
{
if ($this->errors === null) {
@@ -246,6 +272,12 @@ class Field extends Component
return $this->errors;
}
/**
* Checks if the field is empty
*
* @param mixed ...$args
* @return bool
*/
public function isEmpty(...$args): bool
{
if (count($args) === 0) {
@@ -261,22 +293,39 @@ class Field extends Component
return in_array($value, [null, '', []], true);
}
/**
* Checks if the field is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return empty($this->errors()) === false;
}
/**
* Checks if the field is required
*
* @return bool
*/
public function isRequired(): bool
{
return $this->required ?? false;
}
/**
* Checks if the field is valid
*
* @return bool
*/
public function isValid(): bool
{
return empty($this->errors()) === true;
}
/**
* Returns the Kirby instance
*
* @return \Kirby\Cms\App
*/
public function kirby()
@@ -284,6 +333,11 @@ class Field extends Component
return $this->model->kirby();
}
/**
* Returns the parent model
*
* @return mixed|null
*/
public function model()
{
return $this->model;
@@ -329,11 +383,21 @@ class Field extends Component
return true;
}
/**
* Checks if the field is saveable
*
* @return bool
*/
public function save(): bool
{
return ($this->options['save'] ?? true) !== false;
}
/**
* Converts the field to a plain array
*
* @return array
*/
public function toArray(): array
{
$array = parent::toArray();
@@ -352,6 +416,11 @@ class Field extends Component
});
}
/**
* Runs the validations defined for the field
*
* @return void
*/
protected function validate(): void
{
$validations = $this->options['validations'] ?? [];
@@ -395,6 +464,12 @@ class Field extends Component
}
}
/**
* Returns the value of the field if saveable
* otherwise it returns null
*
* @return mixed
*/
public function value()
{
return $this->save() ? $this->value : null;

View File

@@ -22,11 +22,12 @@ class Fields extends Collection
* the collection prop on each object correctly.
*
* @param string $name
* @param object $field
* @param object|array $field
* @return self
*/
public function __set(string $name, $field)
{
if (is_array($field)) {
if (is_array($field) === true) {
// use the array key as name if the name is not set
$field['name'] = $field['name'] ?? $name;
$field = new Field($field['type'], $field);
@@ -40,7 +41,7 @@ class Fields extends Collection
* array and also does that for every
* included field.
*
* @param Closure $map
* @param \Closure|null $map
* @return array
*/
public function toArray(Closure $map = null): array

View File

@@ -19,10 +19,32 @@ use Throwable;
*/
class Form
{
/**
* An array of all found errors
*
* @var array|null
*/
protected $errors;
/**
* Fields in the form
*
* @var \Kirby\Form\Fields|null
*/
protected $fields;
/**
* All values of form
*
* @var array
*/
protected $values = [];
/**
* Form constructor
*
* @param array $props
*/
public function __construct(array $props)
{
$fields = $props['fields'] ?? [];
@@ -85,11 +107,24 @@ 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
{
return $this->data(false, false);
}
/**
* 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
{
$data = $this->values;
@@ -109,6 +144,11 @@ class Form
return $data;
}
/**
* An array of all found errors
*
* @return array
*/
public function errors(): array
{
if ($this->errors !== null) {
@@ -129,6 +169,13 @@ class Form
return $this->errors;
}
/**
* Shows the error with the field
*
* @param \Throwable $exception
* @param array $props
* @return \Kirby\Form\Field
*/
public static function exceptionField(Throwable $exception, array $props = [])
{
$props = array_merge($props, [
@@ -140,21 +187,42 @@ class Form
return new Field('info', $props);
}
/**
* Returns form fields
*
* @return \Kirby\Form\Fields|null
*/
public function fields()
{
return $this->fields;
}
/**
* Checks if the form is invalid
*
* @return bool
*/
public function isInvalid(): bool
{
return empty($this->errors()) === false;
}
/**
* Checks if the form is valid
*
* @return bool
*/
public function isValid(): bool
{
return empty($this->errors()) === true;
}
/**
* Converts the data of fields to strings
*
* @param false $defaults
* @return array
*/
public function strings($defaults = false): array
{
$strings = [];
@@ -172,6 +240,11 @@ class Form
return $strings;
}
/**
* Converts the form to a plain array
*
* @return array
*/
public function toArray(): array
{
$array = [
@@ -185,6 +258,11 @@ class Form
return $array;
}
/**
* Returns form values
*
* @return array
*/
public function values(): array
{
return $this->values;

View File

@@ -19,6 +19,11 @@ use Kirby\Toolkit\I18n;
*/
class Options
{
/**
* Returns the classes of predefined Kirby objects
*
* @return array
*/
protected static function aliases(): array
{
return [
@@ -30,6 +35,13 @@ class Options
];
}
/**
* Brings options through api
*
* @param $api
* @param $model
* @return array
*/
public static function api($api, $model = null): array
{
$model = $model ?? App::instance()->site();
@@ -57,6 +69,10 @@ class Options
return $optionsApi->options();
}
/**
* @param $model
* @return array
*/
protected static function data($model): array
{
$kirby = $model->kirby();
@@ -78,6 +94,14 @@ class Options
return $data;
}
/**
* Brings options by supporting both api and query
*
* @param $options
* @param array $props
* @param null $model
* @return array
*/
public static function factory($options, array $props = [], $model = null): array
{
switch ($options) {
@@ -131,6 +155,13 @@ class Options
return $result;
}
/**
* Brings options with query
*
* @param $query
* @param null $model
* @return array
*/
public static function query($query, $model = null): array
{
$model = $model ?? App::instance()->site();

View File

@@ -25,34 +25,78 @@ class OptionsApi
{
use Properties;
/**
* @var
*/
protected $data;
/**
* @var
*/
protected $fetch;
/**
* @var
*/
protected $options;
/**
* @var string
*/
protected $text = '{{ item.value }}';
/**
* @var
*/
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)
{
$value = $this->$field();
return Str::template($value, $data);
}
/**
* @return array
* @throws \Exception
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function options(): array
{
if (is_array($this->options) === true) {
@@ -103,51 +147,94 @@ class OptionsApi
return $options;
}
/**
* @param array $data
* @return self
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param string|null $fetch
* @return self
*/
protected function setFetch(string $fetch = null)
{
$this->fetch = $fetch;
return $this;
}
/**
* @param $options
* @return self
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param $text
* @return self
*/
protected function setText($text = null)
{
$this->text = $text;
return $this;
}
/**
* @param $url
* @return self
*/
protected function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* @param null $value
* @return self
*/
protected function setValue($value = null)
{
$this->value = $value;
return $this;
}
/**
* @return string
*/
public function text()
{
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()
{
return $this->value;

View File

@@ -27,28 +27,69 @@ class OptionsQuery
{
use Properties;
/**
* @var array
*/
protected $aliases = [];
/**
* @var
*/
protected $data;
/**
* @var
*/
protected $options;
/**
* @var
*/
protected $query;
/**
* @var
*/
protected $text;
/**
* @var
*/
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();
@@ -64,6 +105,9 @@ class OptionsQuery
return Str::template($value, $data);
}
/**
* @return array
*/
public function options(): array
{
if (is_array($this->options) === true) {
@@ -89,11 +133,18 @@ class OptionsQuery
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
@@ -111,6 +162,10 @@ class OptionsQuery
return 'item';
}
/**
* @param $result
* @throws \Kirby\Exception\InvalidArgumentException
*/
protected function resultToCollection($result)
{
if (is_array($result)) {
@@ -133,36 +188,69 @@ class OptionsQuery
return $result;
}
/**
* @param array|null $aliases
* @return self
*/
protected function setAliases(array $aliases = null)
{
$this->aliases = $aliases;
return $this;
}
/**
* @param array $data
* @return self
*/
protected function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* @param $options
* @return self
*/
protected function setOptions($options = null)
{
$this->options = $options;
return $this;
}
/**
* @param string $query
* @return self
*/
protected function setQuery(string $query)
{
$this->query = $query;
return $this;
}
/**
* @param $text
* @return self
*/
protected function setText($text)
{
$this->text = $text;
return $this;
}
/**
* @param $value
* @return self
*/
protected function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function text()
{
return $this->text;
@@ -173,6 +261,9 @@ class OptionsQuery
return $this->options();
}
/**
* @return mixed
*/
public function value()
{
return $this->value;

View File

@@ -16,6 +16,14 @@ use Kirby\Toolkit\V;
*/
class Validations
{
/**
* Validates if the field value is boolean
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function boolean(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -29,6 +37,14 @@ class Validations
return true;
}
/**
* Validates if the field value is valid date
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function date(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -42,6 +58,14 @@ class Validations
return true;
}
/**
* Validates if the field value is valid email
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function email(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -55,6 +79,14 @@ class Validations
return true;
}
/**
* Validates if the field value is maximum
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function max(Field $field, $value): bool
{
if ($field->isEmpty($value) === false && $field->max() !== null) {
@@ -68,6 +100,14 @@ class Validations
return true;
}
/**
* Validates if the field value is max length
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function maxlength(Field $field, $value): bool
{
if ($field->isEmpty($value) === false && $field->maxlength() !== null) {
@@ -81,6 +121,14 @@ class Validations
return true;
}
/**
* Validates if the field value is minimum
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function min(Field $field, $value): bool
{
if ($field->isEmpty($value) === false && $field->min() !== null) {
@@ -94,6 +142,14 @@ class Validations
return true;
}
/**
* Validates if the field value is min length
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function minlength(Field $field, $value): bool
{
if ($field->isEmpty($value) === false && $field->minlength() !== null) {
@@ -107,6 +163,14 @@ class Validations
return true;
}
/**
* Validates if the field value matches defined pattern
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function pattern(Field $field, $value): bool
{
if ($field->isEmpty($value) === false && $field->pattern() !== null) {
@@ -120,6 +184,14 @@ class Validations
return true;
}
/**
* Validates if the field value is required
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function required(Field $field, $value): bool
{
if ($field->isRequired() === true && $field->save() === true && $field->isEmpty($value) === true) {
@@ -131,6 +203,14 @@ class Validations
return true;
}
/**
* Validates if the field value is in defined options
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function option(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -146,6 +226,14 @@ class Validations
return true;
}
/**
* Validates if the field values is in defined options
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function options(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -162,6 +250,14 @@ class Validations
return true;
}
/**
* Validates if the field value is valid time
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function time(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {
@@ -175,6 +271,14 @@ class Validations
return true;
}
/**
* Validates if the field value is valid url
*
* @param \Kirby\Form\Field $field
* @param $value
* @return bool
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function url(Field $field, $value): bool
{
if ($field->isEmpty($value) === false) {