Passed
Pull Request — master (#31)
by Anatoly
39:16
created

User   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 69
ccs 13
cts 14
cp 0.9286
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A getValue() 0 3 1
A create() 0 7 2
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\InvalidUriComponentException;
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 InvalidUriComponentException
55
     *         If the component isn't valid.
56
     */
57 60
    public function __construct($value)
58
    {
59 60
        if ($value === '') {
60 2
            return;
61
        }
62
63 60
        if (!is_string($value)) {
64 9
            throw new InvalidUriComponentException('URI component "user" must be a string');
65
        }
66
67 51
        $this->value = preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string {
68
            /** @var array{0: string, 1?: string} $match */
69
70 51
            return isset($match[1]) ? rawurlencode($match[1]) : $match[0];
71 51
        }, $value);
72
    }
73
74
    /**
75
     * Creates a user component
76
     *
77
     * @param mixed $user
78
     *
79
     * @return User
80
     *
81
     * @throws InvalidUriComponentException
82
     */
83 60
    public static function create($user): User
84
    {
85 60
        if ($user instanceof User) {
86
            return $user;
87
        }
88
89 60
        return new User($user);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     * @return string
96
     */
97 43
    public function getValue(): string
98
    {
99 43
        return $this->value;
100
    }
101
}
102