PageRenderTime 66ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Validator/Constraints/IpValidator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 108 lines | 61 code | 21 blank | 26 comment | 7 complexity | 7a9353d153be5512f486c8adc0686bfd MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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. /**
  15. * Validates whether a value is a valid IP address
  16. *
  17. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  18. * @author Joseph Bielawski <stloyd@gmail.com>
  19. *
  20. * @api
  21. */
  22. class IpValidator extends ConstraintValidator
  23. {
  24. /**
  25. * Checks if the passed value is valid.
  26. *
  27. * @param mixed $value The value that should be validated
  28. * @param Constraint $constraint The constraint for the validation
  29. *
  30. * @return Boolean Whether or not the value is valid
  31. *
  32. * @api
  33. */
  34. public function isValid($value, Constraint $constraint)
  35. {
  36. if (null === $value || '' === $value) {
  37. return true;
  38. }
  39. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  40. throw new UnexpectedTypeException($value, 'string');
  41. }
  42. $value = (string) $value;
  43. switch ($constraint->version) {
  44. case Ip::V4:
  45. $flag = FILTER_FLAG_IPV4;
  46. break;
  47. case Ip::V6:
  48. $flag = FILTER_FLAG_IPV6;
  49. break;
  50. case Ip::V4_NO_PRIV:
  51. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
  52. break;
  53. case Ip::V6_NO_PRIV:
  54. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
  55. break;
  56. case Ip::ALL_NO_PRIV:
  57. $flag = FILTER_FLAG_NO_PRIV_RANGE;
  58. break;
  59. case Ip::V4_NO_RES:
  60. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
  61. break;
  62. case Ip::V6_NO_RES:
  63. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
  64. break;
  65. case Ip::ALL_NO_RES:
  66. $flag = FILTER_FLAG_NO_RES_RANGE;
  67. break;
  68. case Ip::V4_ONLY_PUBLIC:
  69. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  70. break;
  71. case Ip::V6_ONLY_PUBLIC:
  72. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  73. break;
  74. case Ip::ALL_ONLY_PUBLIC:
  75. $flag = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  76. break;
  77. default:
  78. $flag = null;
  79. break;
  80. }
  81. if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
  82. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  83. return false;
  84. }
  85. return true;
  86. }
  87. }