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

AllOfType::parseDefinition()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 9
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerGen\Swagger\Type;
4
5
/**
6
 * allOf compound type
7
 *
8
 * @package    SwaggerGen
9
 * @author     Bruce Weirdan <[email protected]>
10
 * @copyright  2014-2015 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class AllOfType extends AbstractType
14
{
15
	private $allOfItems = array();
16
	private $mostRecentItem;
17
	protected function parseDefinition($definition)
18
	{
19
		$pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END;
20
		$inlineDef = '';
21
		if (preg_match($pattern, $definition, $matches)) {
22
			if (isset($matches[1])) {
23
				$inlineDef = $matches[1];
24
			}
25
		}
26
		if ($inlineDef) {
27
			foreach ($this->parseList($inlineDef) as $item) {
28
				$this->handleCommand('item', $item);
29
			}
30
		}
31
	}
32
33
	public function handleCommand($command, $data = null)
34
	{
35
		switch ($command) {
36
			case 'item':
37
				$this->mostRecentItem = self::typeFactory($this, $data);
38
				$this->allOfItems[] = $this->mostRecentItem;
39
				return $this;
40
		}
41
		if (isset($this->mostRecentItem)) {
42
			if ($this->mostRecentItem->handleCommand($command, $data)) {
43
				return $this;
44
			}
45
		}
46
		return parent::handleCommand($command, $data);
47
	}
48
49
	public function toArray()
50
	{
51
		$allOf = array();
52
		foreach ($this->allOfItems as $item) {
53
			$allOf[] = $item->toArray();
54
		}
55
		return self::arrayFilterNull(array_merge(array(
56
			'allOf' => $allOf,
57
		), parent::toArray()));
58
	}
59
}
60