Validator.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Validate URL
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Url;
  12. class Validator extends \Zend_Validate_Abstract
  13. {
  14. /**#@+
  15. * Error keys
  16. */
  17. const INVALID_URL = 'invalidUrl';
  18. /**#@-*/
  19. /**
  20. * @var \Zend\Validator\Uri
  21. */
  22. private $validator;
  23. /**
  24. * Object constructor
  25. */
  26. public function __construct(\Zend\Validator\Uri $validator)
  27. {
  28. // set translated message template
  29. $this->setMessage((string)new \Magento\Framework\Phrase("Invalid URL '%value%'."), self::INVALID_URL);
  30. $this->validator = $validator;
  31. $this->validator->setAllowRelative(false);
  32. }
  33. /**
  34. * Validation failure message template definitions
  35. *
  36. * @var array
  37. */
  38. protected $_messageTemplates = [self::INVALID_URL => "Invalid URL '%value%'."];
  39. /**
  40. * Validate value
  41. *
  42. * @param string $value
  43. * @return bool
  44. */
  45. public function isValid($value)
  46. {
  47. $this->_setValue($value);
  48. $valid = $this->validator->isValid($value);
  49. if (!$valid) {
  50. $this->_error(self::INVALID_URL);
  51. }
  52. return $valid;
  53. }
  54. }