Completed
Push — master ( 0ef921...49b538 )
by Martijn
03:08
created

Schema::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerGen\Swagger;
4
5
/**
6
 * Describes a Swagger Schema object, defining a primitive type, object, array
7
 * or reference to a defined schema.
8
 *
9
 * @package    SwaggerGen
10
 * @author     Martijn van der Lee <[email protected]>
11
 * @copyright  2014-2015 Martijn van der Lee
12
 * @license    https://opensource.org/licenses/MIT MIT
13
 */
14
class Schema extends AbstractDocumentableObject implements IDefinition
15
{
16
17
	private static $classTypes = array(
18
		'integer' => 'Integer',
19
		'int' => 'Integer',
20
		'int32' => 'Integer',
21
		'int64' => 'Integer',
22
		'long' => 'Integer',
23
		'float' => 'Number',
24
		'double' => 'Number',
25
		'string' => 'String',
26
		'byte' => 'String',
27
		'binary' => 'String',
28
		'password' => 'String',
29
		'enum' => 'String',
30
		'boolean' => 'Boolean',
31
		'bool' => 'Boolean',
32
		'array' => 'Array',
33
		'csv' => 'Array',
34
		'ssv' => 'Array',
35
		'tsv' => 'Array',
36
		'pipes' => 'Array',
37
		'date' => 'Date',
38
		'datetime' => 'Date',
39
		'date-time' => 'Date',
40
		'object' => 'Object',
41
	);
42
43
	/**
44
	 * @var string
45
	 */
46
	private $description = null;
47
48
	/**
49
	 * @var string
50
	 */
51
	private $title = null;
52
53
	/**
54
	 * @var \SwaggerGen\Swagger\Type\AbstractType
55
	 */
56
	private $type;
57
58
	public function __construct(AbstractObject $parent, $definition = 'object', $description = null)
59
	{
60
		parent::__construct($parent);
61
62
		// Parse regex
63
		$match = array();
64
		preg_match('/^([a-z]+)/i', $definition, $match);
65
		$format = strtolower($match[1]);
66 View Code Duplication
		if (isset(self::$classTypes[$format])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
			$type = self::$classTypes[$format];
68
			$class = "SwaggerGen\\Swagger\\Type\\{$type}Type";
69
			$this->type = new $class($this, $definition);
70
		} else {
71
			$this->type = new Type\ReferenceObjectType($this, $definition);
72
		}
73
74
		$this->description = $description;
75
	}
76
77
	/**
78
	 * @param string $command
79
	 * @param string $data
80
	 * @return \SwaggerGen\Swagger\AbstractObject|boolean
81
	 */
82
	public function handleCommand($command, $data = null)
83
	{
84
		// Pass through to Type
85
		if ($this->type && $this->type->handleCommand($command, $data)) {
86
			return $this;
87
		}
88
89
		// handle all the rest manually
90
		switch (strtolower($command)) {
91
			case 'description':
92
				$this->description = $data;
93
				return $this;
94
				
95
			case 'title':
96
				$this->title = $data;
97
				return $this;
98
		}
99
100
		return parent::handleCommand($command, $data);
101
	}
102
103
	public function toArray()
104
	{
105
		return self::arrayFilterNull(array_merge($this->type->toArray(), array(
106
					'title' => empty($this->title) ? null : $this->title,
107
					'description' => empty($this->description) ? null : $this->description,
108
								), parent::toArray()));
109
	}
110
111
	public function __toString()
112
	{
113
		return __CLASS__;
114
	}
115
116
}
117