1
|
|
|
<?php |
2
|
|
|
namespace Kambo\Http\Message\Utils; |
3
|
|
|
|
4
|
|
|
// \Spl |
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Validate selected parts of uri. |
9
|
|
|
* |
10
|
|
|
* @package Kambo\Http\Message\Utils |
11
|
|
|
* @author Bohuslav Simek <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
class UriValidator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Validate Uri port. |
18
|
|
|
* Value can be null or integer between 1 and 65535. |
19
|
|
|
* |
20
|
|
|
* @param null|int $port The Uri port number. |
21
|
|
|
* |
22
|
|
|
* @return null|int |
23
|
|
|
* |
24
|
|
|
* @throws InvalidArgumentException If the port is invalid. |
25
|
|
|
*/ |
26
|
5 |
|
public function validatePort($port) |
27
|
|
|
{ |
28
|
5 |
|
if (!is_null($port) && (!is_integer($port) || ($port <= 1 || $port >= 65535))) { |
29
|
4 |
|
throw new InvalidArgumentException( |
30
|
|
|
'Uri port must be null or an integer between 1 and 65535 (inclusive)' |
31
|
4 |
|
); |
32
|
|
|
} |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validate Uri path. |
37
|
|
|
* |
38
|
|
|
* Path must NOT contain query string or URI fragment. It can be object, |
39
|
|
|
* but then the class must implement __toString method. |
40
|
|
|
* |
41
|
|
|
* @param string|object $path The Uri path |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
* |
45
|
|
|
* @throws InvalidArgumentException If the path is invalid. |
46
|
|
|
*/ |
47
|
7 |
|
public function validatePath($path) |
48
|
|
|
{ |
49
|
|
|
// Part of validation is same as for query validation |
50
|
7 |
|
$this->validateQuery($path); |
51
|
|
|
|
52
|
3 |
|
if (strpos($path, '?') !== false) { |
53
|
2 |
|
throw new InvalidArgumentException( |
54
|
|
|
'Invalid path provided; must not contain a query string' |
55
|
2 |
|
); |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Validate query. |
61
|
|
|
* |
62
|
|
|
* Path must NOT contain URI fragment. It can be object, |
63
|
|
|
* but then the class must implement __toString method. |
64
|
|
|
* |
65
|
|
|
* @param string|object $query The query path |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
* |
69
|
|
|
* @throws InvalidArgumentException If the query is invalid. |
70
|
|
|
*/ |
71
|
14 |
|
public function validateQuery($query) |
72
|
|
|
{ |
73
|
14 |
|
if (!is_string($query) && !method_exists($query, '__toString')) { |
74
|
5 |
|
throw new InvalidArgumentException( |
75
|
|
|
'Query must be a string' |
76
|
5 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
if (strpos($query, '#') !== false) { |
80
|
4 |
|
throw new InvalidArgumentException( |
81
|
|
|
'Query must not contain a URI fragment' |
82
|
4 |
|
); |
83
|
|
|
} |
84
|
5 |
|
} |
85
|
|
|
} |
86
|
|
|
|