1 | <?php |
||
9 | class Url implements ValueObjectInterface |
||
10 | { |
||
11 | /** @var SchemeName */ |
||
12 | protected $scheme; |
||
13 | |||
14 | /** @var StringLiteral */ |
||
15 | protected $user; |
||
16 | |||
17 | /** @var StringLiteral */ |
||
18 | protected $password; |
||
19 | |||
20 | /** @var Domain */ |
||
21 | protected $domain; |
||
22 | |||
23 | /** @var Path */ |
||
24 | protected $path; |
||
25 | |||
26 | /** @var PortNumber */ |
||
27 | protected $port; |
||
28 | |||
29 | /** @var QueryString */ |
||
30 | protected $queryString; |
||
31 | |||
32 | /** @var FragmentIdentifier */ |
||
33 | protected $fragmentIdentifier; |
||
34 | |||
35 | /** |
||
36 | * Returns a new Url object from a native url string |
||
37 | * |
||
38 | * @param $url_string |
||
39 | * @return Url |
||
40 | */ |
||
41 | 2 | public static function fromNative() |
|
42 | { |
||
43 | 2 | $urlString = \func_get_arg(0); |
|
44 | |||
45 | 2 | $user = \parse_url($urlString, PHP_URL_USER); |
|
46 | 2 | $pass = \parse_url($urlString, PHP_URL_PASS); |
|
47 | 2 | $host = \parse_url($urlString, PHP_URL_HOST); |
|
48 | 2 | $queryString = \parse_url($urlString, PHP_URL_QUERY); |
|
49 | 2 | $fragmentId = \parse_url($urlString, PHP_URL_FRAGMENT); |
|
50 | 2 | $port = \parse_url($urlString, PHP_URL_PORT); |
|
51 | |||
52 | 2 | $scheme = new SchemeName(\parse_url($urlString, PHP_URL_SCHEME)); |
|
|
|||
53 | 2 | $user = $user ? new StringLiteral($user) : new StringLiteral(''); |
|
54 | 2 | $pass = $pass ? new StringLiteral($pass) : new StringLiteral(''); |
|
55 | 2 | $domain = Domain::specifyType($host); |
|
56 | 2 | $path = new Path(\parse_url($urlString, PHP_URL_PATH)); |
|
57 | 2 | $portNumber = $port ? new PortNumber($port) : new NullPortNumber(); |
|
58 | 2 | $query = $queryString ? new QueryString(\sprintf('?%s', $queryString)) : new NullQueryString(); |
|
59 | 2 | $fragment = $fragmentId ? new FragmentIdentifier(\sprintf('#%s', $fragmentId)) : new NullFragmentIdentifier(); |
|
60 | |||
61 | 2 | return new self($scheme, $user, $pass, $domain, $portNumber, $path, $query, $fragment); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns a new Url object |
||
66 | * |
||
67 | * @param SchemeName $scheme |
||
68 | * @param String $user |
||
69 | * @param String $password |
||
70 | * @param Domain $domain |
||
71 | * @param Path $path |
||
72 | * @param PortNumberInterface $port |
||
73 | * @param QueryString $query |
||
74 | * @param FragmentIdentifier $fragment |
||
75 | */ |
||
76 | 13 | public function __construct(SchemeName $scheme, StringLiteral $user, StringLiteral $password, Domain $domain, PortNumberInterface $port, Path $path, QueryString $query, FragmentIdentifier $fragment) |
|
77 | { |
||
78 | 13 | $this->scheme = $scheme; |
|
79 | 13 | $this->user = $user; |
|
80 | 13 | $this->password = $password; |
|
81 | 13 | $this->domain = $domain; |
|
82 | 13 | $this->path = $path; |
|
83 | 13 | $this->port = $port; |
|
84 | 13 | $this->queryString = $query; |
|
85 | 13 | $this->fragmentIdentifier = $fragment; |
|
86 | 13 | } |
|
87 | |||
88 | /** |
||
89 | * Tells whether two Url are sameValueAs by comparing their components |
||
90 | * |
||
91 | * @param ValueObjectInterface $url |
||
92 | * @return bool |
||
93 | */ |
||
94 | 2 | public function sameValueAs(ValueObjectInterface $url) |
|
95 | { |
||
96 | 2 | if (false === Util::classEquals($this, $url)) { |
|
97 | 1 | return false; |
|
98 | } |
||
99 | |||
100 | 2 | return $this->getScheme()->sameValueAs($url->getScheme()) && |
|
101 | 2 | $this->getUser()->sameValueAs($url->getUser()) && |
|
102 | 2 | $this->getPassword()->sameValueAs($url->getPassword()) && |
|
103 | 2 | $this->getDomain()->sameValueAs($url->getDomain()) && |
|
104 | 2 | $this->getPath()->sameValueAs($url->getPath()) && |
|
105 | 2 | $this->getPort()->sameValueAs($url->getPort()) && |
|
106 | 2 | $this->getQueryString()->sameValueAs($url->getQueryString()) && |
|
107 | 2 | $this->getFragmentIdentifier()->sameValueAs($url->getFragmentIdentifier()) |
|
108 | 2 | ; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns the domain of the Url |
||
113 | * |
||
114 | * @return Hostname|IPAddress |
||
115 | */ |
||
116 | 6 | public function getDomain() |
|
120 | |||
121 | /** |
||
122 | * Returns the fragment identifier of the Url |
||
123 | * |
||
124 | * @return FragmentIdentifier |
||
125 | */ |
||
126 | 6 | public function getFragmentIdentifier() |
|
130 | |||
131 | /** |
||
132 | * Returns the password part of the Url |
||
133 | * |
||
134 | * @return String |
||
135 | */ |
||
136 | 5 | public function getPassword() |
|
140 | |||
141 | /** |
||
142 | * Returns the path of the Url |
||
143 | * |
||
144 | * @return Path |
||
145 | */ |
||
146 | 6 | public function getPath() |
|
150 | |||
151 | /** |
||
152 | * Returns the port of the Url |
||
153 | * |
||
154 | * @return PortNumberInterface |
||
155 | */ |
||
156 | 6 | public function getPort() |
|
160 | |||
161 | /** |
||
162 | * Returns the query string of the Url |
||
163 | * |
||
164 | * @return QueryString |
||
165 | */ |
||
166 | 6 | public function getQueryString() |
|
170 | |||
171 | /** |
||
172 | * Returns the scheme of the Url |
||
173 | * |
||
174 | * @return SchemeName |
||
175 | */ |
||
176 | 6 | public function getScheme() |
|
180 | |||
181 | /** |
||
182 | * Returns the user part of the Url |
||
183 | * |
||
184 | * @return String |
||
185 | */ |
||
186 | 6 | public function getUser() |
|
190 | |||
191 | /** |
||
192 | * Returns a string representation of the url |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | 4 | public function __toString() |
|
216 | |||
217 | function jsonSerialize() |
||
218 | { |
||
219 | return [ |
||
231 | |||
232 | |||
233 | } |
||
234 |