Completed
Pull Request — master (#46)
by ignace nyamagana
05:56 queued 03:24
created

Port::toInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.1.1
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\Port as PortInterface;
16
use League\Uri\Types\ValidatorTrait;
17
18
/**
19
 * Value object representing a URI port component.
20
 *
21
 * @package League.uri
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 * @since   1.0.0
24
 */
25
class Port extends AbstractComponent implements PortInterface
26
{
27
    use ValidatorTrait;
28
29
    /**
30
     * Validate Port data
31
     *
32
     * @param null|int $port
33
     *
34
     * @throws InvalidArgumentException if the submitted port is invalid
35
     *
36
     * @return null|int
37
     */
38 205
    protected function validate($port)
39
    {
40 205
        if ('' === $port) {
41 3
            throw new InvalidArgumentException(
42 1
                'Expected port to be a int or null; received an empty string'
43 2
            );
44
        }
45
46 202
        return $this->validatePort($port);
47
    }
48
49
    /**
50
     * Returns the instance string representation
51
     * with its optional URI delimiters
52
     *
53
     * @return string
54
     */
55 158
    public function getUriComponent()
56
    {
57 158
        return null === $this->data ? '' : PortInterface::DELIMITER.$this->data;
58
    }
59
60
    /**
61
     * Return an integer representation of the Port component
62
     *
63
     * @return null|int
64
     */
65 583
    public function toInt()
66
    {
67 583
        return $this->data;
68
    }
69
70
    /**
71
     * Initialize the Port data
72
     *
73
     * @param null|int $data
74
     */
75 205
    protected function init($data)
76
    {
77 205
        $this->data = $this->validate($data);
78 178
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 2
    public function __debugInfo()
84
    {
85 2
        return ['port' => $this->toInt()];
86
    }
87
}
88