|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraint; |
| 15 | +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Validates that a value is a valid CIDR notation. |
| 19 | + * |
| 20 | + * @Annotation |
| 21 | + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) |
| 22 | + * |
| 23 | + * @author Sorin Pop <[email protected]> |
| 24 | + * @author Calin Bolea <[email protected]> |
| 25 | + */ |
| 26 | +#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
| 27 | +class Cidr extends Constraint |
| 28 | +{ |
| 29 | + public const INVALID_CIDR_ERROR = '5649e53a-5afb-47c5-a360-ffbab3be8567'; |
| 30 | + public const OUT_OF_RANGE_ERROR = 'b9f14a51-acbd-401a-a078-8c6b204ab32f'; |
| 31 | + |
| 32 | + protected static $errorNames = [ |
| 33 | + self::INVALID_CIDR_ERROR => 'INVALID_CIDR_ERROR', |
| 34 | + self::OUT_OF_RANGE_ERROR => 'OUT_OF_RANGE_VIOLATION', |
| 35 | + ]; |
| 36 | + |
| 37 | + private const NET_MAXES = [ |
| 38 | + Ip::ALL => 128, |
| 39 | + Ip::V4 => 32, |
| 40 | + Ip::V6 => 128, |
| 41 | + ]; |
| 42 | + |
| 43 | + public $version = Ip::ALL; |
| 44 | + |
| 45 | + public $message = 'This value is not a valid CIDR notation.'; |
| 46 | + |
| 47 | + public $netmaskRangeViolationMessage = 'The value of the netmask should be between {{ min }} and {{ max }}.'; |
| 48 | + |
| 49 | + public $netmaskMin = 0; |
| 50 | + |
| 51 | + public $netmaskMax; |
| 52 | + |
| 53 | + public function __construct( |
| 54 | + array $options = null, |
| 55 | + string $version = null, |
| 56 | + int $netmaskMin = null, |
| 57 | + int $netmaskMax = null, |
| 58 | + string $message = null, |
| 59 | + array $groups = null, |
| 60 | + $payload = null |
| 61 | + ) { |
| 62 | + $this->version = $version ?? $options['version'] ?? $this->version; |
| 63 | + |
| 64 | + if (!\in_array($this->version, array_keys(self::NET_MAXES))) { |
| 65 | + throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s".', implode('", "', array_keys(self::NET_MAXES)))); |
| 66 | + } |
| 67 | + |
| 68 | + $this->netmaskMin = $netmaskMin ?? $options['netmaskMin'] ?? $this->netmaskMin; |
| 69 | + $this->netmaskMax = $netmaskMax ?? $options['netmaskMax'] ?? self::NET_MAXES[$this->version]; |
| 70 | + $this->message = $message ?? $this->message; |
| 71 | + |
| 72 | + unset($options['netmaskMin'], $options['netmaskMax'], $options['version']); |
| 73 | + |
| 74 | + if ($this->netmaskMin < 0 || $this->netmaskMax > self::NET_MAXES[$this->version] || $this->netmaskMin > $this->netmaskMax) { |
| 75 | + throw new ConstraintDefinitionException(sprintf('The netmask range must be between 0 and %d.', self::NET_MAXES[$this->version])); |
| 76 | + } |
| 77 | + |
| 78 | + parent::__construct($options, $groups, $payload); |
| 79 | + } |
| 80 | +} |
0 commit comments