Skip to content

Commit d96d0fc

Browse files
committedJan 23, 2020
Update readme
0 parents  commit d96d0fc

16 files changed

+1658
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
.phpunit.result.cache

‎LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Nicola Saliu, https://github.com/nsaliu
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

‎README.md

+270
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
# Laravel URI package
2+
3+
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4+
![PHP from Packagist](https://img.shields.io/packagist/php-v/nsaliu/laravel-uri?label=php&style=flat-square)
5+
![GitHub repo size](https://img.shields.io/github/repo-size/nsaliu/laravel-uri?style=flat-square)
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```.

‎composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "nsaliu/laravel-uri",
3+
"description": "A URI component for Laravel framework",
4+
"homepage": "https://github.com/nsaliu/laravel-uri",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Nicola Saliu",
9+
"email": "nicola.saliu@gmail.com",
10+
"role": "Developer"
11+
}
12+
],
13+
"require": {
14+
"php" : "^7.2"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^8.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Nsaliu\\Uri\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Nsaliu\\Uri\\Tests\\": "tests/"
27+
}
28+
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"Nsaliu\\Uri\\UriComponentServiceProvider"
33+
]
34+
}
35+
},
36+
"minimum-stability": "dev",
37+
"prefer-stable": true
38+
}

‎phpunit.xml.dist

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="URI Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<php>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
</php>
25+
</phpunit>

‎src/CurlWrapper.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri;
4+
5+
use Nsaliu\Uri\Exceptions\CurlExtensionNotLoaded;
6+
7+
class CurlWrapper
8+
{
9+
/**
10+
* @param string $uri
11+
* @return int
12+
* @throws CurlExtensionNotLoaded
13+
*/
14+
public function getReturnCode(string $uri): int
15+
{
16+
if (!extension_loaded('curl')) {
17+
throw new CurlExtensionNotLoaded();
18+
}
19+
20+
$ch = \curl_init($uri);
21+
22+
\curl_setopt($ch, CURLOPT_NOBODY, true);
23+
\curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
24+
\curl_exec($ch);
25+
26+
$returnCode = \curl_getinfo($ch, CURLINFO_HTTP_CODE);
27+
\curl_close($ch);
28+
29+
return (int)$returnCode;
30+
}
31+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
class CurlExtensionNotLoaded extends \Exception
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $message = 'cURL extension is not loaded';
11+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
class InvalidUriException extends \Exception
6+
{
7+
protected $message = 'Given URI is invalid';
8+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
use Throwable;
6+
7+
class PortOutOfRangeException extends \Exception
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $message = "A valid port must be between 0 and 65535, [%s] given";
13+
14+
/**
15+
* PortOutOfRangeException constructor.
16+
* @param int $port
17+
* @param int $code
18+
* @param Throwable|null $previous
19+
*/
20+
public function __construct(int $port, $code = 0, Throwable $previous = null)
21+
{
22+
parent::__construct(sprintf($this->message, $port), $code, $previous);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
class QueryCannotContainFragmentException extends \Exception
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $message = 'Query can not contains fragment parts';
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
use Throwable;
6+
7+
class QueryKeyAlreadyExistsException extends \Exception
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $message = 'Given query key [%s] already exists';
13+
14+
/**
15+
* QueryKeyAlreadyExists constructor.
16+
* @param string $key
17+
* @param int $code
18+
* @param Throwable|null $previous
19+
*/
20+
public function __construct(string $key, $code = 0, Throwable $previous = null)
21+
{
22+
parent::__construct(sprintf($this->message, $key), $code, $previous);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Exceptions;
4+
5+
class QueryKeyMustHaveAtLeastOneCharException extends \Exception
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $message = 'Query key must have at least one character';
11+
}

‎src/Uri.php

+647
Large diffs are not rendered by default.

‎src/UriComponentServiceProvider.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class UriComponentServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
//
27+
}
28+
}

‎tests/CurlWrapperTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Nsaliu\Uri\Tests;
4+
5+
use Nsaliu\Uri\CurlWrapper;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CurlWrapperTest extends TestCase
9+
{
10+
/**
11+
* @var CurlWrapper
12+
*/
13+
private $sut;
14+
15+
protected function setUp(): void
16+
{
17+
$this->sut = $this->createMock(CurlWrapper::class);
18+
}
19+
20+
public function testGetReturnCode()
21+
{
22+
$uri = 'https://github.com';
23+
24+
$this->sut
25+
->expects($this->once())
26+
->method('getReturnCode')
27+
->with($uri)
28+
->willReturn(200);
29+
30+
$this->assertEquals(200, $this->sut->getReturnCode($uri));
31+
}
32+
}

‎tests/UriTest.php

+476
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.