Uri::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 5
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Http;
26
27
use Brickoo\Component\Common\Assert;
28
29
/**
30
 * Uri
31
 *
32
 * Implements an uniform resource identifier.
33
 * @author Celestino Diaz <[email protected]>
34
 */
35
class Uri {
36
37
    /** @var string */
38
    private $scheme;
39
40
    /** @var \Brickoo\Component\Http\UriAuthority */
41
    private $authority;
42
43
    /** @var string */
44
    private $path;
45
46
    /** @var \Brickoo\Component\Http\UriQuery */
47
    private $query;
48
49
    /** @var string */
50
    private $fragment;
51
52
    /**
53
     * Class constructor.
54
     * @param string $scheme the uri protocol scheme
55
     * @param \Brickoo\Component\Http\UriAuthority $authority
56
     * @param string $path the uri path
57
     * @param \Brickoo\Component\Http\UriQuery $query
58
     * @param string $fragment
59
     */
60 4
    public function __construct($scheme, UriAuthority $authority, $path, UriQuery $query, $fragment) {
61 4
        Assert::isString($scheme);
62 3
        Assert::isString($path);
63 2
        Assert::isString($fragment);
64
65 1
        $this->scheme = $scheme;
66 1
        $this->authority = $authority;
67 1
        $this->path = $path;
68 1
        $this->query = $query;
69 1
        $this->fragment = $fragment;
70 1
    }
71
72
    /**
73
     * Returns the request uri protocol scheme.
74
     * @return string the uri scheme
75
     */
76 1
    public function getScheme() {
77 1
        return $this->scheme;
78
    }
79
80
    /**
81
     * Returns the uri authority component.
82
     * @return \Brickoo\Component\Http\UriAuthority
83
     */
84 1
    public function getAuthority() {
85 1
        return $this->authority;
86
    }
87
88
    /**
89
     * Returns the uri hostname.
90
     * @return string the hostname
91
     */
92 1
    public function getHostname() {
93 1
        return $this->authority->getHostname();
94
    }
95
96
    /**
97
     * Returns the uri path.
98
     * @return string the uri path
99
     */
100 1
    public function getPath() {
101 1
        return $this->path;
102
    }
103
104
    /**
105
     * Returns the uri query component.
106
     * @return \Brickoo\Component\Http\UriQuery
107
     */
108 1
    public function getQuery() {
109 1
        return $this->query;
110
    }
111
112
    /**
113
     * Returns the uri fragment.
114
     * @return string the uri fragment
115
     */
116 1
    public function getFragment() {
117 1
        return $this->fragment;
118
    }
119
120
    /**
121
     * Returns the string representation of the uri.
122
     * @return string the uri representation
123
     */
124 1
    public function toString() {
125 1
        $uriParts = sprintf("%s://%s", $this->getScheme(), $this->getAuthority()->toString());
126
127 1
        if ($queryString = $this->getQuery()->toString()) {
128 1
            $queryString = "?".$queryString;
129 1
        }
130
131 1
        if ($fragment = $this->getFragment()) {
132 1
            $fragment = "#".$fragment;
133 1
        }
134
135 1
        return  $uriParts.$this->getPath().$queryString.$fragment;
136
    }
137
138
 }
139