AllowedProtocols.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Protocol validator
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Validator;
  9. use \Zend\Uri\Uri;
  10. /**
  11. * Check is URI starts from allowed protocol
  12. *
  13. * Class AllowedProtocols
  14. * @package Magento\Framework\Validator
  15. */
  16. class AllowedProtocols extends AbstractValidator
  17. {
  18. /**
  19. * List of supported protocols
  20. *
  21. * @var array
  22. */
  23. private $listOfProtocols = [
  24. 'http',
  25. 'https',
  26. ];
  27. /**
  28. * Constructor.
  29. * @param array $listOfProtocols
  30. */
  31. public function __construct($listOfProtocols = [])
  32. {
  33. if (count($listOfProtocols)) {
  34. $this->listOfProtocols = $listOfProtocols;
  35. }
  36. }
  37. /**
  38. * Validate URI
  39. *
  40. * @param string $value
  41. * @return bool
  42. */
  43. public function isValid($value)
  44. {
  45. $uri = new Uri($value);
  46. $isValid = in_array(
  47. strtolower($uri->getScheme()),
  48. $this->listOfProtocols
  49. );
  50. if (!$isValid) {
  51. $this->_addMessages(["Protocol isn't allowed"]);
  52. }
  53. return $isValid;
  54. }
  55. }