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

Port::__construct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 19
rs 9.6111
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 "port"
21
 *
22
 * @link https://tools.ietf.org/html/rfc3986#section-3.2.3
23
 */
24
class Port implements ComponentInterface
25
{
26
27
	/**
28
	 * The component value
29
	 *
30
	 * @var null|int
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
		$min = 1;
44
		$max = 2 ** 16;
45
46
		if (null === $value)
47
		{
48
			return;
49
		}
50
		else if (! \is_int($value))
51
		{
52
			throw new InvalidUriComponentException('URI component "port" must be an integer');
53
		}
54
		else if (! ($value >= $min && $value <= $max))
55
		{
56
			throw new InvalidUriComponentException('Invalid URI component "port"');
57
		}
58
59
		$this->value = $value;
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 *
65
	 * @return null|int
66
	 */
67
	public function present() : ?int
68
	{
69
		return $this->value;
70
	}
71
}
72