Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| IpAddressTransformer | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| transformWrite | |
100.00% |
5 / 5 |
|
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 | |
| 13 | /** |
| 14 | * IP Address UiType Transformer. |
| 15 | * |
| 16 | * Handles UiType: ip_address. |
| 17 | * Read: HTML-escapes IP address string for safe display. |
| 18 | * Write: validates IPv4/IPv6 format; returns null for invalid input. |
| 19 | * |
| 20 | * @package App\Core\Engine\Application\Transformer |
| 21 | */ |
| 22 | final class IpAddressTransformer extends AbstractStringTransformer |
| 23 | { |
| 24 | /** {@inheritdoc} */ |
| 25 | public function supports(string $uitypeName): bool |
| 26 | { |
| 27 | return $uitypeName === 'ip_address'; |
| 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 | $ip = trim((string) $inputValue); |
| 38 | $validated = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6); |
| 39 | |
| 40 | return $validated !== false ? $validated : null; |
| 41 | } |
| 42 | } |