Completed
Push — master ( 8b636b...7c2d20 )
by Martijn
13s
created

ArrayType::validateItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerGen\Swagger\Type;
4
5
/**
6
 * Basic array type definition.
7
 *
8
 * @package    SwaggerGen
9
 * @author     Martijn van der Lee <[email protected]>
10
 * @copyright  2014-2015 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class ArrayType extends AbstractType
14
{
15
16
	const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?';
17
18
	private static $collectionFormats = array(
19
		'array' => 'csv',
20
		'csv' => 'csv',
21
		'ssv' => 'ssv',
22
		'tsv' => 'tsv',
23
		'pipes' => 'pipes',
24
		'multi' => 'multi',
25
	);
26
27
	/**
28
	 * @var AbstractType
29
	 */
30
	private $Items = null;
31
	private $minItems = null;
32
	private $maxItems = null;
33
	private $collectionFormat = null;
34
35
	protected function parseDefinition($definition)
36
	{
37
		$definition = self::trim($definition);
38
		$match = array();
39
		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
40
			// recognized format
41
		} elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
42
			$match[1] = 'array';
43
		} else {
44
			throw new \SwaggerGen\Exception("Unparseable array definition: '{$definition}'");
45
		}
46
47
		$this->parseFormat($definition, $match);
48
		$this->parseItems($definition, $match);
49
		$this->parseRange($definition, $match);
50
	}
51
52
	private function parseFormat($definition, $match)
53
	{
54
		$type = strtolower($match[1]);
55
		if (!isset(self::$collectionFormats[$type])) {
56
			throw new \SwaggerGen\Exception("Not an array: '{$definition}'");
57
		}
58
59
		if ($type === 'multi') {
60
			$parent = $this->getParent();
61
			if (!($parent instanceof \SwaggerGen\Swagger\Parameter) || !$parent->isMulti()) {
62
				throw new \SwaggerGen\Exception("Multi array only allowed on query or form parameter: '{$definition}'");
63
			}
64
		}
65
66
		$this->collectionFormat = self::$collectionFormats[$type];
67
	}
68
69
	private function parseItems($definition, $match)
0 ignored issues
show
Unused Code introduced by
The parameter $definition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
	{
71
		if (!empty($match[2])) {
72
			$this->Items = $this->validateItems($match[2]);
73
		}
74
	}
75
76
	private function parseRange($definition, $match)
77
	{
78
		if (!empty($match[3])) {
79
			if ($match[4] === '' && $match[5] === '') {
80
				throw new \SwaggerGen\Exception("Empty array range: '{$definition}'");
81
			}
82
83
			$exclusiveMinimum = isset($match[3]) ? ($match[3] == '<') : null;
84
			$this->minItems = $match[4] === '' ? null : intval($match[4]);
85
			$this->maxItems = $match[5] === '' ? null : intval($match[5]);
86
			$exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
87
			if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) {
88
				self::swap($this->minItems, $this->maxItems);
89
				self::swap($exclusiveMinimum, $exclusiveMaximum);
90
			}
91
			$this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems);
92
			$this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems);
93
		}
94
	}
95
96
	/**
97
	 * @param string $command The comment command
98
	 * @param string $data Any data added after the command
99
	 * @return \SwaggerGen\Swagger\Type\AbstractType|boolean
100
	 */
101
	public function handleCommand($command, $data = null)
102
	{
103
		if ($this->Items) {
104
			$return = $this->Items->handleCommand($command, $data);
105
			if ($return) {
106
				return $return;
107
			}
108
		}
109
110
		switch (strtolower($command)) {
111
			case 'min':
112
				$this->minItems = intval($data);
113
				if ($this->minItems < 0) {
114
					throw new \SwaggerGen\Exception("Minimum less than zero: '{$data}'");
115
				}
116
				if ($this->maxItems !== null && $this->minItems > $this->maxItems) {
117
					throw new \SwaggerGen\Exception("Minimum greater than maximum: '{$data}'");
118
				}
119
				return $this;
120
121
			case 'max':
122
				$this->maxItems = intval($data);
123
				if ($this->minItems !== null && $this->minItems > $this->maxItems) {
124
					throw new \SwaggerGen\Exception("Maximum less than minimum: '{$data}'");
125
				}
126
				if ($this->maxItems < 0) {
127
					throw new \SwaggerGen\Exception("Maximum less than zero: '{$data}'");
128
				}
129
				return $this;
130
131
			case 'items':
132
				$this->Items = $this->validateItems($data);
133
				return $this->Items;
134
		}
135
136
		return parent::handleCommand($command, $data);
137
	}
138
139
	public function toArray()
140
	{
141
		return self::arrayFilterNull(array_merge(array(
142
					'type' => 'array',
143
					'items' => empty($this->Items) ? null : $this->Items->toArray(),
144
					'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat,
145
					'minItems' => $this->minItems,
146
					'maxItems' => $this->maxItems,
147
								), parent::toArray()));
148
	}
149
150
	private function validateItems($items)
151
	{
152
		if (empty($items)) {
153
			throw new \SwaggerGen\Exception("Empty items definition: '{$items}'");
154
		}
155
156
		return self::typeFactory($this, $items, "Unparseable items definition: '%s'");
157
	}
158
159
	public function __toString()
160
	{
161
		return __CLASS__;
162
	}
163
164
}
165