Completed
Pull Request — master (#33)
by Martijn
02:18
created

Ipv6Type   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B parseDefinition() 0 15 5
A validateDefault() 0 12 3
1
<?php
2
3
namespace SwaggerGen\Swagger\Type\Custom;
4
5
/**
6
 * IPv6 type definition
7
 *
8
 * @package    SwaggerGen
9
 * @author     Martijn van der Lee <[email protected]>
10
 * @copyright  2014-2017 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class Ipv6Type extends \SwaggerGen\Swagger\Type\StringType
14
{
15
16
	const PATTERN = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))';
17
	const FORMAT = 'ipv6';
18
19
	/**
20
	 * Construct and setup the regular expression for this type
21
	 * 
22
	 * @param \SwaggerGen\Swagger\AbstractObject $parent
23
	 * @param string $definition
24
	 */
25
	public function __construct(\SwaggerGen\Swagger\AbstractObject $parent, $definition)
26
	{
27
		$this->pattern = self::PATTERN;
28
29
		parent::__construct($parent, $definition);
30
	}
31
32
	/**
33
	 * Parse a type definition string, assuming it belongs to this type
34
	 * 
35
	 * @param string $definition
36
	 * @throws \SwaggerGen\Exception
37
	 */
38
	protected function parseDefinition($definition)
39
	{
40
		$definition = self::trim($definition);
41
42
		$match = array();
43
		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
44
			throw new \SwaggerGen\Exception("Unparseable IPv6 definition: '{$definition}'");
45
		}
46
47
		if (strtolower($match[1]) !== self::FORMAT) {
48
			throw new \SwaggerGen\Exception("Not an IPv6: '{$definition}'");
49
		}
50
51
		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
52
	}
53
54
	/**
55
	 * Check (and optionally reformat) a default value
56
	 * 
57
	 * @param string $value
58
	 * @return string
59
	 * @throws \SwaggerGen\Exception
60
	 */
61
	protected function validateDefault($value)
62
	{
63
		if (empty($value)) {
64
			throw new \SwaggerGen\Exception("Empty IPv6 default");
65
		}
66
67
		if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
68
			throw new \SwaggerGen\Exception("Invalid IPv6 default value: '{$value}'");
69
		}
70
71
		return $value;
72
	}
73
74
}
75