Ip.php 2.0 KB

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