Completed
Push — master ( 803d3c...9ccb75 )
by Josh
12:51
created

NetworkFilter::filterIpport()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Parser\AttributeFilters;
9
10
class NetworkFilter
11
{
12
	/**
13
	* Filter an IP value (includes IPv4 and IPv6)
14
	*
15
	* @param  string $attrValue Original value
16
	* @return mixed             Filtered value, or FALSE if invalid
17
	*/
18 3
	public static function filterIp($attrValue)
19
	{
20 3
		return filter_var($attrValue, FILTER_VALIDATE_IP);
21
	}
22
23
	/**
24
	* Filter an IP:port value (includes IPv4 and IPv6)
25
	*
26
	* @param  string $attrValue Original value
27
	* @return mixed             Filtered value, or FALSE if invalid
28
	*/
29 6
	public static function filterIpport($attrValue)
30
	{
31 6
		if (preg_match('/^\\[([^\\]]+)(\\]:[1-9][0-9]*)$/D', $attrValue, $m))
32
		{
33 2
			$ip = self::filterIpv6($m[1]);
34
35 2
			if ($ip === false)
36
			{
37 1
				return false;
38
			}
39
40 1
			return '[' . $ip . $m[2];
41
		}
42
43 4
		if (preg_match('/^([^:]+)(:[1-9][0-9]*)$/D', $attrValue, $m))
44
		{
45 2
			$ip = self::filterIpv4($m[1]);
46
47 2
			if ($ip === false)
48
			{
49 1
				return false;
50
			}
51
52 1
			return $ip . $m[2];
53
		}
54
55 2
		return false;
56
	}
57
58
	/**
59
	* Filter an IPv4 value
60
	*
61
	* @param  string $attrValue Original value
62
	* @return mixed             Filtered value, or FALSE if invalid
63
	*/
64 5
	public static function filterIpv4($attrValue)
65
	{
66 5
		return filter_var($attrValue, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
67
	}
68
69
	/**
70
	* Filter an IPv6 value
71
	*
72
	* @param  string $attrValue Original value
73
	* @return mixed             Filtered value, or FALSE if invalid
74
	*/
75 5
	public static function filterIpv6($attrValue)
76
	{
77 5
		return filter_var($attrValue, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
78
	}
79
}