Test Failed
Push — master ( af60fe...667b83 )
by Divine Niiquaye
10:48
created

GeneratedUri   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 47
c 3
b 0
f 0
dl 0
loc 130
ccs 29
cts 29
cp 1
rs 10
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 13 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 17
    private ?string $scheme = null, $host = null, $port = null;
60
61 17
    public function __construct(string $pathInfo, int $referenceType)
62 17
    {
63
        $this->pathInfo = $pathInfo;
64
        $this->referenceType = $referenceType;
65
    }
66
67
    /**
68 17
     * {@inheritdoc}
69
     */
70 17
    public function __toString()
71 17
    {
72
        $prefixed = '/';
73 17
        $type = $this->referenceType;
74 4
75
        if ($this->scheme) {
76
            $prefixed = $this->scheme . '://';
77 17
        }
78 7
79 4
        if ($this->host) {
80
            if ('/' === $prefixed) {
81
                $prefixed = \in_array($type, [self::ABSOLUTE_URL, self::NETWORK_PATH], true) ? '//' : '';
82 7
            }
83 11
84 5
            $prefixed .= \ltrim($this->host, './') . $this->port . '/';
85
        } elseif ('/' === $prefixed && self::RELATIVE_PATH === $type) {
86
            $prefixed = '.' . $prefixed;
87 17
        }
88
89
        return $prefixed . \ltrim($this->pathInfo, '/');
90
    }
91
92
    /**
93 7
     * 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
        return $this;
100
    }
101
102
    /**
103 4
     * Set the scheme component of the URI.
104
     */
105 4
    public function withScheme(string $scheme): self
106
    {
107 4
        $this->scheme = $scheme;
108
109
        return $this;
110
    }
111
112
    /**
113 1
     * 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
            throw new UrlGenerationException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port));
119
        }
120
121
        if (!\in_array($port, ['', 80, 443], true)) {
122
            $this->port = ':' . $port;
123
        }
124
125 1
        return $this;
126
    }
127
128 1
    /**
129 1
     * Set the query component of the URI.
130
     *
131 1
     * @param array<int|string,int|string> $queryParams
132
     */
133
    public function withQuery(array $queryParams = []): self
134 1
    {
135
        // Incase query is added to uri.
136
        if ([] !== $queryParams) {
137
            $queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986);
138
139
            if (!empty($queryString)) {
140
                $this->pathInfo .= '?' . \strtr($queryString, self::QUERY_DECODED);
141
            }
142
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * Set the fragment component of the URI
150
     */
151
    public function withFragment(string $fragment): self
152
    {
153
        if (!empty($fragment)) {
154
            $this->pathInfo .= '#' . $fragment;
155
        }
156
157
        return $this;
158
    }
159
}
160