Passed
Push — master ( 667b83...7ca373 )
by Divine Niiquaye
02:38
created

GeneratedUri   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 89.19%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 129
ccs 33
cts 37
cp 0.8919
rs 10
c 3
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A withFragment() 0 7 2
A withScheme() 0 5 1
B __toString() 0 20 7
A withPort() 0 11 4
A withQuery() 0 12 3
A __construct() 0 4 1
A withHost() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Generator;
19
20
use Flight\Routing\Exceptions\UrlGenerationException;
21
22
/**
23
 * A generated URI from route made up of only the
24
 * URIs path component (pathinfo, scheme, host, and maybe port.) starting with a slash.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class GeneratedUri implements \Stringable
29
{
30
    /** Generates an absolute URL, e.g. "http://example.com/dir/file". */
31
    public const ABSOLUTE_URL = 0;
32
33
    /** Generates an absolute path, e.g. "/dir/file". */
34
    public const ABSOLUTE_PATH = 1;
35
36
    /** Generates a path with beginning with a single dot, e.g. "./file". */
37
    public const RELATIVE_PATH = 2;
38
39
    /** Generates a network path, e.g. "//example.com/dir/file". */
40
    public const NETWORK_PATH = 3;
41
42
    /** Adopted from symfony's routing component: Symfony\Component\Routing\Generator::QUERY_FRAGMENT_DECODED */
43
    private const QUERY_DECODED = [
44
        // RFC 3986 explicitly allows those in the query to reference other URIs unencoded
45
        '%2F' => '/',
46
        '%3F' => '?',
47
        // reserved chars that have no special meaning for HTTP URIs in a query
48
        // this excludes esp. "&", "=" and also "+" because PHP would treat it as a space (form-encoded)
49
        '%40' => '@',
50
        '%3A' => ':',
51
        '%21' => '!',
52
        '%3B' => ';',
53
        '%2C' => ',',
54
        '%2A' => '*',
55
    ];
56
57
    private string $pathInfo;
58
    private int $referenceType;
59
    private ?string $scheme = null, $host = null, $port = null;
60
61 17
    public function __construct(string $pathInfo, int $referenceType)
62
    {
63 17
        $this->pathInfo = $pathInfo;
64 17
        $this->referenceType = $referenceType;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 17
    public function __toString()
71
    {
72 17
        $prefixed = '/';
73 17
        $type = $this->referenceType;
74
75 17
        if ($this->scheme) {
76 4
            $prefixed = $this->scheme . '://';
77
        }
78
79 17
        if ($this->host) {
80 7
            if ('/' === $prefixed) {
81 4
                $prefixed = \in_array($type, [self::ABSOLUTE_URL, self::NETWORK_PATH], true) ? '//' : '';
82
            }
83
84 7
            $prefixed .= \ltrim($this->host, './') . $this->port . '/';
85 11
        } elseif ('/' === $prefixed && self::RELATIVE_PATH === $type) {
86 5
            $prefixed = '.' . $prefixed;
87
        }
88
89 17
        return $prefixed . \ltrim($this->pathInfo, '/');
90
    }
91
92
    /**
93
     * Set the host component of the URI, may include port too.
94
     */
95 7
    public function withHost(string $host): self
96
    {
97 7
        $this->host = $host;
98
99 7
        return $this;
100
    }
101
102
    /**
103
     * Set the scheme component of the URI.
104
     */
105 4
    public function withScheme(string $scheme): self
106
    {
107 4
        $this->scheme = $scheme;
108
109 4
        return $this;
110
    }
111
112
    /**
113
     * Sets the port component of the URI.
114
     */
115 1
    public function withPort(int $port): self
116
    {
117 1
        if (0 > $port || 0xffff < $port) {
118 1
            throw new UrlGenerationException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port));
119
        }
120
121 1
        if (!\in_array($port, ['', 80, 443], true)) {
122 1
            $this->port = ':' . $port;
123
        }
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * Set the query component of the URI.
130
     *
131
     * @param array<int|string,int|string> $queryParams
132
     */
133 1
    public function withQuery(array $queryParams = []): self
134
    {
135
        // Incase query is added to uri.
136 1
        if ([] !== $queryParams) {
137 1
            $queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986);
138
139 1
            if (!empty($queryString)) {
140 1
                $this->pathInfo .= '?' . \strtr($queryString, self::QUERY_DECODED);
141
            }
142
        }
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * Set the fragment component of the URI
149
     */
150
    public function withFragment(string $fragment): self
151
    {
152
        if (!empty($fragment)) {
153
            $this->pathInfo .= '#' . $fragment;
154
        }
155
156
        return $this;
157
    }
158
}
159