Completed
Push — master ( a29264...8b636b )
by Martijn
02:23
created

AbstractType::swap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerGen\Swagger\Type;
4
5
/**
6
 * Abstract foundation of all type definitions.
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
abstract class AbstractType extends \SwaggerGen\Swagger\AbstractObject
14
{
15
16
	const REGEX_START = '/^';
17
	const REGEX_FORMAT = '([a-z][a-z0-9]*)';
18
	const REGEX_CONTENT = '(?:\((.*)\))?';
19
	const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?';
20
	const REGEX_DEFAULT = '(?:=(.+))?';
21
	const REGEX_END = '$/i';
22
23
	private $example = null;
24
25
	protected static function swap(&$a, &$b)
26
	{
27
		$tmp = $a;
28
		$a = $b;
29
		$b = $tmp;
30
	}
31
32
	/**
33
	 * @var string $definition
34
	 */
35
	public function __construct(\SwaggerGen\Swagger\AbstractObject $parent, $definition)
36
	{
37
		parent::__construct($parent);
38
39
		$this->parseDefinition($definition);
40
	}
41
42
	abstract protected function parseDefinition($definition);
43
44
	/**
45
	 * Overwrites default AbstractObject parser, since Types should not handle
46
	 * extensions themselves.
47
	 * 
48
	 * @param string $command
49
	 * @param string $data
50
	 * @return \SwaggerGen\Swagger\Type\AbstractType|boolean
51
	 */
52
	public function handleCommand($command, $data = null)
53
	{
54
		switch (strtolower($command)) {
55
			case 'example':
56
				if ($data === '') {
57
					throw new \SwaggerGen\Exception("Missing content for type example");
58
				}
59
				$json = preg_replace_callback('/([^{}:]+)/', function($match) {
60
					json_decode($match[1]);
61
					return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]);
62
				}, trim($data));
63
				$this->example = json_decode($json, true);
64
				return $this;
65
		}
66
67
		return false;
68
	}
69
70
	public function toArray()
71
	{
72
		return self::arrayFilterNull(array_merge(array(
73
					'example' => $this->example,
74
								), parent::toArray()));
75
	}
76
77
}
78