Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BooleanTransformer
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
6
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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 transformWrite
100.00% covered (success)
100.00%
3 / 3
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;
12
13/**
14 * Boolean Switch UiType Transformer.
15 *
16 * Handles UiType: boolean_toggle.
17 * Read: renders as an HTML badge (Active/Inactive) for grid display.
18 * Write: normalizes truthy/falsy values to PHP int 1 or 0.
19 *
20 * @package App\Core\Engine\Application\Transformer
21 */
22final class BooleanTransformer implements UiTypeTransformerInterface
23{
24    /** {@inheritdoc} */
25    public function supports(string $uitypeName): bool
26    {
27        return $uitypeName === 'boolean_toggle';
28    }
29
30    /** {@inheritdoc} */
31    public function transformRead(mixed $rawValue, FieldMetadata $field): string
32    {
33        return (bool) $rawValue ? '1' : '0';
34    }
35
36    /** {@inheritdoc} */
37    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
38    {
39        if ($inputValue === null) {
40            return 0;
41        }
42
43        return in_array($inputValue, [1, '1', 'true', 'on', true], true) ? 1 : 0;
44    }
45}