Scheme   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A getValue() 0 3 1
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_match;
18
use function strtolower;
19
20
/**
21
 * @link https://tools.ietf.org/html/rfc3986#section-3.1
22
 */
23
final class Scheme implements ComponentInterface
24
{
25
    private const VALIDATION_REGEX = '/^(?:[A-Za-z][0-9A-Za-z\x2b\x2d\x2e]*)?$/';
26
27
    private string $value = '';
28
29
    /**
30
     * @param mixed $value
31
     *
32
     * @throws InvalidArgumentException
33
     */
34 113
    public function __construct($value)
35
    {
36 113
        if ($value === '') {
37 1
            return;
38
        }
39
40 113
        if (!is_string($value)) {
41 13
            throw new InvalidArgumentException('URI component "scheme" must be a string');
42
        }
43
44 100
        if (!preg_match(self::VALIDATION_REGEX, $value)) {
45 1
            throw new InvalidArgumentException('Invalid URI component "scheme"');
46
        }
47
48
        // the component is case-insensitive...
49 100
        $this->value = strtolower($value);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @return string
56
     */
57 100
    public function getValue(): string
58
    {
59 100
        return $this->value;
60
    }
61
}
62