PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/UrlValidator.php

https://bitbucket.org/ingsol/music_sonata
PHP | 70 lines | 38 code | 11 blank | 21 comment | 6 complexity | 02a65de84d9a54024a27c9666af0c6ef MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, 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. * @api
  16. */
  17. class UrlValidator extends ConstraintValidator
  18. {
  19. const PATTERN = '~^
  20. (%s):// # protocol
  21. (
  22. ([\pL\pN\pS-]+\.)+[\pL]+ # a domain name
  23. | # or
  24. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
  25. | # or
  26. \[
  27. (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})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1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
  28. \] # a IPv6 address
  29. )
  30. (:[0-9]+)? # a port (optional)
  31. (/?|/\S+) # a /, nothing or a / with something
  32. $~ixu';
  33. /**
  34. * Checks if the passed value is valid.
  35. *
  36. * @param mixed $value The value that should be validated
  37. * @param Constraint $constraint The constraint for the validation
  38. *
  39. * @return Boolean Whether or not the value is valid
  40. *
  41. * @api
  42. */
  43. public function isValid($value, Constraint $constraint)
  44. {
  45. if (null === $value || '' === $value) {
  46. return true;
  47. }
  48. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  49. throw new UnexpectedTypeException($value, 'string');
  50. }
  51. $value = (string) $value;
  52. $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
  53. if (!preg_match($pattern, $value)) {
  54. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  55. return false;
  56. }
  57. return true;
  58. }
  59. }