Skip to content

Commit

Permalink
Merge pull request #6782 from getkirby/v5/changes/forms-data-methods
Browse files Browse the repository at this point in the history
`Form\Value`: Move data methods to `Data` classes
  • Loading branch information
bastianallgeier authored Nov 9, 2024
2 parents 8c455ee + dd3fa82 commit af35c8b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 227 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 $fail = true
): array {
try {
return static::handler($type)->decode($string);
} 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
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 @@ -285,7 +286,7 @@ public function toStoredValue(bool $default = false): mixed
return '';
}

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

public function validations(): array
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', fail: false);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();

foreach ($layouts as $layoutIndex => $layout) {
Expand Down Expand Up @@ -288,7 +290,7 @@ public function toStoredValue(bool $default = false): 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 @@ -108,48 +105,4 @@ public function value(bool $default = false): mixed
{
return $this->toFormValue($default);
}

/**
* 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');
}
}
10 changes: 10 additions & 0 deletions tests/Data/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public function testDecodeInvalid3($handler)
Data::decode(true, $handler);
}

/**
* @covers ::decode
* @dataProvider handlerProvider
*/
public function testDecodeInvalidNoExceptions($handler)
{
$data = Data::decode(1, $handler, fail: false);
$this->assertSame([], $data);
}

public static function handlerProvider(): array
{
// the PHP handler doesn't support decoding and therefore cannot be
Expand Down
39 changes: 30 additions & 9 deletions tests/Data/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ public function testDecodeInvalid2()
Json::decode(1);
}

/**
* @covers ::encode
*/
public function testEncodeUnicode()
{
$string = 'здравей';
$this->assertSame('"' . $string . '"', Json::encode($string));
}

/**
* @covers ::decode
*/
Expand All @@ -87,4 +78,34 @@ public function testDecodeCorrupted2()

Json::decode('true');
}

/**
* @covers ::encode
*/
public function testEncodePretty()
{
$array = [
'name' => 'Homer',
'children' => ['Lisa', 'Bart', 'Maggie']
];

$data = Json::encode($array, pretty: true);
$this->assertSame('{
"name": "Homer",
"children": [
"Lisa",
"Bart",
"Maggie"
]
}', $data);
}

/**
* @covers ::encode
*/
public function testEncodeUnicode()
{
$string = 'здравей';
$this->assertSame('"' . $string . '"', Json::encode($string));
}
}
68 changes: 0 additions & 68 deletions tests/Form/FieldClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

class TestField extends FieldClass
Expand All @@ -27,22 +26,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 @@ -599,57 +582,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
Loading

0 comments on commit af35c8b

Please sign in to comment.