Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Cidr.php

class Cidr

Validates that a value is a valid CIDR notation.

@author Sorin Pop <popsorin15@gmail.com> @author Calin Bolea <calin.bolea@gmail.com> @author Ninos Ego <me@ninosego.de>

Hierarchy

  • class \Symfony\Component\Validator\Constraint
    • class \Symfony\Component\Validator\Constraints\Cidr extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of Cidr

See also

https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

File

vendor/symfony/validator/Constraints/Cidr.php, line 27

Namespace

Symfony\Component\Validator\Constraints
View source
class Cidr extends Constraint {
    public const INVALID_CIDR_ERROR = '5649e53a-5afb-47c5-a360-ffbab3be8567';
    public const OUT_OF_RANGE_ERROR = 'b9f14a51-acbd-401a-a078-8c6b204ab32f';
    protected const ERROR_NAMES = [
        self::INVALID_CIDR_ERROR => 'INVALID_CIDR_ERROR',
        self::OUT_OF_RANGE_ERROR => 'OUT_OF_RANGE_VIOLATION',
    ];
    private const NET_MAXES = [
        Ip::V4 => 32,
        Ip::V6 => 128,
        Ip::ALL => 128,
        Ip::V4_NO_PUBLIC => 32,
        Ip::V6_NO_PUBLIC => 128,
        Ip::ALL_NO_PUBLIC => 128,
        Ip::V4_NO_PRIVATE => 32,
        Ip::V6_NO_PRIVATE => 128,
        Ip::ALL_NO_PRIVATE => 128,
        Ip::V4_NO_RESERVED => 32,
        Ip::V6_NO_RESERVED => 128,
        Ip::ALL_NO_RESERVED => 128,
        Ip::V4_ONLY_PUBLIC => 32,
        Ip::V6_ONLY_PUBLIC => 128,
        Ip::ALL_ONLY_PUBLIC => 128,
        Ip::V4_ONLY_PRIVATE => 32,
        Ip::V6_ONLY_PRIVATE => 128,
        Ip::ALL_ONLY_PRIVATE => 128,
        Ip::V4_ONLY_RESERVED => 32,
        Ip::V6_ONLY_RESERVED => 128,
        Ip::ALL_ONLY_RESERVED => 128,
    ];
    public string $version = Ip::ALL;
    public string $message = 'This value is not a valid CIDR notation.';
    public string $netmaskRangeViolationMessage = 'The value of the netmask should be between {{ min }} and {{ max }}.';
    public int $netmaskMin = 0;
    public int $netmaskMax;
    
    /** @var callable|null */
    public $normalizer;
    public function __construct(?array $options = null, ?string $version = null, ?int $netmaskMin = null, ?int $netmaskMax = null, ?string $message = null, ?array $groups = null, $payload = null, ?callable $normalizer = null) {
        $this->version = $version ?? $options['version'] ?? $this->version;
        if (!\array_key_exists($this->version, self::NET_MAXES)) {
            throw new ConstraintDefinitionException(\sprintf('The option "version" must be one of "%s".', implode('", "', array_keys(self::NET_MAXES))));
        }
        $this->netmaskMin = $netmaskMin ?? $options['netmaskMin'] ?? $this->netmaskMin;
        $this->netmaskMax = $netmaskMax ?? $options['netmaskMax'] ?? self::NET_MAXES[$this->version];
        $this->message = $message ?? $this->message;
        $this->normalizer = $normalizer ?? $this->normalizer;
        unset($options['netmaskMin'], $options['netmaskMax'], $options['version']);
        if ($this->netmaskMin < 0 || $this->netmaskMax > self::NET_MAXES[$this->version] || $this->netmaskMin > $this->netmaskMax) {
            throw new ConstraintDefinitionException(\sprintf('The netmask range must be between 0 and %d.', self::NET_MAXES[$this->version]));
        }
        if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
            throw new InvalidArgumentException(\sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
        }
        parent::__construct($options, $groups, $payload);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Cidr::$message public property
Cidr::$netmaskMax public property
Cidr::$netmaskMin public property
Cidr::$netmaskRangeViolationMessage public property
Cidr::$normalizer public property @var callable|null
Cidr::$version public property
Cidr::ERROR_NAMES protected constant Maps error codes to the names of their constants. Overrides Constraint::ERROR_NAMES
Cidr::INVALID_CIDR_ERROR public constant
Cidr::NET_MAXES private constant
Cidr::OUT_OF_RANGE_ERROR public constant
Cidr::__construct public function Initializes the constraint with options. Overrides Constraint::__construct
Constraint::$groups public property The groups that the constraint belongs to.
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 2
Constraint::CLASS_CONSTRAINT public constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP public constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 28
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getRequiredOptions public function Returns the name of the required options. 22
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or
both.
8
Constraint::normalizeOptions protected function
Constraint::PROPERTY_CONSTRAINT public constant Marks a constraint that can be put onto properties.
Constraint::validatedBy public function Returns the name of the class that validates this constraint. 9
Constraint::__get public function Returns the value of a lazily initialized option. 2
Constraint::__isset public function 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
RSS feed
Powered by Drupal