Skip to content

Commit

Permalink
Merge pull request #6776 from getkirby/v5/changes/forms-cleanup-3
Browse files Browse the repository at this point in the history
Form Cleanup Vol. 3
  • Loading branch information
bastianallgeier authored Nov 12, 2024
2 parents 5d3ff24 + a06a134 commit 9aa6a50
Show file tree
Hide file tree
Showing 25 changed files with 904 additions and 732 deletions.
13 changes: 2 additions & 11 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
use Stringable;
use Throwable;

/**
* The `$language` object represents
Expand Down Expand Up @@ -375,11 +374,7 @@ public static function loadRules(string $code): array
$file = $kirby->root('i18n:rules') . '/' . Str::before($code, '_') . '.json';
}

try {
return Data::read($file);
} catch (\Exception) {
return [];
}
return Data::read($file, fail: false);
}

/**
Expand Down Expand Up @@ -471,11 +466,7 @@ public function rules(): array
*/
public function save(): static
{
try {
$existingData = Data::read($this->root());
} catch (Throwable) {
$existingData = [];
}
$existingData = Data::read($this->root(), fail: false);

$data = [
...$existingData,
Expand Down
10 changes: 4 additions & 6 deletions src/Cms/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Data\Data;
use Kirby\Toolkit\Str;

Expand Down Expand Up @@ -111,11 +110,10 @@ public static function load(
string $root,
array $inject = []
): static {
try {
$data = [...Data::read($root), ...$inject];
} catch (Exception) {
$data = [];
}
$data = [
...Data::read($root, fail: false),
...$inject
];

return new static($code, $data);
}
Expand Down
39 changes: 31 additions & 8 deletions src/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kirby\Exception\Exception;
use Kirby\Filesystem\F;
use Throwable;

/**
* The `Data` class provides readers and
Expand Down Expand Up @@ -80,9 +81,20 @@ public static function handler(string $type): Handler
/**
* Decodes data with the specified handler
*/
public static function decode($string, string $type): array
{
return static::handler($type)->decode($string);
public static function decode(
$string,
string $type,
bool $fail = true
): array {
try {
return static::handler($type)->decode($string);
} catch (Throwable $e) {
if ($fail === false) {
return [];
}

throw $e;
}
}

/**
Expand All @@ -98,11 +110,22 @@ public static function encode($data, string $type): string
* the data handler is automatically chosen by
* the extension if not specified
*/
public static function read(string $file, string|null $type = null): array
{
$type ??= F::extension($file);
$handler = static::handler($type);
return $handler->read($file);
public static function read(
string $file,
string|null $type = null,
bool $fail = true
): array {
try {
$type ??= F::extension($file);
$handler = static::handler($type);
return $handler->read($file);
} catch (Throwable $e) {
if ($fail === false) {
return [];
}

throw $e;
}
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Data/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class Json extends Handler
/**
* Converts an array to an encoded JSON string
*/
public static function encode($data): string
public static function encode($data, bool $pretty = false): string
{
return json_encode(
$data,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
$constants = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

if ($pretty === true) {
$constants |= JSON_PRETTY_PRINT;
}

return json_encode($data, $constants);
}

/**
Expand Down
78 changes: 19 additions & 59 deletions src/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class Field extends Component
use HasSiblings;
use Mixin\Api;
use Mixin\Model;
use Mixin\Translatable;
use Mixin\Validation;
use Mixin\When;
use Mixin\Value;

/**
* Parent collection with all fields of the current form
Expand Down Expand Up @@ -76,30 +78,6 @@ public function __construct(
$this->siblings = $siblings ?? new Fields([$this]);
}

/**
* Returns field data
*/
public function data(bool $default = false): mixed
{
$save = $this->options['save'] ?? true;

if ($default === true && $this->isEmpty($this->value)) {
$value = $this->default();
} else {
$value = $this->value;
}

if ($save === false) {
return null;
}

if ($save instanceof Closure) {
return $save->call($this, $value);
}

return $value;
}

/**
* Default props and computed of the field
*/
Expand Down Expand Up @@ -322,34 +300,6 @@ public function isDisabled(): bool
return $this->disabled === true;
}

/**
* Checks if the field is empty
* @deprecated 5.0.0 Passing arguments is deprecated. Use `::isEmptyValue()` instead to check for
*/
public function isEmpty(mixed ...$args): bool
{
$value = match (count($args)) {
0 => $this->value(),
default => $args[0]
};

return $this->isEmptyValue($value);
}

/**
* Checks if the given value is considered empty
*
* @since 5.0.0
*/
public function isEmptyValue(mixed $value = null): bool
{
if ($empty = $this->options['isEmpty'] ?? null) {
return $empty->call($this, $value);
}

return in_array($value, [null, '', []], true);
}

/**
* Checks if the field is hidden
*/
Expand Down Expand Up @@ -435,19 +385,29 @@ public function toArray(): array
}

/**
* Defines all validation rules
* Returns the value of the field in a format to be stored by our storage classes
*/
protected function validations(): array
public function toStoredValue(bool $default = false): mixed
{
return $this->options['validations'] ?? [];
$value = $this->value($default);
$store = $this->options['save'] ?? true;

if ($store === false) {
return null;
}

if ($store instanceof Closure) {
return $store->call($this, $value);
}

return $value;
}

/**
* Returns the value of the field if saveable
* otherwise it returns null
* Defines all validation rules
*/
public function value(): mixed
protected function validations(): array
{
return $this->isSaveable() ? $this->value : null;
return $this->options['validations'] ?? [];
}
}
28 changes: 15 additions & 13 deletions src/Form/Field/BlocksField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kirby\Cms\Fieldset;
use Kirby\Cms\Fieldsets;
use Kirby\Cms\ModelWithContent;
use Kirby\Data\Json;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Form\FieldClass;
Expand Down Expand Up @@ -238,19 +239,6 @@ public function routes(): array
];
}

public function store(mixed $value): mixed
{
$blocks = $this->blocksToValues((array)$value, 'content');

// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if ($blocks === []) {
return '';
}

return $this->valueToJson($blocks, $this->pretty());
}

protected function setDefault(mixed $default = null): void
{
// set id for blocks if not exists
Expand Down Expand Up @@ -287,6 +275,20 @@ protected function setPretty(bool $pretty = false): void
$this->pretty = $pretty;
}

public function toStoredValue(bool $default = false): mixed
{
$value = $this->toFormValue($default);
$blocks = $this->blocksToValues((array)$value, 'content');

// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if ($blocks === []) {
return '';
}

return Json::encode($blocks, pretty: $this->pretty());
}

public function validations(): array
{
return [
Expand Down
9 changes: 6 additions & 3 deletions src/Form/Field/LayoutField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Kirby\Cms\Fieldset;
use Kirby\Cms\Layout;
use Kirby\Cms\Layouts;
use Kirby\Data\Data;
use Kirby\Data\Json;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Form\Form;
use Kirby\Toolkit\Str;
Expand All @@ -30,7 +32,7 @@ public function __construct(array $params)

public function fill(mixed $value = null): void
{
$value = $this->valueFromJson($value);
$value = Data::decode($value, type: 'json', fail: false);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();

foreach ($layouts as $layoutIndex => $layout) {
Expand Down Expand Up @@ -267,8 +269,9 @@ public function settings(): Fieldset|null
return $this->settings;
}

public function store(mixed $value): mixed
public function toStoredValue(bool $default = false): mixed
{
$value = $this->toFormValue($default);
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();

// returns empty string to avoid storing empty array as string `[]`
Expand All @@ -287,7 +290,7 @@ public function store(mixed $value): mixed
}
}

return $this->valueToJson($value, $this->pretty());
return Json::encode($value, pretty: $this->pretty());
}

public function validations(): array
Expand Down
Loading

0 comments on commit 9aa6a50

Please sign in to comment.