Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UserReferenceTransformer | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
7.14 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| transformWrite | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
5.12 | |||
| 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 | |
| 13 | /** |
| 14 | * User Reference (Rel2User) UiType Transformer. |
| 15 | * |
| 16 | * Handles UiType: user_reference (rel2user). |
| 17 | * Read: displays pre-resolved user username label or falls back to FK integer. |
| 18 | * Write: casts valid user ID inputs to integer or null. |
| 19 | * |
| 20 | * @package App\Core\Engine\Application\Transformer |
| 21 | */ |
| 22 | final class UserReferenceTransformer extends AbstractIntegerReferenceTransformer |
| 23 | { |
| 24 | /** {@inheritdoc} */ |
| 25 | public function supports(string $uitypeName): bool |
| 26 | { |
| 27 | return $uitypeName === 'user_reference' || $uitypeName === 'rel2user'; |
| 28 | } |
| 29 | |
| 30 | /** {@inheritdoc} */ |
| 31 | public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed |
| 32 | { |
| 33 | if ($inputValue === null || $inputValue === '') { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | $valStr = (string) $inputValue; |
| 38 | if (str_starts_with($valStr, 'struct_')) { |
| 39 | return (int) substr($valStr, 7); |
| 40 | } |
| 41 | |
| 42 | return is_numeric($inputValue) ? (int) $inputValue : null; |
| 43 | } |
| 44 | } |