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
CurrencyTransformer
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 * Currency Amount UiType Transformer.
15 *
16 * Handles UiType: currency_amount.
17 * Read: formats as a monetary string with 2 decimal places.
18 * Write: casts to string with 2 decimal places for DECIMAL(15,2) DB type.
19 *
20 * @package App\Core\Engine\Application\Transformer
21 */
22final class CurrencyTransformer implements UiTypeTransformerInterface
23{
24    /** {@inheritdoc} */
25    public function supports(string $uitypeName): bool
26    {
27        return $uitypeName === 'currency_amount';
28    }
29
30    /** {@inheritdoc} */
31    public function transformRead(mixed $rawValue, FieldMetadata $field): string
32    {
33        if ($rawValue === null) {
34            return '';
35        }
36
37        return number_format((float) $rawValue, 2, '.', ' ');
38    }
39
40    /** {@inheritdoc} */
41    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
42    {
43        if ($inputValue === null || $inputValue === '') {
44            return null;
45        }
46
47        return number_format((float) str_replace(',', '.', (string) $inputValue), 2, '.', '');
48    }
49}