Passed
Pull Request — master (#27)
by Anatoly
39:10
created

Scheme::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 10
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_match;
24
use function strtolower;
25
26
/**
27
 * URI component "scheme"
28
 *
29
 * @link https://tools.ietf.org/html/rfc3986#section-3.1
30
 */
31
final class Scheme implements ComponentInterface
32
{
33
34
    /**
35
     * Regular expression to validate the component value
36
     *
37
     * @var string
38
     */
39
    private const VALIDATE_REGEX = '/^(?:[A-Za-z][0-9A-Za-z\+\-\.]*)?$/';
40
41
    /**
42
     * The component value
43
     *
44
     * @var string
45
     */
46
    private string $value = '';
47
48
    /**
49
     * Constructor of the class
50
     *
51
     * @param mixed $value
52
     *
53
     * @throws InvalidUriComponentException
54
     *         If the component isn't valid.
55
     */
56 117
    public function __construct($value)
57
    {
58 117
        if ($value === '') {
59 1
            return;
60
        }
61
62 117
        if (!is_string($value)) {
63 13
            throw new InvalidUriComponentException('URI component "scheme" must be a string');
64
        }
65
66 104
        if (!preg_match(self::VALIDATE_REGEX, $value)) {
67 1
            throw new InvalidUriComponentException('Invalid URI component "scheme"');
68
        }
69
70
        // the component is case-insensitive...
71 104
        $this->value = strtolower($value);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @return string
78
     */
79 104
    public function getValue(): string
80
    {
81 104
        return $this->value;
82
    }
83
}
84