|
| 1 | +# Laravel URI package |
| 2 | + |
| 3 | +[](LICENSE.md) |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +A simple and useful URI package for Laravel framework. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +This package requires PHP 7.2 and Laravel 5.8 or higher. |
| 12 | + |
| 13 | +Install with composer: |
| 14 | + |
| 15 | +``` composer require nsaliu/laravel-uri ``` |
| 16 | + |
| 17 | +## Methods |
| 18 | + |
| 19 | +### createFromString |
| 20 | + |
| 21 | +Creates a new instance with the specified URI. |
| 22 | + |
| 23 | +``` |
| 24 | +$uri = new Uri(); |
| 25 | +$uri->createFromString('https://test.test'); |
| 26 | +``` |
| 27 | + |
| 28 | +### getScheme |
| 29 | + |
| 30 | +Return the scheme part of the URI. |
| 31 | + |
| 32 | +``` |
| 33 | +$uri = new Uri(); |
| 34 | +$uri->createFromString('https://test.test'); |
| 35 | +$uri->getScheme(); |
| 36 | +``` |
| 37 | + |
| 38 | +Return the scheme part of URI: ```https``` |
| 39 | + |
| 40 | +### getUsername |
| 41 | + |
| 42 | +Return the user part from the URI. |
| 43 | + |
| 44 | +``` |
| 45 | +$uri = new Uri(); |
| 46 | +$uri->createFromString('https://fakeuser:fakepass@test.test'); |
| 47 | +$uri->getUsername(); |
| 48 | +``` |
| 49 | + |
| 50 | +If a user is set returns ```fakeuser```, otherwise return an empty string. |
| 51 | + |
| 52 | +### getPassword |
| 53 | + |
| 54 | +Return the pass part from the URI. |
| 55 | + |
| 56 | +``` |
| 57 | +$uri = new Uri(); |
| 58 | +$uri->createFromString('https://fakeuser:fakepass@test.test'); |
| 59 | +$uri->getPassword(); |
| 60 | +``` |
| 61 | + |
| 62 | +If a password is set return ```fakepass```, otherwise return an empty string. |
| 63 | + |
| 64 | +### getAuthority |
| 65 | + |
| 66 | +Return the authority part of the URI. |
| 67 | + |
| 68 | +``` |
| 69 | +$uri = new Uri(); |
| 70 | +$uri->createFromString('https://fakeuser:fakepass@test.test'); |
| 71 | +$uri->getAuthority(); |
| 72 | +``` |
| 73 | + |
| 74 | +If no authority information is present, this method return an ```empty string```. |
| 75 | + |
| 76 | +If authority is set return it in form of: ```jhon:doe@test.com```. |
| 77 | + |
| 78 | +If the port is a standard port for scheme, the port is not included, otherwise yes. |
| 79 | + |
| 80 | +Example: |
| 81 | + |
| 82 | +``` |
| 83 | +// Create a URI with 'https' scheme and '80' as port (not default for https) |
| 84 | +$uri = new Uri(); |
| 85 | +$uri->createFromString('https://fakeuser:fakepass@test.test'); |
| 86 | +$uri->getAuthority(); |
| 87 | +``` |
| 88 | + |
| 89 | +Results in: ```jhon:doe@test.com:80``` |
| 90 | + |
| 91 | +### getAuthorityWithPort |
| 92 | + |
| 93 | +Return the authority part of the URI with the port too, even if it's the default. |
| 94 | + |
| 95 | +``` |
| 96 | +$uri = new Uri(); |
| 97 | +$uri->createFromString('https://fakeuser:fakepass@test.test:443'); |
| 98 | +$uri->getAuthorityWithPort(); |
| 99 | +``` |
| 100 | + |
| 101 | +Return: ```jhon:doe@test.com:443``` |
| 102 | + |
| 103 | +### getUserInfo |
| 104 | + |
| 105 | +Return the user part of the URI. |
| 106 | + |
| 107 | +``` |
| 108 | +$uri = new Uri(); |
| 109 | +$uri->createFromString('https://fakeuser:fakepass@test.test:443'); |
| 110 | +$uri->getUserInfo(); |
| 111 | +``` |
| 112 | + |
| 113 | +Return: |
| 114 | +- if no user information is present return an empty string. |
| 115 | +- if a user is present in the URI returns that value: ```fakeuser``` |
| 116 | +- if a password is present it returns the password too separated by ':' from the username: ```fakeuser:fakepass``` |
| 117 | + |
| 118 | +### getHost |
| 119 | + |
| 120 | +Return the host part of the URI. |
| 121 | + |
| 122 | +``` |
| 123 | +$uri = new Uri(); |
| 124 | +$uri->createFromString('https://test.test:443'); |
| 125 | +$uri->getHost(); |
| 126 | +``` |
| 127 | + |
| 128 | +Results in: ```test.test``` |
| 129 | + |
| 130 | +### getPort |
| 131 | + |
| 132 | +Return the port part of the URI. |
| 133 | + |
| 134 | +``` |
| 135 | +$uri = new Uri(); |
| 136 | +$uri->createFromString('https://fakeuser:fakepass@test.test:443'); |
| 137 | +$uri->getPort(); |
| 138 | +``` |
| 139 | +Return: |
| 140 | +- if the port is present return the port as an integer: ```443```. |
| 141 | +- if the port is not present, return ```null```. |
| 142 | + |
| 143 | +### isDefaultPort |
| 144 | + |
| 145 | +Get whether the port value of the URI is the default for given scheme. |
| 146 | + |
| 147 | +``` |
| 148 | +$uri = new Uri(); |
| 149 | +$uri->createFromString('https://fakeuser:fakepass@test.test:80'); |
| 150 | +$uri->isDefaultPort(); |
| 151 | +
|
| 152 | +// return false because 80 isn't the default port for https scheme |
| 153 | +``` |
| 154 | + |
| 155 | +Return: |
| 156 | +- ```true``` if the port is default for this scheme. |
| 157 | +- ```false``` if the port isn't the default. |
| 158 | + |
| 159 | +### getPath |
| 160 | + |
| 161 | +Get the path value of the URI. |
| 162 | + |
| 163 | +``` |
| 164 | +$uri = new Uri(); |
| 165 | +$uri->createFromString('https://test.test/path1/path2/page.html'); |
| 166 | +$uri->getPath(); |
| 167 | +``` |
| 168 | + |
| 169 | +Return: |
| 170 | +- if a path is present return ```/path1/path2/page.html```. |
| 171 | +- if the path is not present return an ```empty string```. |
| 172 | + |
| 173 | +### getPathAsArray |
| 174 | + |
| 175 | +Get the path value of the URI as an array. |
| 176 | + |
| 177 | +``` |
| 178 | +$uri = new Uri(); |
| 179 | +$uri->createFromString('https://test.test/path1/path2/page.html'); |
| 180 | +$uri->getPathAsArray(); |
| 181 | +``` |
| 182 | + |
| 183 | +Return: |
| 184 | +- if the path is present return the path portion of the URI as an array: |
| 185 | +``` |
| 186 | +[ |
| 187 | + 'path1', |
| 188 | + 'path2', |
| 189 | + 'page.html', |
| 190 | +] |
| 191 | +``` |
| 192 | +- if path is not present return an ```empty string```. |
| 193 | + |
| 194 | +### getQuery |
| 195 | + |
| 196 | +Get the query part of the URI. |
| 197 | + |
| 198 | +``` |
| 199 | +$uri = new Uri(); |
| 200 | +$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2'); |
| 201 | +$uri->getQuery(); |
| 202 | +``` |
| 203 | + |
| 204 | +Return: |
| 205 | +- if the query part of the URI is present: ```key1=value1&key2=value2```. |
| 206 | +- if the query part of the URI is not present return an ```empty string```. |
| 207 | + |
| 208 | +### getQueryValue |
| 209 | + |
| 210 | +Get the query value given a query key. |
| 211 | + |
| 212 | +``` |
| 213 | +$uri = new Uri(); |
| 214 | +$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2'); |
| 215 | +$uri->getQueryValue('key1'); |
| 216 | +``` |
| 217 | + |
| 218 | +Return: |
| 219 | +- if the key exists in query part return: ```value1```. |
| 220 | +- if the key not exists in query part return an ```empty string```. |
| 221 | + |
| 222 | +### getQueryAsArray |
| 223 | + |
| 224 | +Get the query part as an array. |
| 225 | + |
| 226 | +``` |
| 227 | +$uri = new Uri(); |
| 228 | +$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2'); |
| 229 | +$uri->getQueryAsArray(); |
| 230 | +``` |
| 231 | + |
| 232 | +Return: |
| 233 | +- if the query part exists return: |
| 234 | +``` |
| 235 | +[ |
| 236 | + 'key1' => 'value1', |
| 237 | + 'key2' => 'value2', |
| 238 | +] |
| 239 | +``` |
| 240 | +- if the query part not exists return an ```empty array```. |
| 241 | + |
| 242 | +### getPathAndQuery |
| 243 | + |
| 244 | +Get the path and query part of the URI. |
| 245 | + |
| 246 | +``` |
| 247 | +$uri = new Uri(); |
| 248 | +$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2'); |
| 249 | +$uri->getPathAndQuery(); |
| 250 | +``` |
| 251 | + |
| 252 | +Return: |
| 253 | +- if the path and query exists return ```/path1/path2/page.html?key1=value1&key2=value2```. |
| 254 | +- if the path exists and query not exists return ```/path1/path2/page.html```. |
| 255 | +- if the path not exists and query too return an ```empty string```. |
| 256 | + |
| 257 | +### getFragment |
| 258 | + |
| 259 | +Get the fragment part of the URI. |
| 260 | + |
| 261 | +``` |
| 262 | +$uri = new Uri(); |
| 263 | +$uri->createFromString('https://test.test/path1/path2/page.html?key1=value1&key2=value2#fragment-1'); |
| 264 | +$uri->getFragment(); |
| 265 | +``` |
| 266 | + |
| 267 | +Return: |
| 268 | +- if the path and query exists return ```/path1/path2/page.html?key1=value1&key2=value2```. |
| 269 | +- if the path exists and query not exists return ```/path1/path2/page.html```. |
| 270 | +- if the path not exists and query too return an ```empty string```. |
0 commit comments