Passed
Push — master ( cd6def...a3ee83 )
by Anatoly
02:04
created

Scheme::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/uri/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/uri
10
 */
11
12
namespace Sunrise\Uri\Component;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Uri\Exception\InvalidUriComponentException;
18
19
/**
20
 * URI component "scheme"
21
 *
22
 * @link https://tools.ietf.org/html/rfc3986#section-3.1
23
 */
24
class Scheme implements ComponentInterface
25
{
26
27
	/**
28
	 * The component value
29
	 *
30
	 * @var string
31
	 */
32
	protected $value = '';
33
34
	/**
35
	 * Constructor of the class
36
	 *
37
	 * @param mixed $value
38
	 *
39
	 * @throws InvalidUriComponentException
40
	 */
41
	public function __construct($value)
42
	{
43
		$regex = '/^(?:[A-Za-z][0-9A-Za-z\+\-\.]*)?$/';
44
45
		if ('' === $value)
46
		{
47
			return;
48
		}
49
		else if (! \is_string($value))
50
		{
51
			throw new InvalidUriComponentException('URI component "scheme" must be a string');
52
		}
53
		else if (! \preg_match($regex, $value))
54
		{
55
			throw new InvalidUriComponentException('Invalid URI component "scheme"');
56
		}
57
58
		$this->value = $value;
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 *
64
	 * @return string
65
	 */
66
	public function present() : string
67
	{
68
		return \strtolower($this->value);
69
	}
70
}
71