Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PhoneInputTransformer | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| transformWrite | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
7 | |||
| 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 | * Phone Input UiType Transformer. |
| 15 | * |
| 16 | * Handles UiType: phone_input (E.164 normalization, white-space cleaning, format validation). |
| 17 | * |
| 18 | * @package App\Core\Engine\Application\Transformer |
| 19 | */ |
| 20 | final class PhoneInputTransformer extends AbstractStringTransformer |
| 21 | { |
| 22 | /** {@inheritdoc} */ |
| 23 | public function supports(string $uitypeName): bool |
| 24 | { |
| 25 | return $uitypeName === 'phone_input' || $uitypeName === 'phone' || $uitypeName === 'tel'; |
| 26 | } |
| 27 | |
| 28 | /** {@inheritdoc} */ |
| 29 | public function transformWrite(mixed $inputValue, FieldMetadata $field): mixed |
| 30 | { |
| 31 | $raw = is_scalar($inputValue) ? trim((string)$inputValue) : ''; |
| 32 | if ($raw === '') { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | $hasPlus = str_starts_with($raw, '+') || str_starts_with($raw, '00'); |
| 37 | $cleanRaw = str_starts_with($raw, '00') ? substr($raw, 2) : $raw; |
| 38 | $digitsOnly = preg_replace('/\D/', '', $cleanRaw) ?? ''; |
| 39 | |
| 40 | if (strlen($digitsOnly) < 5) { |
| 41 | return $raw; |
| 42 | } |
| 43 | |
| 44 | return $hasPlus ? '+' . $digitsOnly : $digitsOnly; |
| 45 | } |
| 46 | } |