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

SwaggerGen/Swagger/Type/Property.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SwaggerGen\Swagger\Type;
4
5
/**
6
 * Describes a property of an object type definition.
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
class Property extends \SwaggerGen\Swagger\AbstractObject
14
{
15
16
	private static $classTypes = array(
17
		'integer' => 'Integer',
18
		'int' => 'Integer',
19
		'int32' => 'Integer',
20
		'int64' => 'Integer',
21
		'long' => 'Integer',
22
		'float' => 'Number',
23
		'double' => 'Number',
24
		'string' => 'String',
25
		'uuid' => 'StringUuid',
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
		'multi' => 'Array',
38
		'date' => 'Date',
39
		'datetime' => 'Date',
40
		'date-time' => 'Date',
41
		'object' => 'Object',
42
		'refobject' => 'ReferenceObject',
43
	);
44
45
	/**
46
	 * Description of this property
47
	 * @var string
48
	 */
49
	private $description;
50
51
    /**
52
     * Whether property is read only
53
     * @var bool
54
     */
55
	private $readOnly;
56
57
	/**
58
	 * Type definition of this property
59
	 * @var \SwaggerGen\Swagger\Type\AbstractType
60
	 */
61
	private $Type;
62
63
	/**
64
	 * Create a new property
65
	 * @param \SwaggerGen\Swagger\AbstractObject $parent
66
	 * @param string $definition Either a built-in type or a definition name
67
	 * @param string $description description of the property
68
     * @param bool $readOnly Whether the property is read only
69
	 * @throws \SwaggerGen\Exception
70
	 */
71
	public function __construct(\SwaggerGen\Swagger\AbstractObject $parent, $definition, $description = null, $readOnly = null)
72
	{
73
		parent::__construct($parent);
74
75
		// Parse regex
76
		$match = array();
77
		if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
78
			// recognized format
79
		} elseif (preg_match('/^(\[)(?:.*?)\]$/i', $definition, $match) === 1) {
80
			$match[1] = 'array';
81
		} elseif (preg_match('/^(\{)(?:.*?)\}$/i', $definition, $match) === 1) {
82
			$match[1] = 'object';
83
		} else {
84
			throw new \SwaggerGen\Exception("Not a property: '{$definition}'");
85
		}
86
		$format = strtolower($match[1]);
87
		if (isset(self::$classTypes[$format])) {
88
			$type = self::$classTypes[$format];
89
			$class = "SwaggerGen\\Swagger\\Type\\{$type}Type";
90
			$this->Type = new $class($this, $definition);
91
		} else {
92
			$this->Type = new \SwaggerGen\Swagger\Type\ReferenceObjectType($this, $definition);
93
		}
94
95
		$this->description = $description;
96
		$this->readOnly = $readOnly;
97
	}
98
99
	/**
100
	 * @param string $command The comment command
101
	 * @param string $data Any data added after the command
102
	 * @return \SwaggerGen\Swagger\Type\AbstractType|boolean
103
	 */
104
	public function handleCommand($command, $data = null)
105
	{
106
		// Pass through to Type
107
		if ($this->Type && $this->Type->handleCommand($command, $data)) {
108
			return $this;
109
		}
110
111
		return parent::handleCommand($command, $data);
112
	}
113
114
	public function toArray()
115
	{
116
		// Reference + readonly/description result in allOf construct
117
		// as it's semantically the same and that's what swagger tools
118
		// like swagger-ui can understand
119
		$requiresWrap =
120
			$this->Type instanceof \SwaggerGen\Swagger\Type\ReferenceObjectType
121
			&& (!empty($this->description) || !is_null($this->readOnly));
122
123
		$valueType = $this->Type->toArray();
124
125
		if ($requiresWrap) {
126
			$valueType = array(
127
				"allOf" => array($valueType),
128
			);
129
		}
130
131
		return self::arrayFilterNull(array_merge($valueType, array(
132
					'description' => empty($this->description) ? null : $this->description,
133
                    'readOnly' => $this->readOnly
134
								), parent::toArray()));
135
	}
136
137
	public function __toString()
138
	{
139
		return __CLASS__;
140
	}
141
142
}
143