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
|
|
|
use Sunrise\Http\Message\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
use function is_string; |
17
|
|
|
use function preg_replace_callback; |
18
|
|
|
use function rawurlencode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-3.2.1 |
22
|
|
|
*/ |
23
|
|
|
final class Password implements ComponentInterface |
24
|
|
|
{ |
25
|
|
|
// phpcs:ignore Generic.Files.LineLength |
26
|
|
|
private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
27
|
|
|
|
28
|
|
|
private string $value = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $value |
32
|
|
|
* |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
52 |
|
public function __construct($value) |
36
|
|
|
{ |
37
|
52 |
|
if ($value === '') { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
52 |
|
if (!is_string($value)) { |
42
|
8 |
|
throw new InvalidArgumentException('URI component "password" must be a string'); |
43
|
|
|
} |
44
|
|
|
|
45
|
44 |
|
$this->value = (string) preg_replace_callback( |
46
|
44 |
|
self::NORMALIZATION_REGEX, |
47
|
44 |
|
static fn(array $matches): string => ( |
48
|
|
|
/** @var array{0: string, 1?: string} $matches */ |
49
|
44 |
|
isset($matches[1]) ? rawurlencode($matches[1]) : $matches[0] |
50
|
44 |
|
), |
51
|
44 |
|
$value, |
52
|
44 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed $password |
57
|
|
|
* |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
52 |
|
public static function create($password): Password |
61
|
|
|
{ |
62
|
52 |
|
if ($password instanceof Password) { |
63
|
|
|
return $password; |
64
|
|
|
} |
65
|
|
|
|
66
|
52 |
|
return new Password($password); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
44 |
|
public function getValue(): string |
75
|
|
|
{ |
76
|
44 |
|
return $this->value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|