Completed
Push — master ( e44420...645bfc )
by Derek
02:15
created

Port::getValidPattern()   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

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
use Subreality\Dilmun\Anshar\Http\SchemePortsTrait;
5
use Subreality\Dilmun\Anshar\Utils\ArrayHelper;
6
7
/**
8
 * Class Port
9
 * @package Subreality\Dilmun\Anshar\Http\UriParts
10
 */
11
class Port implements UriPartInterface
12
{
13
    use SchemePortsTrait;
14
15
    protected $port          = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 10 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
16
17
    protected static $valid_pattern = '/^[0-9]{0,4}$/';
18
19
    /**
20
     * Port constructor. Accepts an integer or null value representing a URI port component. Construction will throw an
21
     * exception if port is not an integer or is not null or if the given integer is outside of the allowed TCP/UDP
22
     * range of 1 to 65535.
23
     *
24
     * port = *DIGIT
25
     *
26
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
27
     *
28
     * @throws \InvalidArgumentException if the given port is not an integer or null or if the given integer is out of
29
     * range
30
     *
31
     * @param int|null $port    A string representing a URI port
32
     */
33 114
    public function __construct($port)
34
    {
35 114
        if (!is_int($port) && !is_null($port)) {
36 6
            throw new \InvalidArgumentException("Port must be an integer or null");
37 108
        } elseif (!self::isValid($port)) {
38 3
            throw new \InvalidArgumentException("Port must be between 1 and 65535 inclusive");
39
        }
40
41 105
        $this->port = $port;
42 105
    }
43
44
    /**
45
     * Returns a string representation of the port.
46
     *
47
     * @return string   The string representation of the port
48
     */
49 3
    public function __toString()
50
    {
51 3
        return (string) $this->port;
52
    }
53
54 1
    public static function getValidPattern()
55
    {
56 1
        return self::$valid_pattern;
57
    }
58
59
    /**
60
     * Validates a given port. A port is valid if it is either null or an integer between 1 and 65535 inclusive.
61
     *
62
     * @param mixed $port   The port to be validated
63
     * @return bool         Returns true if the given port is valid
64
     *                      Returns false otherwise
65
     */
66 113
    public static function isValid($port)
67
    {
68 113
        $port_valid = false;
69
70 113
        if (is_int($port)) {
71 44
            $port_valid = self::portIsInValidRange($port);
72 113
        } elseif (is_null($port)) {
73 69
            $port_valid = true;
74 69
        }
75
76 113
        return $port_valid;
77
    }
78
79
    /**
80
     * Returns a string representation of the port formatted so that it can be compiled into a complete URI string
81
     * per the Uri object's string specification.
82
     *
83
     * If the port is empty, toUriString returns an empty string; if the port is not empty, toUriString returns the
84
     * port prefixed with a colon.
85
     *
86
     * toUriString will also check the port against an optionally-provided scheme; if the port is standard for the given
87
     * scheme, toUriString returns an empty string.
88
     *
89
     * @see Uri::__toString
90
     *
91
     * @param Scheme|null $scheme   [optional] A scheme against which to check if the port is the standard
92
     *
93
     * @return string               A string representation of the port formatted for a complete URI string
94
     */
95 8
    public function toUriString(Scheme $scheme = null)
96
    {
97 8
        $normalized_port = $this->normalizePortAgainstScheme($scheme);
98
99 8
        $uri_string = (string) $normalized_port;
100
101 8
        if (!is_null($normalized_port)) {
102 4
            $uri_string = ":" . $uri_string;
103 4
        }
104
105 8
        return $uri_string;
106
    }
107
108
    /**
109
     * Normalizes the port against a given scheme so that the scheme is reported as null if the port is standard for the
110
     * scheme.
111
     *
112
     * For example, given a http scheme and a port of 80, the normalized port will be reported as null; given a https
113
     * scheme and a port of 80, the normalized port will be reported as 80.
114
     *
115
     * @see Port::toUriString()
116
     * @see SchemePortsTrait
117
     *
118
     * @param Scheme|null $scheme
119
     * @return int|null
120
     */
121 12
    public function normalizePortAgainstScheme(Scheme $scheme = null)
122
    {
123 12
        $scheme_port_array = new ArrayHelper($this->scheme_ports);
124 12
        $scheme_string     = (string) $scheme;
125
126 12
        $standard_port = $scheme_port_array->valueLookup($scheme_string);
127
128 12
        if ($this->port == $standard_port) {
129 7
            $normalized_port = null;
130 7
        } else {
131 5
            $normalized_port = $this->port;
132
        }
133
134 12
        return $normalized_port;
135
    }
136
137
    /**
138
     * Validates whether a given port falls within the range of valid ports for TCP/UDP (1 to 65535)
139
     *
140
     * @param int $port The port to be validated
141
     *
142
     * @return bool     Returns true if the port falls within the valid range
143
     *                  Returns false otherwise
144
     */
145 44
    private static function portIsInValidRange($port)
146
    {
147 44
        if ($port > 0 && $port < 65536) {
148 39
            return true;
149
        } else {
150 5
            return false;
151
        }
152
    }
153
}
154