Query   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A __construct() 0 17 4
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.4
22
 */
23
final class Query implements ComponentInterface
24
{
25
    // phpcs:ignore Generic.Files.LineLength
26
    private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x3f-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u';
27
28
    private string $value = '';
29
30
    /**
31
     * @param mixed $value
32
     *
33
     * @throws InvalidArgumentException
34
     */
35 56
    public function __construct($value)
36
    {
37 56
        if ($value === '') {
38 1
            return;
39
        }
40
41 56
        if (!is_string($value)) {
42 9
            throw new InvalidArgumentException('URI component "query" must be a string');
43
        }
44
45 47
        $this->value = (string) preg_replace_callback(
46 47
            self::NORMALIZATION_REGEX,
47 47
            static fn(array $matches): string => (
48
                /** @var array{0: string, 1?: string} $matches */
49 47
                isset($matches[1]) ? rawurlencode($matches[1]) : $matches[0]
50 47
            ),
51 47
            $value,
52 47
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @return string
59
     */
60 47
    public function getValue(): string
61
    {
62 47
        return $this->value;
63
    }
64
}
65