Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| SpecialAccessTransformer | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| transformRead | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| transformWrite | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Engine\Application\Transformer; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Engine\Domain\Model\FieldMetadata; |
| 12 | use App\Core\Engine\Domain\Model\SpecialAccess; |
| 13 | |
| 14 | /** |
| 15 | * Special Access UiType Transformer. |
| 16 | * |
| 17 | * Handles UiType: special_access (and legacy record_status alias). |
| 18 | * Read: formats integer special access code to human-readable label. |
| 19 | * Write: validates and casts input to integer in [1, 2, 3, 4] range, defaulting to 1 (AVAILABLE). |
| 20 | * |
| 21 | * @package App\Core\Engine\Application\Transformer |
| 22 | */ |
| 23 | final class SpecialAccessTransformer implements UiTypeTransformerInterface |
| 24 | { |
| 25 | /** {@inheritdoc} */ |
| 26 | public function supports(string $uitypeName): bool |
| 27 | { |
| 28 | return $uitypeName === 'special_access' || $uitypeName === 'record_status'; |
| 29 | } |
| 30 | |
| 31 | /** {@inheritdoc} */ |
| 32 | public function transformRead(mixed $rawValue, FieldMetadata $field): string |
| 33 | { |
| 34 | if ($rawValue === null || $rawValue === '') { |
| 35 | return SpecialAccess::getLabel(SpecialAccess::AVAILABLE); |
| 36 | } |
| 37 | |
| 38 | $code = (int) $rawValue; |
| 39 | if (!SpecialAccess::isValid($code)) { |
| 40 | return SpecialAccess::getLabel(SpecialAccess::AVAILABLE); |
| 41 | } |
| 42 | |
| 43 | return SpecialAccess::getLabel($code); |
| 44 | } |
| 45 | |
| 46 | /** {@inheritdoc} */ |
| 47 | public function transformWrite(mixed $inputValue, FieldMetadata $field): int |
| 48 | { |
| 49 | if ($inputValue === null || $inputValue === '') { |
| 50 | return SpecialAccess::AVAILABLE; |
| 51 | } |
| 52 | |
| 53 | $code = (int) $inputValue; |
| 54 | if (!SpecialAccess::isValid($code)) { |
| 55 | return SpecialAccess::AVAILABLE; |
| 56 | } |
| 57 | |
| 58 | return $code; |
| 59 | } |
| 60 | } |