Passed
Push — master ( 67a6c5...5b4678 )
by Divine Niiquaye
24:01 queued 08:03
created

GeneratedUri::withPort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
/**
21
 * A generated URI from route made up of only the
22
 * URIs path component (pathinfo, scheme, host, and maybe port.) starting with a slash.
23
 *
24
 * @author Divine Niiquaye Ibok <[email protected]>
25
 */
26
class GeneratedUri implements \Stringable
27
{
28
    /** Generates an absolute URL, e.g. "http://example.com/dir/file". */
29
    public const ABSOLUTE_URL = 0;
30
31
    /** Generates an absolute path, e.g. "/dir/file". */
32
    public const ABSOLUTE_PATH = 1;
33
34
    /** Generates a path with beginning with a single dot, e.g. "./file". */
35
    public const RELATIVE_PATH = 2;
36
37
    /** Generates a network path, e.g. "//example.com/dir/file". */
38
    public const NETWORK_PATH = 3;
39
40
    /** Adopted from symfony's routing component: Symfony\Component\Routing\Generator::QUERY_FRAGMENT_DECODED */
41
    private const QUERY_DECODED = [
42
        // RFC 3986 explicitly allows those in the query to reference other URIs unencoded
43
        '%2F' => '/',
44
        '%3F' => '?',
45
        // reserved chars that have no special meaning for HTTP URIs in a query
46
        // this excludes esp. "&", "=" and also "+" because PHP would treat it as a space (form-encoded)
47
        '%40' => '@',
48
        '%3A' => ':',
49
        '%21' => '!',
50
        '%3B' => ';',
51
        '%2C' => ',',
52
        '%2A' => '*',
53
    ];
54
55
    private string $pathInfo;
56
    private int $referenceType;
57
    private ?string $scheme = null, $host = null, $port = null;
58
59 17
    public function __construct(string $pathInfo, int $referenceType)
60
    {
61 17
        $this->pathInfo = $pathInfo;
62 17
        $this->referenceType = $referenceType;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 17
    public function __toString()
69
    {
70 17
        $prefixed = '/';
71 17
        $type = $this->referenceType;
72
73 17
        if ($this->scheme) {
74 4
            $prefixed = $this->scheme . '://';
75
        }
76
77 17
        if ($this->host) {
78 7
            if ('/' === $prefixed) {
79 4
                $prefixed = \in_array($type, [self::ABSOLUTE_URL, self::NETWORK_PATH], true) ? '//' : '';
80
            }
81
82 7
            $prefixed .= \ltrim($this->host, './') . $this->port . '/';
83 11
        } elseif ('/' === $prefixed && self::RELATIVE_PATH === $type) {
84 5
            $prefixed = '.' . $prefixed;
85
        }
86
87 17
        return $prefixed . \ltrim($this->pathInfo, '/');
88
    }
89
90
    /**
91
     * Set the host component of the URI, may include port too.
92
     */
93 7
    public function withHost(string $host): self
94
    {
95 7
        $this->host = $host;
96
97 7
        return $this;
98
    }
99
100
    /**
101
     * Set the scheme component of the URI.
102
     */
103 4
    public function withScheme(string $scheme): self
104
    {
105 4
        $this->scheme = $scheme;
106
107 4
        return $this;
108
    }
109
110
    /**
111
     * Sets the port component of the URI.
112
     */
113 1
    public function withPort(string $port): self
114
    {
115 1
        $this->port = \in_array($port, ['', 80, 443], true) ? null : ':' . $port;
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * Set the query component of the URI.
122
     *
123
     * @param array<int|string,int|string> $queryParams
124
     */
125 1
    public function withQuery(array $queryParams = []): self
126
    {
127
        // Incase query is added to uri.
128 1
        if ([] !== $queryParams) {
129 1
            $queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986);
130
131 1
            $this->pathInfo .= '?' . \strtr($queryString, self::QUERY_DECODED);
132
        }
133
134 1
        return $this;
135
    }
136
}
137