PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/validator/sfValidatorUrl.class.php

https://github.com/bheneka/gitta
PHP | 59 lines | 24 code | 5 blank | 30 comment | 0 complexity | 6b9cdf1a45b8a44a1c65d5dff655478f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorUrl validates Urls.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id$
  16. */
  17. class sfValidatorUrl extends sfValidatorRegex
  18. {
  19. const REGEX_URL_FORMAT = '~^
  20. (%s):// # protocol
  21. (
  22. ([a-z0-9-]+\.)+[a-z]{2,6} # a domain name
  23. | # or
  24. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
  25. )
  26. (:[0-9]+)? # a port (optional)
  27. (/?|/\S+) # a /, nothing or a / with something
  28. $~ix';
  29. /**
  30. * Available options:
  31. *
  32. * * protocols: An array of acceptable URL protocols (http, https, ftp and ftps by default)
  33. *
  34. * @param array $options An array of options
  35. * @param array $messages An array of error messages
  36. *
  37. * @see sfValidatorRegex
  38. */
  39. protected function configure($options = array(), $messages = array())
  40. {
  41. parent::configure($options, $messages);
  42. $this->addOption('protocols', array('http', 'https', 'ftp', 'ftps'));
  43. $this->setOption('pattern', new sfCallable(array($this, 'generateRegex')));
  44. }
  45. /**
  46. * Generates the current validator's regular expression.
  47. *
  48. * @return string
  49. */
  50. public function generateRegex()
  51. {
  52. return sprintf(self::REGEX_URL_FORMAT, implode('|', $this->getOption('protocols')));
  53. }
  54. }