Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ModuleReferenceTransformer
100.00% covered (success)
100.00%
15 / 15
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
2
 transformRead
100.00% covered (success)
100.00%
7 / 7
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
3
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;
12use App\Shared\Utils\JsonArrayHelper;
13
14/**
15 * Module Reference (Rel2Module) UiType Transformer.
16 *
17 * Handles UiType: module_reference (rel2module).
18 * Read: displays pre-resolved module label or falls back to FK integer / multi-module IDs.
19 * Write: casts valid module ID inputs to integer or JSON array.
20 *
21 * @package App\Core\Engine\Application\Transformer
22 */
23final class ModuleReferenceTransformer implements UiTypeTransformerInterface
24{
25    /** {@inheritdoc} */
26    public function supports(string $uitypeName): bool
27    {
28        return $uitypeName === 'module_reference' || $uitypeName === 'rel2module';
29    }
30
31    /** {@inheritdoc} */
32    public function transformRead(mixed $rawValue, FieldMetadata $field): string
33    {
34        if ($rawValue === null || $rawValue === '') {
35            return '';
36        }
37
38        if (is_string($rawValue) && str_starts_with($rawValue, '[')) {
39            $ids = JsonArrayHelper::toIntList($rawValue);
40            if ($ids !== []) {
41                return implode(', ', $ids);
42            }
43        }
44
45        return (string) $rawValue;
46    }
47
48    /** {@inheritdoc} */
49    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
50    {
51        if (is_array($inputValue)) {
52            $ids = array_values(array_filter(
53                array_map('intval', $inputValue),
54                static fn(int $id): bool => $id > 0
55            ));
56            return (string) json_encode($ids);
57        }
58
59        return is_numeric($inputValue) ? (int) $inputValue : null;
60    }
61}
62