Ip.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  14. * Validates that a value is a valid IP address.
  15. *
  16. * @Annotation
  17. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. * @author Joseph Bielawski <stloyd@gmail.com>
  21. */
  22. class Ip extends Constraint
  23. {
  24. const V4 = '4';
  25. const V6 = '6';
  26. const ALL = 'all';
  27. // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
  28. const V4_NO_PRIV = '4_no_priv';
  29. const V6_NO_PRIV = '6_no_priv';
  30. const ALL_NO_PRIV = 'all_no_priv';
  31. // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
  32. const V4_NO_RES = '4_no_res';
  33. const V6_NO_RES = '6_no_res';
  34. const ALL_NO_RES = 'all_no_res';
  35. // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
  36. const V4_ONLY_PUBLIC = '4_public';
  37. const V6_ONLY_PUBLIC = '6_public';
  38. const ALL_ONLY_PUBLIC = 'all_public';
  39. const INVALID_IP_ERROR = 'b1b427ae-9f6f-41b0-aa9b-84511fbb3c5b';
  40. protected static $versions = array(
  41. self::V4,
  42. self::V6,
  43. self::ALL,
  44. self::V4_NO_PRIV,
  45. self::V6_NO_PRIV,
  46. self::ALL_NO_PRIV,
  47. self::V4_NO_RES,
  48. self::V6_NO_RES,
  49. self::ALL_NO_RES,
  50. self::V4_ONLY_PUBLIC,
  51. self::V6_ONLY_PUBLIC,
  52. self::ALL_ONLY_PUBLIC,
  53. );
  54. protected static $errorNames = array(
  55. self::INVALID_IP_ERROR => 'INVALID_IP_ERROR',
  56. );
  57. public $version = self::V4;
  58. public $message = 'This is not a valid IP address.';
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function __construct($options = null)
  63. {
  64. parent::__construct($options);
  65. if (!\in_array($this->version, self::$versions)) {
  66. throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
  67. }
  68. }
  69. }