UriComparator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use Psr\Http\Message\UriInterface;
  5. /**
  6. * Provides methods to determine if a modified URL should be considered cross-origin.
  7. *
  8. * @author Graham Campbell
  9. */
  10. final class UriComparator
  11. {
  12. /**
  13. * Determines if a modified URL should be considered cross-origin with
  14. * respect to an original URL.
  15. */
  16. public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool
  17. {
  18. if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) {
  19. return true;
  20. }
  21. if ($original->getScheme() !== $modified->getScheme()) {
  22. return true;
  23. }
  24. if (self::computePort($original) !== self::computePort($modified)) {
  25. return true;
  26. }
  27. return false;
  28. }
  29. private static function computePort(UriInterface $uri): int
  30. {
  31. $port = $uri->getPort();
  32. if (null !== $port) {
  33. return $port;
  34. }
  35. return 'https' === $uri->getScheme() ? 443 : 80;
  36. }
  37. private function __construct()
  38. {
  39. // cannot be instantiated
  40. }
  41. }