Url.php 855 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Validator;
  7. /**
  8. * Class Url validates URL and checks that it has allowed scheme
  9. */
  10. class Url
  11. {
  12. /**
  13. * Validate URL and check that it has allowed scheme
  14. *
  15. * @param string $value
  16. * @param array $allowedSchemes
  17. * @return bool
  18. */
  19. public function isValid($value, array $allowedSchemes = [])
  20. {
  21. $isValid = true;
  22. if (!filter_var($value, FILTER_VALIDATE_URL)) {
  23. $isValid = false;
  24. }
  25. if ($isValid && !empty($allowedSchemes)) {
  26. $url = parse_url($value);
  27. if (empty($url['scheme']) || !in_array($url['scheme'], $allowedSchemes)) {
  28. $isValid = false;
  29. }
  30. }
  31. return $isValid;
  32. }
  33. }