1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Uri\Component; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Sunrise\Http\Message\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Import functions |
21
|
|
|
*/ |
22
|
|
|
use function is_string; |
23
|
|
|
use function preg_replace_callback; |
24
|
|
|
use function rawurlencode; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* URI component "user" |
28
|
|
|
* |
29
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-3.2.1 |
30
|
|
|
*/ |
31
|
|
|
final class User implements ComponentInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Regular expression used for the component normalization |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
// phpcs:ignore Generic.Files.LineLength |
40
|
|
|
private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The component value |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private string $value = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor of the class |
51
|
|
|
* |
52
|
|
|
* @param mixed $value |
53
|
|
|
* |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
* If the component isn't valid. |
56
|
|
|
*/ |
57
|
62 |
|
public function __construct($value) |
58
|
|
|
{ |
59
|
62 |
|
if ($value === '') { |
60
|
2 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
62 |
|
if (!is_string($value)) { |
64
|
9 |
|
throw new InvalidArgumentException('URI component "user" must be a string'); |
65
|
|
|
} |
66
|
|
|
|
67
|
53 |
|
$this->value = (string) preg_replace_callback( |
68
|
53 |
|
self::NORMALIZATION_REGEX, |
69
|
53 |
|
static function (array $match): string { |
70
|
|
|
/** @var array{0: string, 1?: string} $match */ |
71
|
53 |
|
return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
72
|
53 |
|
}, |
73
|
53 |
|
$value |
74
|
53 |
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a user component |
79
|
|
|
* |
80
|
|
|
* @param mixed $user |
81
|
|
|
* |
82
|
|
|
* @return User |
83
|
|
|
* |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
*/ |
86
|
62 |
|
public static function create($user): User |
87
|
|
|
{ |
88
|
62 |
|
if ($user instanceof User) { |
89
|
|
|
return $user; |
90
|
|
|
} |
91
|
|
|
|
92
|
62 |
|
return new User($user); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
45 |
|
public function getValue(): string |
101
|
|
|
{ |
102
|
45 |
|
return $this->value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|