vendor/guzzlehttp/psr7/src/UriComparator.php

60 lines · 1278 bytes

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