IpValidator.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16. * Validates whether a value is a valid IP address.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. * @author Joseph Bielawski <stloyd@gmail.com>
  20. */
  21. class IpValidator extends ConstraintValidator
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function validate($value, Constraint $constraint)
  27. {
  28. if (!$constraint instanceof Ip) {
  29. throw new UnexpectedTypeException($constraint, Ip::class);
  30. }
  31. if (null === $value || '' === $value) {
  32. return;
  33. }
  34. if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  35. throw new UnexpectedValueException($value, 'string');
  36. }
  37. $value = (string) $value;
  38. if (null !== $constraint->normalizer) {
  39. $value = ($constraint->normalizer)($value);
  40. }
  41. switch ($constraint->version) {
  42. case Ip::V4:
  43. $flag = \FILTER_FLAG_IPV4;
  44. break;
  45. case Ip::V6:
  46. $flag = \FILTER_FLAG_IPV6;
  47. break;
  48. case Ip::V4_NO_PRIV:
  49. $flag = \FILTER_FLAG_IPV4 | \FILTER_FLAG_NO_PRIV_RANGE;
  50. break;
  51. case Ip::V6_NO_PRIV:
  52. $flag = \FILTER_FLAG_IPV6 | \FILTER_FLAG_NO_PRIV_RANGE;
  53. break;
  54. case Ip::ALL_NO_PRIV:
  55. $flag = \FILTER_FLAG_NO_PRIV_RANGE;
  56. break;
  57. case Ip::V4_NO_RES:
  58. $flag = \FILTER_FLAG_IPV4 | \FILTER_FLAG_NO_RES_RANGE;
  59. break;
  60. case Ip::V6_NO_RES:
  61. $flag = \FILTER_FLAG_IPV6 | \FILTER_FLAG_NO_RES_RANGE;
  62. break;
  63. case Ip::ALL_NO_RES:
  64. $flag = \FILTER_FLAG_NO_RES_RANGE;
  65. break;
  66. case Ip::V4_ONLY_PUBLIC:
  67. $flag = \FILTER_FLAG_IPV4 | \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE;
  68. break;
  69. case Ip::V6_ONLY_PUBLIC:
  70. $flag = \FILTER_FLAG_IPV6 | \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE;
  71. break;
  72. case Ip::ALL_ONLY_PUBLIC:
  73. $flag = \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE;
  74. break;
  75. default:
  76. $flag = null;
  77. break;
  78. }
  79. if (!filter_var($value, \FILTER_VALIDATE_IP, $flag)) {
  80. $this->context->buildViolation($constraint->message)
  81. ->setParameter('{{ value }}', $this->formatValue($value))
  82. ->setCode(Ip::INVALID_IP_ERROR)
  83. ->addViolation();
  84. }
  85. }
  86. }