Ip.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. use Symfony\Component\Validator\Exception\InvalidArgumentException;
  14. /**
  15. * Validates that a value is a valid IP address.
  16. *
  17. * @Annotation
  18. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  19. *
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. * @author Joseph Bielawski <stloyd@gmail.com>
  22. */
  23. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  24. class Ip extends Constraint
  25. {
  26. public const V4 = '4';
  27. public const V6 = '6';
  28. public const ALL = 'all';
  29. // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
  30. public const V4_NO_PRIV = '4_no_priv';
  31. public const V6_NO_PRIV = '6_no_priv';
  32. public const ALL_NO_PRIV = 'all_no_priv';
  33. // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
  34. public const V4_NO_RES = '4_no_res';
  35. public const V6_NO_RES = '6_no_res';
  36. public const ALL_NO_RES = 'all_no_res';
  37. // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
  38. public const V4_ONLY_PUBLIC = '4_public';
  39. public const V6_ONLY_PUBLIC = '6_public';
  40. public const ALL_ONLY_PUBLIC = 'all_public';
  41. public const INVALID_IP_ERROR = 'b1b427ae-9f6f-41b0-aa9b-84511fbb3c5b';
  42. protected static $versions = [
  43. self::V4,
  44. self::V6,
  45. self::ALL,
  46. self::V4_NO_PRIV,
  47. self::V6_NO_PRIV,
  48. self::ALL_NO_PRIV,
  49. self::V4_NO_RES,
  50. self::V6_NO_RES,
  51. self::ALL_NO_RES,
  52. self::V4_ONLY_PUBLIC,
  53. self::V6_ONLY_PUBLIC,
  54. self::ALL_ONLY_PUBLIC,
  55. ];
  56. protected static $errorNames = [
  57. self::INVALID_IP_ERROR => 'INVALID_IP_ERROR',
  58. ];
  59. public $version = self::V4;
  60. public $message = 'This is not a valid IP address.';
  61. public $normalizer;
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function __construct(
  66. array $options = null,
  67. string $version = null,
  68. string $message = null,
  69. callable $normalizer = null,
  70. array $groups = null,
  71. $payload = null
  72. ) {
  73. parent::__construct($options, $groups, $payload);
  74. $this->version = $version ?? $this->version;
  75. $this->message = $message ?? $this->message;
  76. $this->normalizer = $normalizer ?? $this->normalizer;
  77. if (!\in_array($this->version, self::$versions)) {
  78. throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s".', implode('", "', self::$versions)));
  79. }
  80. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  81. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  82. }
  83. }
  84. }