Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
StarRatingTransformer
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
8
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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 transformWrite
100.00% covered (success)
100.00%
4 / 4
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 * Star Rating UiType Transformer.
15 *
16 * Handles UiType: star_rating (ratings 1 to 5).
17 * Read: casts to integer value between 0 and 5.
18 * Write: casts to integer value between 0 and 5, or null if unset.
19 *
20 * @package App\Core\Engine\Application\Transformer
21 */
22final class StarRatingTransformer implements UiTypeTransformerInterface
23{
24    /** {@inheritdoc} */
25    public function supports(string $uitypeName): bool
26    {
27        return $uitypeName === 'star_rating' || $uitypeName === 'rating';
28    }
29
30    /** {@inheritdoc} */
31    public function transformRead(mixed $rawValue, FieldMetadata $field): string
32    {
33        if ($rawValue === null || $rawValue === '') {
34            return '';
35        }
36
37        $rating = (int) $rawValue;
38        return (string) max(0, min(5, $rating));
39    }
40
41    /** {@inheritdoc} */
42    public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed
43    {
44        if ($inputValue === null || $inputValue === '') {
45            return null;
46        }
47
48        $rating = (int) $inputValue;
49        return max(0, min(5, $rating));
50    }
51}