1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Http Message. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2019, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Http\Message; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Uri trait. |
16
|
|
|
* |
17
|
|
|
* Provide help methods for Uri class. |
18
|
|
|
*/ |
19
|
|
|
trait UriTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Create uri string. |
23
|
|
|
* |
24
|
|
|
* @param string $scheme |
25
|
|
|
* @param string $authority |
26
|
|
|
* @param string $path |
27
|
|
|
* @param string $query |
28
|
|
|
* @param string $fragment |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
26 |
|
private function createUriString( |
33
|
|
|
string $scheme, |
34
|
|
|
string $authority, |
35
|
|
|
string $path, |
36
|
|
|
string $query, |
37
|
|
|
string $fragment |
38
|
|
|
): string { |
39
|
26 |
|
$uri = $scheme.$authority; |
40
|
|
|
|
41
|
26 |
|
$path = ('/' !== \substr($path, 0, 1) && $uri !== '' && $path !== '') ? '/'.$path : $path; |
42
|
|
|
|
43
|
26 |
|
return \implode('', [$uri, $path, $query, $fragment]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get non standard port. |
48
|
|
|
* |
49
|
|
|
* @param int $port |
50
|
|
|
* @param string $scheme |
51
|
|
|
* @param bool $standardScheme |
52
|
|
|
* @param array $supportedSchemes |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
11 |
|
private function getNonStandardPort( |
57
|
|
|
int $port, |
58
|
|
|
string $scheme, |
59
|
|
|
bool $standardScheme, |
60
|
|
|
array $supportedSchemes |
61
|
|
|
): int { |
62
|
11 |
|
return (!$port && $standardScheme) ? $supportedSchemes[$scheme] : $port; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get port for standard scheme. |
67
|
|
|
* |
68
|
|
|
* @param bool $standardPort |
69
|
|
|
* @param int $port |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
1 |
|
private function getPortForStandardScheme(bool $standardPort, int $port): int |
74
|
|
|
{ |
75
|
1 |
|
return ($standardPort) ? 0 : $port; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check standard port for current scheme. |
80
|
|
|
* |
81
|
|
|
* @param string $scheme |
82
|
|
|
* @param int $port |
83
|
|
|
* @param array $supportedSchemes |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
12 |
|
private function checkStandardPortForCurretScheme(string $scheme, int $port, array $supportedSchemes): bool |
88
|
|
|
{ |
89
|
12 |
|
return (isset($supportedSchemes[$scheme]) && $port === $supportedSchemes[$scheme]) ? true : false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|