Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DecimalTransformer
100.00% covered (success)
100.00%
7 / 7
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%
3 / 3
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 * Decimal Number UiType Transformer.
15 *
16 * Handles UiType: decimal_number.
17 * Read: formats to 4 decimal places for display.
18 * Write: casts to string with 4 decimal precision for DECIMAL DB type.
19 *
20 * @package App\Core\Engine\Application\Transformer
21 */
22final class DecimalTransformer implements UiTypeTransformerInterface
23{
24    /** @var int Decimal display precision. */
25    private const int PRECISION = 4;
26
27    /** {@inheritdoc} */
28    public function supports(string $uitypeName): bool
29    {
30        return $uitypeName === 'decimal_number';
31    }
32
33    /** {@inheritdoc} */
34    public function transformRead(mixed $rawValue, FieldMetadata $field): string
35    {
36        if ($rawValue === null) {
37            return '';
38        }
39
40        return number_format((float) $rawValue, self::PRECISION, '.', ' ');
41    }
42
43    /** {@inheritdoc} */
44    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
45    {
46        if ($inputValue === null || $inputValue === '') {
47            return null;
48        }
49
50        return number_format((float) str_replace(',', '.', (string) $inputValue), self::PRECISION, '.', '');
51    }
52}