Completed
Pull Request — master (#23)
by
unknown
02:33
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 'ref':
37
			case 'inline':
38
			case 'item':
39
				$this->mostRecentItem = self::typeFactory($this, $data);
40
				$this->allOfItems[] = $this->mostRecentItem;
41
				return $this;
42
		}
43
		if (isset($this->mostRecentItem)) {
44
			if ($this->mostRecentItem->handleCommand($command, $data)) {
45
				return $this;
46
			}
47
		}
48
		return parent::handleCommand($command, $data);
49
	}
50
51
	public function toArray()
52
	{
53
		$allOf = array();
54
		foreach ($this->allOfItems as $item) {
55
			$allOf[] = $item->toArray();
56
		}
57
		return self::arrayFilterNull(array_merge(array(
58
			'allOf' => $allOf,
59
		), parent::toArray()));
60
	}
61
}
62