|
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 "password" |
|
28
|
|
|
* |
|
29
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-3.2.1 |
|
30
|
|
|
*/ |
|
31
|
|
|
final class Password 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
|
52 |
|
public function __construct($value) |
|
58
|
|
|
{ |
|
59
|
52 |
|
if ($value === '') { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
52 |
|
if (!is_string($value)) { |
|
64
|
8 |
|
throw new InvalidArgumentException('URI component "password" must be a string'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
44 |
|
$this->value = (string) preg_replace_callback( |
|
68
|
44 |
|
self::NORMALIZATION_REGEX, |
|
69
|
44 |
|
static function (array $match): string { |
|
70
|
|
|
/** @var array{0: string, 1?: string} $match */ |
|
71
|
44 |
|
return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
72
|
44 |
|
}, |
|
73
|
44 |
|
$value |
|
74
|
44 |
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Creates a password component |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $password |
|
81
|
|
|
* |
|
82
|
|
|
* @return Password |
|
83
|
|
|
* |
|
84
|
|
|
* @throws InvalidArgumentException |
|
85
|
|
|
*/ |
|
86
|
52 |
|
public static function create($password): Password |
|
87
|
|
|
{ |
|
88
|
52 |
|
if ($password instanceof Password) { |
|
89
|
|
|
return $password; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
52 |
|
return new Password($password); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
44 |
|
public function getValue(): string |
|
101
|
|
|
{ |
|
102
|
44 |
|
return $this->value; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|