Skip to content

Commit

Permalink
Form\Value: Move data methods to Data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Nov 5, 2024
1 parent bf4c4e7 commit 5cb9522
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 217 deletions.
18 changes: 15 additions & 3 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 $exceptions = true
): array {
try {
return static::handler($type)->decode($string);
} catch (Throwable $e) {
if ($exceptions === 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
3 changes: 2 additions & 1 deletion 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 @@ -248,7 +249,7 @@ protected function store(mixed $value): mixed
return '';
}

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

protected function setDefault(mixed $default = null): void
Expand Down
6 changes: 4 additions & 2 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', exceptions: false);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();

foreach ($layouts as $layoutIndex => $layout) {
Expand Down Expand Up @@ -287,7 +289,7 @@ protected function store(mixed $value): mixed
}
}

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

public function validations(): array
Expand Down
47 changes: 0 additions & 47 deletions src/Form/Mixin/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Kirby\Form\Mixin;

use Kirby\Data\Data;
use Throwable;

/**
* @package Kirby Form
* @author Bastian Allgeier <[email protected]>
Expand Down Expand Up @@ -119,48 +116,4 @@ public function value(bool $default = false): mixed

return $this->value;
}

/**
* Decodes a JSON string into an array
*/
protected function valueFromJson(mixed $value): array
{
try {
return Data::decode($value, 'json');
} catch (Throwable) {
return [];
}
}

/**
* Decodes a YAML string into an array
*/
protected function valueFromYaml(mixed $value): array
{
return Data::decode($value, 'yaml');
}

/**
* Encodes an array into a JSON string
*/
protected function valueToJson(
array|null $value = null,
bool $pretty = false
): string {
$constants = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

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

return json_encode($value, $constants);
}

/**
* Encodes an array into a YAML string
*/
protected function valueToYaml(array|null $value = null): string
{
return Data::encode($value, 'yaml');
}
}
68 changes: 1 addition & 67 deletions tests/Form/FieldClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kirby\Cms\Page;
use Kirby\Data\Data;
use Kirby\Exception\InvalidArgumentException;
use Kirby\TestCase;

Expand All @@ -27,22 +28,6 @@ public function isSaveable(): bool
}
}

class JsonField extends FieldClass
{
public function fill($value = null): void
{
$this->value = $this->valueFromJson($value);
}
}

class YamlField extends FieldClass
{
public function fill($value = null): void
{
$this->value = $this->valueFromYaml($value);
}
}

class ValidatedField extends FieldClass
{
public function validations(): array
Expand Down Expand Up @@ -600,57 +585,6 @@ public function testValue()
$this->assertNull($field->value());
}

/**
* @covers ::valueFromJson
*/
public function testValueFromJson()
{
$value = [
[
'content' => 'Heading 1',
'id' => 'h1',
'type' => 'h1',
]
];

// use simple value
$field = new JsonField(['value' => json_encode($value)]);
$this->assertSame($value, $field->value());

// use empty value
$field = new JsonField(['value' => '']);
$this->assertSame([], $field->value());

// use invalid value
$field = new JsonField(['value' => '{invalid}']);
$this->assertSame([], $field->value());
}

/**
* @covers ::valueFromYaml
*/
public function testValueFromYaml()
{
$value = "name: Homer\nchildren:\n - Lisa\n - Bart\n - Maggie\n";
$expected = [
'name' => 'Homer',
'children' => ['Lisa', 'Bart', 'Maggie']
];

// use simple value
$field = new YamlField(['value' => $value]);
$this->assertSame($expected, $field->value());

// use empty value
$field = new YamlField(['value' => '']);
$this->assertSame([], $field->value());

// use invalid value
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid YAML data; please pass a string');
new YamlField(['value' => new \stdClass()]);
}

/**
* @covers ::when
*/
Expand Down
92 changes: 0 additions & 92 deletions tests/Form/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,96 +1279,4 @@ public function testWidth()
$this->assertSame('1/2', $field->width());
$this->assertSame('1/2', $field->width);
}

/**
* @covers ::valueFromJson
*/
public function testValueFromJson()
{
// no defined as default
Field::$types = [
'test' => [
'props' => [
'value' => function (string $value): array {
return $this->valueFromJson($value);
}
]
]
];

$field = new Field('test', [
'model' => $model = new Page(['slug' => 'test']),
'value' => json_encode($value = ['a' => 'A'])
]);

$this->assertSame($value, $field->toFormValue());
}

/**
* @covers ::valueFromYaml
*/
public function testValueFromYaml()
{
// no defined as default
Field::$types = [
'test' => [
'props' => [
'value' => function (string $value): array {
return $this->valueFromYaml($value);
}
]
]
];

$field = new Field('test', [
'model' => $model = new Page(['slug' => 'test']),
'value' => Data::encode($value = ['a' => 'A'], 'yml')
]);

$this->assertSame($value, $field->toFormValue());
}

/**
* @covers ::valueToJson
*/
public function testValueToJson()
{
// no defined as default
Field::$types = [
'test' => [
'save' => function (array $value): string {
return $this->valueToJson($value);
}
]
];

$field = new Field('test', [
'model' => $model = new Page(['slug' => 'test']),
'value' => ['a' => 'A']
]);

$this->assertSame('{"a":"A"}', $field->toStoredValue());
}

/**
* @covers ::valueToYaml
*/
public function testValueToYaml()
{
// no defined as default
Field::$types = [
'test' => [
'save' => function (array $value): string {
return $this->valueToYaml($value);
}
]
];

$field = new Field('test', [
'model' => $model = new Page(['slug' => 'test']),
'value' => ['a' => 'A']
]);

$this->assertSame("a: A\n", $field->toStoredValue());
}
}

0 comments on commit 5cb9522

Please sign in to comment.