Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
JsonDisplayTransformer
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transformRead
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 transformWrite
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Engine\Application\Transformer;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Engine\Domain\Model\FieldMetadata;
12
13/**
14 * JSON Viewer UiType Transformer.
15 *
16 * Handles UiType: json_display.
17 * Read: decodes and formats JSON string for display.
18 * Write: validates JSON string and stores as-is.
19 *
20 * @package App\Core\Engine\Application\Transformer
21 */
22final class JsonDisplayTransformer implements UiTypeTransformerInterface
23{
24    /** {@inheritdoc} */
25    public function supports(string $uitypeName): bool
26    {
27        return $uitypeName === 'json_display';
28    }
29
30    /** {@inheritdoc} */
31    public function transformRead(mixed $rawValue, FieldMetadata $field): string
32    {
33        if ($rawValue === null || $rawValue === '') {
34            return '';
35        }
36
37        $raw = (string) $rawValue;
38        $decoded = json_decode($raw, true);
39
40        if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) {
41            return $raw;
42        }
43
44        $pretty = json_encode($decoded, JSON_UNESCAPED_UNICODE);
45        return $pretty !== false ? $pretty : $raw;
46    }
47
48    /** {@inheritdoc} */
49    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
50    {
51        if ($inputValue === null || $inputValue === '') {
52            return null;
53        }
54
55        $raw = (string) $inputValue;
56        json_decode($raw);
57
58        if (json_last_error() !== JSON_ERROR_NONE) {
59            return null;
60        }
61
62        return $raw;
63    }
64}