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

Schema::toArray()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 1
nop 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
	/**
18
	 * @var string
19
	 */
20
	private $description = null;
21
22
	/**
23
	 * @var string
24
	 */
25
	private $title = null;
26
27
    /**
28
     * @var bool
29
     */
30
    private $readOnly = null;
31
32
	/**
33
	 * @var \SwaggerGen\Swagger\Type\AbstractType
34
	 */
35
	private $type;
36
37
	public function __construct(AbstractObject $parent, $definition = 'object', $description = null)
38
	{
39
		parent::__construct($parent);
40
41
		// Check if definition set
42
		if ($this->getSwagger()->hasDefinition($definition)) {
43
			$this->type = new Type\ReferenceObjectType($this, $definition);
44
		} else {
45
			$this->type = Type\AbstractType::typeFactory($this, $definition);
46
		}
47
48
		$this->description = $description;
49
	}
50
51
	/**
52
	 * @param string $command
53
	 * @param string $data
54
	 * @return \SwaggerGen\Swagger\AbstractObject|boolean
55
	 */
56
	public function handleCommand($command, $data = null)
57
	{
58
		// Pass through to Type
59
		if ($this->type && $this->type->handleCommand($command, $data)) {
60
			return $this;
61
		}
62
63
		// handle all the rest manually
64
		switch (strtolower($command)) {
65
			case 'description':
66
				$this->description = $data;
67
				return $this;
68
				
69
			case 'title':
70
				$this->title = $data;
71
				return $this;
72
		}
73
74
		return parent::handleCommand($command, $data);
75
	}
76
77
	public function toArray()
78
	{
79
		return self::arrayFilterNull(array_merge($this->type->toArray(), array(
80
					'title' => empty($this->title) ? null : $this->title,
81
					'description' => empty($this->description) ? null : $this->description,
82
                    'readOnly' => $this->readOnly
83
								), parent::toArray()));
84
	}
85
86
	public function __toString()
87
	{
88
		return __CLASS__;
89
	}
90
91
    public function setReadOnly()
92
    {
93
        $this->readOnly = true;
94
    }
95
96
}
97