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

GeneratedUri::withQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
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;
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 = null;
48
49
    /** @var string|null */
50
    private $host = null;
51
52
    public function __construct(string $pathInfo)
53
    {
54
        $this->pathInfo = $pathInfo;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function __toString()
61
    {
62
        $uriRoute = $this->scheme . $this->host . $this->pathInfo;
63
64
        if (!\str_contains($uriRoute, '://')) {
65
            $prefix = '.'; // Append missing "." at the beginning of the $uri.
66
67
            if ('/' !== @$uriRoute[0]) {
68
                $prefix .= '/';
69
            }
70
71
            $uriRoute = $prefix . $uriRoute;
72
        }
73
74
        return $uriRoute;
75
    }
76
77
    /**
78
     * Set the host component of the URI, may include port too.
79
     */
80
    public function withHost(string $host): self
81
    {
82
        $this->host = '' !== $host ? '//' . $host : null;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Set the scheme component of the URI.
89
     */
90
    public function withScheme(string $scheme): self
91
    {
92
        $this->scheme = '' !== $scheme ? $scheme . ':' : null;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Set the query component of the URI.
99
     *
100
     * @param array<int|string,int|string> $queryParams
101
     */
102
    public function withQuery(array $queryParams = []): self
103
    {
104
        // Incase query is added to uri.
105
        if ([] !== $queryParams) {
106
            $queryString = \http_build_query($queryParams, '', '&', \PHP_QUERY_RFC3986);
107
108
            $this->pathInfo .= '?' . \strtr($queryString, self::QUERY_DECODED);
109
        }
110
111
        return $this;
112
    }
113
}
114