AssemblyResult   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 21
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 104
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F generateUri() 0 39 21
1
<?php
2
/**
3
 * Dash
4
 *
5
 * @link      http://github.com/DASPRiD/Dash For the canonical source repository
6
 * @copyright 2013-2015 Ben Scholzen 'DASPRiD'
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Dash\Route;
11
12
/**
13
 * A generic assembly result which is returned by assemble methods.
14
 *
15
 * {@internal We are not using Zend\Uri for performance reasons. All the
16
 * normalization going on in Zend\Uri have a huge performance impact and are not
17
 * required for simple assembling.
18
 *
19
 * There is no validation done in this class, as it is assumed that someone who
20
 * writes custom route classes actually knows what one is doing. We only take
21
 * care of required encoding here.
22
 *
23
 * Seriously, consider twice before tweaking this class, as it is quite
24
 * performance-critical to assembling. Any changes should be backed by proper
25
 * benchmarks.}}
26
 */
27
class AssemblyResult
28
{
29
    /**
30
     * These characters are allowed within a path and should not be encoded.
31
     *
32
     * @var array
33
     */
34
    protected static $allowedPathChars = [
35
        '%2F' => '/',
36
        '%40' => '@',
37
        '%3A' => ':',
38
        '%3B' => ';',
39
        '%2C' => ',',
40
        '%3D' => '=',
41
        '%2B' => '+',
42
        '%21' => '!',
43
        '%2A' => '*',
44
        '%7C' => '|',
45
    ];
46
47
    /**
48
     * @var null|string
49
     */
50
    public $scheme;
51
52
    /**
53
     * @var null|string
54
     */
55
    public $host;
56
57
    /**
58
     * @var null|int
59
     */
60
    public $port;
61
62
    /**
63
     * @var null|string
64
     */
65
    public $path;
66
67
    /**
68
     * @var null|array
69
     */
70
    public $query;
71
72
    /**
73
     * @var null|string
74
     */
75
    public $fragment;
76
77
    /**
78
     * Converts the assembly result to a string.
79
     *
80
     * If $forceCanonical is set to false, a scheme or host is only prepended
81
     * to the result if they differ from the references. In case $forceCanonical
82
     * is set to true, either the set scheme and host will be used, or if not
83
     * set, the reference values.
84
     *
85
     * @param  string $referenceScheme
86
     * @param  string $referenceHost
87
     * @param  int    $referencePort
88
     * @param  bool   $forceCanonical
89
     * @return string
90
     */
91
    public function generateUri($referenceScheme, $referenceHost, $referencePort, $forceCanonical)
92
    {
93
        $url    = '';
94
        $scheme = $referenceScheme;
95
        $port   = $this->port;
96
97
        if ($forceCanonical || $this->scheme !== null && $referenceScheme !== $this->scheme) {
98
            $url .= ($scheme = $this->scheme ?: $referenceScheme) . ':';
99
            $forceCanonical = true;
100
        }
101
102
        if (null === $this->port) {
103
            if ($scheme === $referenceScheme && (null === $this->host || $this->host === $referenceHost)) {
104
                $port = $referencePort;
105
            } else {
106
                $port = 'http' === $scheme ? 80 : 443;
107
            }
108
        }
109
110
        if ($forceCanonical || $this->host !== null && $referenceHost !== $this->host || $port !== $referencePort) {
111
            $url .= '//' . ($this->host ?: $referenceHost);
112
113
            if ('http' === $scheme && 80 !== $port || 'https' === $scheme && 443 !== $port) {
114
                $url .= ':' . $port;
115
            }
116
        }
117
118
        $url .= strtr(rawurlencode($this->path), static::$allowedPathChars);
119
120
        if ($this->query !== null) {
121
            $url .= '?' . http_build_query($this->query, '', '&');
122
        }
123
124
        if ($this->fragment !== null) {
125
            $url .= '#' . $this->fragment;
126
        }
127
128
        return $url;
129
    }
130
}
131