Test Failed
Branch master (effa58)
by Divine Niiquaye
02:13
created

GeneratedUri   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withScheme() 0 5 2
A __toString() 0 17 5
A withQuery() 0 10 2
A __construct() 0 3 1
A withHost() 0 5 2
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
    /** 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 string $pathInfo;
45
46
    private ?string $scheme = null;
47
48
    private ?string $host = null;
49
50
    public function __construct(string $pathInfo)
51
    {
52
        $this->pathInfo = $pathInfo;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function __toString()
59
    {
60
        $prefixed = $this->scheme ?? '';
61
62
        if (null !== $this->host) {
63
            $prefixed .= !empty($prefixed) ? $this->host : \substr($this->host, 2);
64
        }
65
66
        if (empty($prefixed)) {
67
            $prefixed .= '.'; // Append missing "." at the beginning of the $uri.
68
        }
69
70
        if ('/' !== @$this->pathInfo[0]) {
71
            $prefixed .= '/';
72
        }
73
74
        return $prefixed . $this->pathInfo;
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 ? '//' . \ltrim($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