Test Failed
Pull Request — master (#16)
by Divine Niiquaye
02:34
created

GeneratedUri::withQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
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.1 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
    /** Adopted from symfony's routing component: Symfony\Component\Routing\Generator::QUERY_FRAGMENT_DECODED */
29
    private const QUERY_DECODED = [
30
        // RFC 3986 explicitly allows those in the query to reference other URIs unencoded
31
        '%2F' => '/',
32
        '%3F' => '?',
33
        // reserved chars that have no special meaning for HTTP URIs in a query
34
        // this excludes esp. "&", "=" and also "+" because PHP would treat it as a space (form-encoded)
35
        '%40' => '@',
36
        '%3A' => ':',
37
        '%21' => '!',
38
        '%3B' => ';',
39
        '%2C' => ',',
40
        '%2A' => '*',
41
    ];
42
43
    /** @var string */
44
    private $pathInfo;
45
46
    /** @var string|null */
47
    private $scheme;
48
49
    /** @var string|null */
50
    private $host;
51
52
    public function __construct(string $pathInfo)
53
    {
54
        $this->pathInfo = $pathInfo;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function __toString()
61
    {
62
        $prefixed = $this->scheme ?? '';
63
64
        if (null !== $this->host) {
65
            $prefixed .= !empty($prefixed) ? $this->host : \substr($this->host, 2);
66
        }
67
68
        if (empty($prefixed)) {
69
            $prefixed .= '.'; // Append missing "." at the beginning of the $uri.
70
        }
71
72
        if ('/' !== @$this->pathInfo[0]) {
73
            $prefixed .= '/';
74
        }
75
76
        return $prefixed . $this->pathInfo;
77
    }
78
79
    /**
80
     * Set the host component of the URI, may include port too.
81
     */
82
    public function withHost(string $host): self
83
    {
84
        $this->host = '' !== $host ? '//' . \ltrim($host, './') : null;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Set the scheme component of the URI.
91
     */
92
    public function withScheme(string $scheme): self
93
    {
94
        $this->scheme = '' !== $scheme ? $scheme . ':' : null;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Set the query component of the URI.
101
     *
102
     * @param array<int|string,int|string> $queryParams
103
     */
104
    public function withQuery(array $queryParams = []): self
105
    {
106
        // Incase query is added to uri.
107
        if ([] !== $queryParams) {
108
            $queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986);
109
110
            $this->pathInfo .= '?' . \strtr($queryString, self::QUERY_DECODED);
111
        }
112
113
        return $this;
114
    }
115
}
116