Completed
Pull Request — master (#51)
by
unknown
20s
created
SwaggerGen/Swagger/Type/Custom/EmailType.php 1 patch
Indentation   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -17,77 +17,77 @@
 block discarded – undo
17 17
 class EmailType extends StringType implements ICustomType
18 18
 {
19 19
 
20
-    const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z';
21
-
22
-    /**
23
-     * List of formats recognized by this class
24
-     * @var string[]
25
-     */
26
-    private static $formats = array('email');
27
-
28
-    /**
29
-     * Construct and set up the regular expression for this type
30
-     *
31
-     * @param AbstractObject $parent
32
-     * @param string $definition
33
-     */
34
-    public function __construct(AbstractObject $parent, $definition)
35
-    {
36
-        $this->pattern = self::PATTERN;
37
-
38
-        parent::__construct($parent, $definition);
39
-    }
40
-
41
-    /**
42
-     * Parse a type definition string, assuming it belongs to this type
43
-     *
44
-     * @param string $definition
45
-     * @throws Exception
46
-     */
47
-    protected function parseDefinition($definition)
48
-    {
49
-        $definition = self::trim($definition);
50
-
51
-        $match = array();
52
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
-            throw new Exception("Unparseable email definition: '{$definition}'");
54
-        }
55
-
56
-        if (!in_array(strtolower($match[1]), self::$formats)) {
57
-            throw new Exception("Not an email: '{$definition}'");
58
-        }
59
-
60
-        $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
-    }
62
-
63
-    /**
64
-     * Check (and optionally reformat) a default value
65
-     *
66
-     * @param string $value
67
-     * @return string
68
-     * @throws Exception
69
-     */
70
-    protected function validateDefault($value)
71
-    {
72
-        if (empty($value)) {
73
-            throw new Exception("Empty email default");
74
-        }
75
-
76
-        if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
77
-            throw new Exception("Invalid email default value: '{$value}'");
78
-        }
79
-
80
-        return $value;
81
-    }
82
-
83
-    public static function getFormats()
84
-    {
85
-        return self::$formats;
86
-    }
87
-
88
-    public static function setFormats(array $formats)
89
-    {
90
-        self::$formats = $formats;
91
-    }
20
+	const PATTERN = '\A[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z';
21
+
22
+	/**
23
+	 * List of formats recognized by this class
24
+	 * @var string[]
25
+	 */
26
+	private static $formats = array('email');
27
+
28
+	/**
29
+	 * Construct and set up the regular expression for this type
30
+	 *
31
+	 * @param AbstractObject $parent
32
+	 * @param string $definition
33
+	 */
34
+	public function __construct(AbstractObject $parent, $definition)
35
+	{
36
+		$this->pattern = self::PATTERN;
37
+
38
+		parent::__construct($parent, $definition);
39
+	}
40
+
41
+	/**
42
+	 * Parse a type definition string, assuming it belongs to this type
43
+	 *
44
+	 * @param string $definition
45
+	 * @throws Exception
46
+	 */
47
+	protected function parseDefinition($definition)
48
+	{
49
+		$definition = self::trim($definition);
50
+
51
+		$match = array();
52
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
+			throw new Exception("Unparseable email definition: '{$definition}'");
54
+		}
55
+
56
+		if (!in_array(strtolower($match[1]), self::$formats)) {
57
+			throw new Exception("Not an email: '{$definition}'");
58
+		}
59
+
60
+		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
+	}
62
+
63
+	/**
64
+	 * Check (and optionally reformat) a default value
65
+	 *
66
+	 * @param string $value
67
+	 * @return string
68
+	 * @throws Exception
69
+	 */
70
+	protected function validateDefault($value)
71
+	{
72
+		if (empty($value)) {
73
+			throw new Exception("Empty email default");
74
+		}
75
+
76
+		if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
77
+			throw new Exception("Invalid email default value: '{$value}'");
78
+		}
79
+
80
+		return $value;
81
+	}
82
+
83
+	public static function getFormats()
84
+	{
85
+		return self::$formats;
86
+	}
87
+
88
+	public static function setFormats(array $formats)
89
+	{
90
+		self::$formats = $formats;
91
+	}
92 92
 
93 93
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/Custom/Ipv4Type.php 1 patch
Indentation   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -17,77 +17,77 @@
 block discarded – undo
17 17
 class Ipv4Type extends StringType implements ICustomType
18 18
 {
19 19
 
20
-    const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$';
21
-
22
-    /**
23
-     * List of formats recognized by this class
24
-     * @var string[]
25
-     */
26
-    private static $formats = array('ipv4');
27
-
28
-    /**
29
-     * Construct and set up the regular expression for this type
30
-     *
31
-     * @param AbstractObject $parent
32
-     * @param string $definition
33
-     */
34
-    public function __construct(AbstractObject $parent, $definition)
35
-    {
36
-        $this->pattern = self::PATTERN;
37
-
38
-        parent::__construct($parent, $definition);
39
-    }
40
-
41
-    /**
42
-     * Parse a type definition string, assuming it belongs to this type
43
-     *
44
-     * @param string $definition
45
-     * @throws Exception
46
-     */
47
-    protected function parseDefinition($definition)
48
-    {
49
-        $definition = self::trim($definition);
50
-
51
-        $match = array();
52
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
-            throw new Exception("Unparseable IPv4 definition: '{$definition}'");
54
-        }
55
-
56
-        if (!in_array(strtolower($match[1]), self::$formats)) {
57
-            throw new Exception("Not an IPv4: '{$definition}'");
58
-        }
59
-
60
-        $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
-    }
62
-
63
-    /**
64
-     * Check (and optionally reformat) a default value
65
-     *
66
-     * @param string $value
67
-     * @return string
68
-     * @throws Exception
69
-     */
70
-    protected function validateDefault($value)
71
-    {
72
-        if (empty($value)) {
73
-            throw new Exception("Empty IPv4 default");
74
-        }
75
-
76
-        if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
77
-            throw new Exception("Invalid IPv4 default value: '{$value}'");
78
-        }
79
-
80
-        return $value;
81
-    }
82
-
83
-    public static function getFormats()
84
-    {
85
-        return self::$formats;
86
-    }
87
-
88
-    public static function setFormats(array $formats)
89
-    {
90
-        self::$formats = $formats;
91
-    }
20
+	const PATTERN = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}$';
21
+
22
+	/**
23
+	 * List of formats recognized by this class
24
+	 * @var string[]
25
+	 */
26
+	private static $formats = array('ipv4');
27
+
28
+	/**
29
+	 * Construct and set up the regular expression for this type
30
+	 *
31
+	 * @param AbstractObject $parent
32
+	 * @param string $definition
33
+	 */
34
+	public function __construct(AbstractObject $parent, $definition)
35
+	{
36
+		$this->pattern = self::PATTERN;
37
+
38
+		parent::__construct($parent, $definition);
39
+	}
40
+
41
+	/**
42
+	 * Parse a type definition string, assuming it belongs to this type
43
+	 *
44
+	 * @param string $definition
45
+	 * @throws Exception
46
+	 */
47
+	protected function parseDefinition($definition)
48
+	{
49
+		$definition = self::trim($definition);
50
+
51
+		$match = array();
52
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
+			throw new Exception("Unparseable IPv4 definition: '{$definition}'");
54
+		}
55
+
56
+		if (!in_array(strtolower($match[1]), self::$formats)) {
57
+			throw new Exception("Not an IPv4: '{$definition}'");
58
+		}
59
+
60
+		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
+	}
62
+
63
+	/**
64
+	 * Check (and optionally reformat) a default value
65
+	 *
66
+	 * @param string $value
67
+	 * @return string
68
+	 * @throws Exception
69
+	 */
70
+	protected function validateDefault($value)
71
+	{
72
+		if (empty($value)) {
73
+			throw new Exception("Empty IPv4 default");
74
+		}
75
+
76
+		if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
77
+			throw new Exception("Invalid IPv4 default value: '{$value}'");
78
+		}
79
+
80
+		return $value;
81
+	}
82
+
83
+	public static function getFormats()
84
+	{
85
+		return self::$formats;
86
+	}
87
+
88
+	public static function setFormats(array $formats)
89
+	{
90
+		self::$formats = $formats;
91
+	}
92 92
 
93 93
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/Custom/Ipv6Type.php 1 patch
Indentation   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -17,77 +17,77 @@
 block discarded – undo
17 17
 class Ipv6Type extends StringType implements ICustomType
18 18
 {
19 19
 
20
-    const PATTERN = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))';
21
-
22
-    /**
23
-     * List of formats recognized by this class
24
-     * @var string[]
25
-     */
26
-    private static $formats = array('ipv6');
27
-
28
-    /**
29
-     * Construct and set up the regular expression for this type
30
-     *
31
-     * @param AbstractObject $parent
32
-     * @param string $definition
33
-     */
34
-    public function __construct(AbstractObject $parent, $definition)
35
-    {
36
-        $this->pattern = self::PATTERN;
37
-
38
-        parent::__construct($parent, $definition);
39
-    }
40
-
41
-    /**
42
-     * Parse a type definition string, assuming it belongs to this type
43
-     *
44
-     * @param string $definition
45
-     * @throws Exception
46
-     */
47
-    protected function parseDefinition($definition)
48
-    {
49
-        $definition = self::trim($definition);
50
-
51
-        $match = array();
52
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
-            throw new Exception("Unparseable IPv6 definition: '{$definition}'");
54
-        }
55
-
56
-        if (!in_array(strtolower($match[1]), self::$formats)) {
57
-            throw new Exception("Not an IPv6: '{$definition}'");
58
-        }
59
-
60
-        $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
-    }
62
-
63
-    /**
64
-     * Check (and optionally reformat) a default value
65
-     *
66
-     * @param string $value
67
-     * @return string
68
-     * @throws Exception
69
-     */
70
-    protected function validateDefault($value)
71
-    {
72
-        if (empty($value)) {
73
-            throw new Exception("Empty IPv6 default");
74
-        }
75
-
76
-        if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
77
-            throw new Exception("Invalid IPv6 default value: '{$value}'");
78
-        }
79
-
80
-        return $value;
81
-    }
82
-
83
-    public static function getFormats()
84
-    {
85
-        return self::$formats;
86
-    }
87
-
88
-    public static function setFormats(array $formats)
89
-    {
90
-        self::$formats = $formats;
91
-    }
20
+	const PATTERN = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))';
21
+
22
+	/**
23
+	 * List of formats recognized by this class
24
+	 * @var string[]
25
+	 */
26
+	private static $formats = array('ipv6');
27
+
28
+	/**
29
+	 * Construct and set up the regular expression for this type
30
+	 *
31
+	 * @param AbstractObject $parent
32
+	 * @param string $definition
33
+	 */
34
+	public function __construct(AbstractObject $parent, $definition)
35
+	{
36
+		$this->pattern = self::PATTERN;
37
+
38
+		parent::__construct($parent, $definition);
39
+	}
40
+
41
+	/**
42
+	 * Parse a type definition string, assuming it belongs to this type
43
+	 *
44
+	 * @param string $definition
45
+	 * @throws Exception
46
+	 */
47
+	protected function parseDefinition($definition)
48
+	{
49
+		$definition = self::trim($definition);
50
+
51
+		$match = array();
52
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
53
+			throw new Exception("Unparseable IPv6 definition: '{$definition}'");
54
+		}
55
+
56
+		if (!in_array(strtolower($match[1]), self::$formats)) {
57
+			throw new Exception("Not an IPv6: '{$definition}'");
58
+		}
59
+
60
+		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
61
+	}
62
+
63
+	/**
64
+	 * Check (and optionally reformat) a default value
65
+	 *
66
+	 * @param string $value
67
+	 * @return string
68
+	 * @throws Exception
69
+	 */
70
+	protected function validateDefault($value)
71
+	{
72
+		if (empty($value)) {
73
+			throw new Exception("Empty IPv6 default");
74
+		}
75
+
76
+		if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
77
+			throw new Exception("Invalid IPv6 default value: '{$value}'");
78
+		}
79
+
80
+		return $value;
81
+	}
82
+
83
+	public static function getFormats()
84
+	{
85
+		return self::$formats;
86
+	}
87
+
88
+	public static function setFormats(array $formats)
89
+	{
90
+		self::$formats = $formats;
91
+	}
92 92
 
93 93
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/ArrayType.php 1 patch
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -16,181 +16,181 @@
 block discarded – undo
16 16
 class ArrayType extends AbstractType
17 17
 {
18 18
 
19
-    const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?';
20
-
21
-    private static $collectionFormats = array(
22
-        'array' => 'csv',
23
-        'csv' => 'csv',
24
-        'ssv' => 'ssv',
25
-        'tsv' => 'tsv',
26
-        'pipes' => 'pipes',
27
-        'multi' => 'multi',
28
-    );
29
-
30
-    /**
31
-     * @var AbstractType
32
-     */
33
-    private $Items = null;
34
-    private $minItems = null;
35
-    private $maxItems = null;
36
-    private $collectionFormat = null;
37
-
38
-    /**
39
-     * @throws Exception
40
-     */
41
-    protected function parseDefinition($definition)
42
-    {
43
-        $definition = self::trim($definition);
44
-        $match = array();
45
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
46
-            $match[1] = strtolower($match[1]);
47
-        } elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
48
-            $match[1] = 'array';
49
-        } else {
50
-            throw new Exception('Unparseable array definition: \'' . $definition . '\'');
51
-        }
52
-
53
-        $this->parseFormat($definition, $match);
54
-        $this->parseItems($definition, $match);
55
-        $this->parseRange($definition, $match);
56
-    }
57
-
58
-    /**
59
-     * @param string $definition
60
-     * @param string[] $match
61
-     * @throws Exception
62
-     * @throws Exception
63
-     */
64
-    private function parseFormat($definition, $match)
65
-    {
66
-        $type = strtolower($match[1]);
67
-        if (!isset(self::$collectionFormats[$type])) {
68
-            throw new Exception("Not an array: '{$definition}'");
69
-        }
70
-
71
-        if ($type === 'multi') {
72
-            $parent = $this->getParent();
73
-            if (!($parent instanceof Parameter) || !$parent->isMulti()) {
74
-                throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'");
75
-            }
76
-        }
77
-
78
-        $this->collectionFormat = self::$collectionFormats[$type];
79
-    }
80
-
81
-    /**
82
-     * @param string $definition
83
-     * @param string[] $match
84
-     * @throws Exception
85
-     */
86
-    private function parseItems($definition, $match)
87
-    {
88
-        if (!empty($match[2])) {
89
-            $this->Items = $this->validateItems($match[2]);
90
-        }
91
-    }
92
-
93
-    /**
94
-     * @param string $definition
95
-     * @param string[] $match
96
-     * @throws Exception
97
-     */
98
-    private function parseRange($definition, $match)
99
-    {
100
-        if (!empty($match[3])) {
101
-            if ($match[4] === '' && $match[5] === '') {
102
-                throw new Exception("Empty array range: '{$definition}'");
103
-            }
104
-
105
-            $exclusiveMinimum = $match[3] == '<';
106
-            $this->minItems = $match[4] === '' ? null : intval($match[4]);
107
-            $this->maxItems = $match[5] === '' ? null : intval($match[5]);
108
-            $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
109
-            if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) {
110
-                self::swap($this->minItems, $this->maxItems);
111
-                self::swap($exclusiveMinimum, $exclusiveMaximum);
112
-            }
113
-            $this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems);
114
-            $this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems);
115
-        }
116
-    }
117
-
118
-    /**
119
-     * @param string $command The comment command
120
-     * @param string $data Any data added after the command
121
-     * @return AbstractType|boolean
122
-     * @throws Exception
123
-     * @throws Exception
124
-     * @throws Exception
125
-     * @throws Exception
126
-     * @throws Exception
127
-     * @throws Exception
128
-     * @throws Exception
129
-     */
130
-    public function handleCommand($command, $data = null)
131
-    {
132
-        if ($this->Items) {
133
-            $return = $this->Items->handleCommand($command, $data);
134
-            if ($return) {
135
-                return $return;
136
-            }
137
-        }
138
-
139
-        switch (strtolower($command)) {
140
-            case 'min':
141
-                $this->minItems = intval($data);
142
-                if ($this->minItems < 0) {
143
-                    throw new Exception("Minimum less than zero: '{$data}'");
144
-                }
145
-                if ($this->maxItems !== null && $this->minItems > $this->maxItems) {
146
-                    throw new Exception("Minimum greater than maximum: '{$data}'");
147
-                }
148
-                return $this;
149
-
150
-            case 'max':
151
-                $this->maxItems = intval($data);
152
-                if ($this->minItems !== null && $this->minItems > $this->maxItems) {
153
-                    throw new Exception("Maximum less than minimum: '{$data}'");
154
-                }
155
-                if ($this->maxItems < 0) {
156
-                    throw new Exception("Maximum less than zero: '{$data}'");
157
-                }
158
-                return $this;
159
-
160
-            case 'items':
161
-                $this->Items = $this->validateItems($data);
162
-                return $this->Items;
163
-        }
164
-
165
-        return parent::handleCommand($command, $data);
166
-    }
167
-
168
-    public function toArray()
169
-    {
170
-        return self::arrayFilterNull(array_merge(array(
171
-            'type' => 'array',
172
-            'items' => empty($this->Items) ? null : $this->Items->toArray(),
173
-            'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat,
174
-            'minItems' => $this->minItems,
175
-            'maxItems' => $this->maxItems,
176
-        ), parent::toArray()));
177
-    }
178
-
179
-    /**
180
-     * @throws Exception
181
-     */
182
-    private function validateItems($items)
183
-    {
184
-        if (empty($items)) {
185
-            throw new Exception("Empty items definition: '{$items}'");
186
-        }
187
-
188
-        return self::typeFactory($this, $items, "Unparseable items definition: '%s'");
189
-    }
190
-
191
-    public function __toString()
192
-    {
193
-        return __CLASS__;
194
-    }
19
+	const REGEX_ARRAY_CONTENT = '(?:(\[)(.*)\])?';
20
+
21
+	private static $collectionFormats = array(
22
+		'array' => 'csv',
23
+		'csv' => 'csv',
24
+		'ssv' => 'ssv',
25
+		'tsv' => 'tsv',
26
+		'pipes' => 'pipes',
27
+		'multi' => 'multi',
28
+	);
29
+
30
+	/**
31
+	 * @var AbstractType
32
+	 */
33
+	private $Items = null;
34
+	private $minItems = null;
35
+	private $maxItems = null;
36
+	private $collectionFormat = null;
37
+
38
+	/**
39
+	 * @throws Exception
40
+	 */
41
+	protected function parseDefinition($definition)
42
+	{
43
+		$definition = self::trim($definition);
44
+		$match = array();
45
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
46
+			$match[1] = strtolower($match[1]);
47
+		} elseif (preg_match(self::REGEX_START . self::REGEX_ARRAY_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
48
+			$match[1] = 'array';
49
+		} else {
50
+			throw new Exception('Unparseable array definition: \'' . $definition . '\'');
51
+		}
52
+
53
+		$this->parseFormat($definition, $match);
54
+		$this->parseItems($definition, $match);
55
+		$this->parseRange($definition, $match);
56
+	}
57
+
58
+	/**
59
+	 * @param string $definition
60
+	 * @param string[] $match
61
+	 * @throws Exception
62
+	 * @throws Exception
63
+	 */
64
+	private function parseFormat($definition, $match)
65
+	{
66
+		$type = strtolower($match[1]);
67
+		if (!isset(self::$collectionFormats[$type])) {
68
+			throw new Exception("Not an array: '{$definition}'");
69
+		}
70
+
71
+		if ($type === 'multi') {
72
+			$parent = $this->getParent();
73
+			if (!($parent instanceof Parameter) || !$parent->isMulti()) {
74
+				throw new Exception("Multi array only allowed on query or form parameter: '{$definition}'");
75
+			}
76
+		}
77
+
78
+		$this->collectionFormat = self::$collectionFormats[$type];
79
+	}
80
+
81
+	/**
82
+	 * @param string $definition
83
+	 * @param string[] $match
84
+	 * @throws Exception
85
+	 */
86
+	private function parseItems($definition, $match)
87
+	{
88
+		if (!empty($match[2])) {
89
+			$this->Items = $this->validateItems($match[2]);
90
+		}
91
+	}
92
+
93
+	/**
94
+	 * @param string $definition
95
+	 * @param string[] $match
96
+	 * @throws Exception
97
+	 */
98
+	private function parseRange($definition, $match)
99
+	{
100
+		if (!empty($match[3])) {
101
+			if ($match[4] === '' && $match[5] === '') {
102
+				throw new Exception("Empty array range: '{$definition}'");
103
+			}
104
+
105
+			$exclusiveMinimum = $match[3] == '<';
106
+			$this->minItems = $match[4] === '' ? null : intval($match[4]);
107
+			$this->maxItems = $match[5] === '' ? null : intval($match[5]);
108
+			$exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
109
+			if ($this->minItems && $this->maxItems && $this->minItems > $this->maxItems) {
110
+				self::swap($this->minItems, $this->maxItems);
111
+				self::swap($exclusiveMinimum, $exclusiveMaximum);
112
+			}
113
+			$this->minItems = $this->minItems === null ? null : max(0, $exclusiveMinimum ? $this->minItems + 1 : $this->minItems);
114
+			$this->maxItems = $this->maxItems === null ? null : max(0, $exclusiveMaximum ? $this->maxItems - 1 : $this->maxItems);
115
+		}
116
+	}
117
+
118
+	/**
119
+	 * @param string $command The comment command
120
+	 * @param string $data Any data added after the command
121
+	 * @return AbstractType|boolean
122
+	 * @throws Exception
123
+	 * @throws Exception
124
+	 * @throws Exception
125
+	 * @throws Exception
126
+	 * @throws Exception
127
+	 * @throws Exception
128
+	 * @throws Exception
129
+	 */
130
+	public function handleCommand($command, $data = null)
131
+	{
132
+		if ($this->Items) {
133
+			$return = $this->Items->handleCommand($command, $data);
134
+			if ($return) {
135
+				return $return;
136
+			}
137
+		}
138
+
139
+		switch (strtolower($command)) {
140
+			case 'min':
141
+				$this->minItems = intval($data);
142
+				if ($this->minItems < 0) {
143
+					throw new Exception("Minimum less than zero: '{$data}'");
144
+				}
145
+				if ($this->maxItems !== null && $this->minItems > $this->maxItems) {
146
+					throw new Exception("Minimum greater than maximum: '{$data}'");
147
+				}
148
+				return $this;
149
+
150
+			case 'max':
151
+				$this->maxItems = intval($data);
152
+				if ($this->minItems !== null && $this->minItems > $this->maxItems) {
153
+					throw new Exception("Maximum less than minimum: '{$data}'");
154
+				}
155
+				if ($this->maxItems < 0) {
156
+					throw new Exception("Maximum less than zero: '{$data}'");
157
+				}
158
+				return $this;
159
+
160
+			case 'items':
161
+				$this->Items = $this->validateItems($data);
162
+				return $this->Items;
163
+		}
164
+
165
+		return parent::handleCommand($command, $data);
166
+	}
167
+
168
+	public function toArray()
169
+	{
170
+		return self::arrayFilterNull(array_merge(array(
171
+			'type' => 'array',
172
+			'items' => empty($this->Items) ? null : $this->Items->toArray(),
173
+			'collectionFormat' => $this->collectionFormat == 'csv' ? null : $this->collectionFormat,
174
+			'minItems' => $this->minItems,
175
+			'maxItems' => $this->maxItems,
176
+		), parent::toArray()));
177
+	}
178
+
179
+	/**
180
+	 * @throws Exception
181
+	 */
182
+	private function validateItems($items)
183
+	{
184
+		if (empty($items)) {
185
+			throw new Exception("Empty items definition: '{$items}'");
186
+		}
187
+
188
+		return self::typeFactory($this, $items, "Unparseable items definition: '%s'");
189
+	}
190
+
191
+	public function __toString()
192
+	{
193
+		return __CLASS__;
194
+	}
195 195
 
196 196
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/AbstractRegexType.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -16,61 +16,61 @@
 block discarded – undo
16 16
 class AbstractRegexType extends StringType
17 17
 {
18 18
 
19
-    const REGEX_DEFAULT_START = '(?:=(';
20
-    const REGEX_DEFAULT_END = '))?';
19
+	const REGEX_DEFAULT_START = '(?:=(';
20
+	const REGEX_DEFAULT_END = '))?';
21 21
 
22
-    /**
23
-     * The raw regular expression to use.
24
-     * Exclude start (`^`) and end (`$`) anchors.
25
-     * @var string
26
-     */
27
-    private $regex;
22
+	/**
23
+	 * The raw regular expression to use.
24
+	 * Exclude start (`^`) and end (`$`) anchors.
25
+	 * @var string
26
+	 */
27
+	private $regex;
28 28
 
29
-    /**
30
-     * Construct and set up the regular expression for this type
31
-     *
32
-     * @param AbstractObject $parent
33
-     * @param string $definition
34
-     * @param string $format Name of the string format
35
-     * @param string $regex Regular expression to use as the format and for default validation
36
-     */
37
-    public function __construct(AbstractObject $parent, $definition, $format, $regex)
38
-    {
39
-        $this->format = $format;
40
-        $this->regex = $regex;
29
+	/**
30
+	 * Construct and set up the regular expression for this type
31
+	 *
32
+	 * @param AbstractObject $parent
33
+	 * @param string $definition
34
+	 * @param string $format Name of the string format
35
+	 * @param string $regex Regular expression to use as the format and for default validation
36
+	 */
37
+	public function __construct(AbstractObject $parent, $definition, $format, $regex)
38
+	{
39
+		$this->format = $format;
40
+		$this->regex = $regex;
41 41
 
42
-        parent::__construct($parent, $definition);
43
-    }
42
+		parent::__construct($parent, $definition);
43
+	}
44 44
 
45
-    /**
46
-     * @throws Exception
47
-     */
48
-    protected function parseDefinition($definition)
49
-    {
50
-        $definition = self::trim($definition);
45
+	/**
46
+	 * @throws Exception
47
+	 */
48
+	protected function parseDefinition($definition)
49
+	{
50
+		$definition = self::trim($definition);
51 51
 
52
-        $match = array();
53
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) {
54
-            throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\'');
55
-        }
52
+		$match = array();
53
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT_START . $this->regex . self::REGEX_DEFAULT_END . self::REGEX_END, $definition, $match) !== 1) {
54
+			throw new Exception('Unparseable ' . $this->format . ' definition: \'' . $definition . '\'');
55
+		}
56 56
 
57
-        if (strtolower($match[1] !== $this->format)) {
58
-            throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\'');
59
-        }
57
+		if (strtolower($match[1] !== $this->format)) {
58
+			throw new Exception('Not a ' . $this->format . ': \'' . $definition . '\'');
59
+		}
60 60
 
61
-        $this->pattern = '^' . $this->regex . '$';
62
-        $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
63
-    }
61
+		$this->pattern = '^' . $this->regex . '$';
62
+		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
63
+	}
64 64
 
65
-    protected function validateDefault($value)
66
-    {
67
-        $value = parent::validateDefault($value);
65
+	protected function validateDefault($value)
66
+	{
67
+		$value = parent::validateDefault($value);
68 68
 
69
-        if (preg_match('/' . $this->pattern . '/', $value) !== 1) {
70
-            throw new Exception('Invalid ' . $this->format . ' default value');
71
-        }
69
+		if (preg_match('/' . $this->pattern . '/', $value) !== 1) {
70
+			throw new Exception('Invalid ' . $this->format . ' default value');
71
+		}
72 72
 
73
-        return $value;
74
-    }
73
+		return $value;
74
+	}
75 75
 
76 76
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/ObjectType.php 1 patch
Indentation   +250 added lines, -250 removed lines patch added patch discarded remove patch
@@ -15,281 +15,281 @@
 block discarded – undo
15 15
 class ObjectType extends AbstractType
16 16
 {
17 17
 
18
-    const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?';
19
-    const REGEX_PROP_START = '/^';
20
-    const REGEX_PROP_NAME = '([^?!:]+)';
21
-    const REGEX_PROP_REQUIRED = '([\?!])?';
22
-    const REGEX_PROP_ASSIGN = ':';
23
-    const REGEX_PROP_DEFINITION = '(.+)';
24
-    const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?';
25
-    const REGEX_PROP_END = '$/';
18
+	const REGEX_OBJECT_CONTENT = '(?:(\{)(.*)\})?';
19
+	const REGEX_PROP_START = '/^';
20
+	const REGEX_PROP_NAME = '([^?!:]+)';
21
+	const REGEX_PROP_REQUIRED = '([\?!])?';
22
+	const REGEX_PROP_ASSIGN = ':';
23
+	const REGEX_PROP_DEFINITION = '(.+)';
24
+	const REGEX_PROP_ADDITIONAL = '\.\.\.(!|.+)?';
25
+	const REGEX_PROP_END = '$/';
26 26
 
27
-    private $minProperties = null;
28
-    private $maxProperties = null;
29
-    private $discriminator = null;
30
-    private $additionalProperties = null;
31
-    private $required = array();
27
+	private $minProperties = null;
28
+	private $maxProperties = null;
29
+	private $discriminator = null;
30
+	private $additionalProperties = null;
31
+	private $required = array();
32 32
 
33
-    /**
34
-     * @var Property[]
35
-     */
36
-    private $properties = array();
33
+	/**
34
+	 * @var Property[]
35
+	 */
36
+	private $properties = array();
37 37
 
38
-    /**
39
-     * @var Property
40
-     */
41
-    private $mostRecentProperty = null;
38
+	/**
39
+	 * @var Property
40
+	 */
41
+	private $mostRecentProperty = null;
42 42
 
43
-    /**
44
-     * @throws Exception
45
-     */
46
-    protected function parseDefinition($definition)
47
-    {
48
-        $definition = self::trim($definition);
43
+	/**
44
+	 * @throws Exception
45
+	 */
46
+	protected function parseDefinition($definition)
47
+	{
48
+		$definition = self::trim($definition);
49 49
 
50
-        $match = array();
51
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
52
-            $match[1] = strtolower($match[1]);
53
-        } elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
54
-            $match[1] = 'object';
55
-        } else {
56
-            throw new Exception('Unparseable object definition: \'' . $definition . '\'');
57
-        }
50
+		$match = array();
51
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
52
+			$match[1] = strtolower($match[1]);
53
+		} elseif (preg_match(self::REGEX_START . self::REGEX_OBJECT_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) === 1) {
54
+			$match[1] = 'object';
55
+		} else {
56
+			throw new Exception('Unparseable object definition: \'' . $definition . '\'');
57
+		}
58 58
 
59
-        $this->parseFormat($definition, $match);
60
-        $this->parseProperties($definition, $match);
61
-        $this->parseRange($definition, $match);
62
-    }
59
+		$this->parseFormat($definition, $match);
60
+		$this->parseProperties($definition, $match);
61
+		$this->parseRange($definition, $match);
62
+	}
63 63
 
64
-    /**
65
-     * @param string $definition
66
-     * @param string[] $match
67
-     * @throws Exception
68
-     */
69
-    private function parseFormat($definition, $match)
70
-    {
71
-        if (strtolower($match[1]) !== 'object') {
72
-            throw new Exception("Not an object: '{$definition}'");
73
-        }
74
-    }
64
+	/**
65
+	 * @param string $definition
66
+	 * @param string[] $match
67
+	 * @throws Exception
68
+	 */
69
+	private function parseFormat($definition, $match)
70
+	{
71
+		if (strtolower($match[1]) !== 'object') {
72
+			throw new Exception("Not an object: '{$definition}'");
73
+		}
74
+	}
75 75
 
76
-    /**
77
-     * @param AbstractType|bool $type
78
-     * @return void
79
-     * @throws Exception
80
-     */
81
-    private function setAdditionalProperties($type)
82
-    {
83
-        if ($this->additionalProperties !== null) {
84
-            throw new Exception('Additional properties may only be set once');
85
-        }
86
-        $this->additionalProperties = $type;
87
-    }
76
+	/**
77
+	 * @param AbstractType|bool $type
78
+	 * @return void
79
+	 * @throws Exception
80
+	 */
81
+	private function setAdditionalProperties($type)
82
+	{
83
+		if ($this->additionalProperties !== null) {
84
+			throw new Exception('Additional properties may only be set once');
85
+		}
86
+		$this->additionalProperties = $type;
87
+	}
88 88
 
89
-    /**
90
-     * @param string $definition
91
-     * @param string[] $match
92
-     * @throws Exception
93
-     * @throws Exception
94
-     * @throws Exception
95
-     * @throws Exception
96
-     * @throws Exception
97
-     * @throws Exception
98
-     */
99
-    private function parseProperties($definition, $match)
100
-    {
101
-        if (empty($match[2])) {
102
-            return;
103
-        }
89
+	/**
90
+	 * @param string $definition
91
+	 * @param string[] $match
92
+	 * @throws Exception
93
+	 * @throws Exception
94
+	 * @throws Exception
95
+	 * @throws Exception
96
+	 * @throws Exception
97
+	 * @throws Exception
98
+	 */
99
+	private function parseProperties($definition, $match)
100
+	{
101
+		if (empty($match[2])) {
102
+			return;
103
+		}
104 104
 
105
-        while (($property = self::parseListItem($match[2])) !== '') {
106
-            $prop_match = array();
107
-            if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) {
108
-                if (empty($prop_match[1])) {
109
-                    $this->setAdditionalProperties(true);
110
-                } else if ($prop_match[1] === '!') {
111
-                    $this->setAdditionalProperties(false);
112
-                } else {
113
-                    $this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'"));
114
-                }
115
-                continue;
116
-            }
117
-            if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) {
118
-                throw new Exception("Unparseable property definition: '{$property}'");
119
-            }
120
-            $this->properties[$prop_match[1]] = new Property($this, $prop_match[3]);
121
-            if ($prop_match[2] !== '!' && $prop_match[2] !== '?') {
122
-                $this->required[$prop_match[1]] = true;
123
-            }
124
-        }
105
+		while (($property = self::parseListItem($match[2])) !== '') {
106
+			$prop_match = array();
107
+			if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_ADDITIONAL . self::REGEX_PROP_END, $property, $prop_match) === 1) {
108
+				if (empty($prop_match[1])) {
109
+					$this->setAdditionalProperties(true);
110
+				} else if ($prop_match[1] === '!') {
111
+					$this->setAdditionalProperties(false);
112
+				} else {
113
+					$this->setAdditionalProperties(self::typeFactory($this, $prop_match[1], "Unparseable additional properties definition: '...%s'"));
114
+				}
115
+				continue;
116
+			}
117
+			if (preg_match(self::REGEX_PROP_START . self::REGEX_PROP_NAME . self::REGEX_PROP_REQUIRED . self::REGEX_PROP_ASSIGN . self::REGEX_PROP_DEFINITION . self::REGEX_PROP_END, $property, $prop_match) !== 1) {
118
+				throw new Exception("Unparseable property definition: '{$property}'");
119
+			}
120
+			$this->properties[$prop_match[1]] = new Property($this, $prop_match[3]);
121
+			if ($prop_match[2] !== '!' && $prop_match[2] !== '?') {
122
+				$this->required[$prop_match[1]] = true;
123
+			}
124
+		}
125 125
 
126
-    }
126
+	}
127 127
 
128
-    /**
129
-     * @param string $definition
130
-     * @param string[] $match
131
-     * @throws Exception
132
-     */
133
-    private function parseRange($definition, $match)
134
-    {
135
-        if (!empty($match[3])) {
136
-            if ($match[4] === '' && $match[5] === '') {
137
-                throw new Exception("Empty object range: '{$definition}'");
138
-            }
128
+	/**
129
+	 * @param string $definition
130
+	 * @param string[] $match
131
+	 * @throws Exception
132
+	 */
133
+	private function parseRange($definition, $match)
134
+	{
135
+		if (!empty($match[3])) {
136
+			if ($match[4] === '' && $match[5] === '') {
137
+				throw new Exception("Empty object range: '{$definition}'");
138
+			}
139 139
 
140
-            $exclusiveMinimum = $match[3] == '<';
141
-            $this->minProperties = $match[4] === '' ? null : intval($match[4]);
142
-            $this->maxProperties = $match[5] === '' ? null : intval($match[5]);
143
-            $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
144
-            if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) {
145
-                self::swap($this->minProperties, $this->maxProperties);
146
-                self::swap($exclusiveMinimum, $exclusiveMaximum);
147
-            }
148
-            $this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties);
149
-            $this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties);
150
-        }
151
-    }
140
+			$exclusiveMinimum = $match[3] == '<';
141
+			$this->minProperties = $match[4] === '' ? null : intval($match[4]);
142
+			$this->maxProperties = $match[5] === '' ? null : intval($match[5]);
143
+			$exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
144
+			if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) {
145
+				self::swap($this->minProperties, $this->maxProperties);
146
+				self::swap($exclusiveMinimum, $exclusiveMaximum);
147
+			}
148
+			$this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties);
149
+			$this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties);
150
+		}
151
+	}
152 152
 
153
-    /**
154
-     * @param string|false $discriminator
155
-     * @throws Exception
156
-     * @throws Exception
157
-     */
158
-    private function setDiscriminator($discriminator)
159
-    {
160
-        if (!empty($this->discriminator)) {
161
-            throw new Exception("Discriminator may only be set once, "
162
-                . "trying to change it "
163
-                . "from '{$this->discriminator}' "
164
-                . "to '{$discriminator}'");
165
-        }
166
-        if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) {
167
-            throw new Exception("Discriminator must be a required property, "
168
-                . "property '{$discriminator}' is not required");
169
-        }
170
-        $this->discriminator = $discriminator;
171
-    }
153
+	/**
154
+	 * @param string|false $discriminator
155
+	 * @throws Exception
156
+	 * @throws Exception
157
+	 */
158
+	private function setDiscriminator($discriminator)
159
+	{
160
+		if (!empty($this->discriminator)) {
161
+			throw new Exception("Discriminator may only be set once, "
162
+				. "trying to change it "
163
+				. "from '{$this->discriminator}' "
164
+				. "to '{$discriminator}'");
165
+		}
166
+		if (isset($this->properties[$discriminator]) && empty($this->required[$discriminator])) {
167
+			throw new Exception("Discriminator must be a required property, "
168
+				. "property '{$discriminator}' is not required");
169
+		}
170
+		$this->discriminator = $discriminator;
171
+	}
172 172
 
173
-    /**
174
-     * @param string $command The comment command
175
-     * @param string $data Any data added after the command
176
-     * @return AbstractType|boolean
177
-     * @throws Exception
178
-     * @throws Exception
179
-     * @throws Exception
180
-     * @throws Exception
181
-     * @throws Exception
182
-     * @throws Exception
183
-     * @throws Exception
184
-     * @throws Exception
185
-     * @throws Exception
186
-     * @throws Exception
187
-     * @throws Exception
188
-     * @throws Exception
189
-     */
190
-    public function handleCommand($command, $data = null)
191
-    {
192
-        switch (strtolower($command)) {
193
-            // type name description...
194
-            case 'additionalproperties':
195
-                $value = self::wordShift($data);
196
-                if (is_bool($value)) {
197
-                    $type = $value;
198
-                } else if ($value === 'false') {
199
-                    $type = false;
200
-                } else if ($value === 'true') {
201
-                    $type = true;
202
-                } else {
203
-                    $type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'");
204
-                }
205
-                $this->setAdditionalProperties($type);
206
-                return $this;
207
-            case 'discriminator':
208
-                $discriminator = self::wordShift($data);
209
-                $this->setDiscriminator($discriminator);
210
-                return $this;
211
-            case 'property':
212
-            case 'property?':
213
-            case 'property!':
214
-                $definition = self::wordShift($data);
215
-                if (empty($definition)) {
216
-                    throw new Exception("Missing property definition");
217
-                }
173
+	/**
174
+	 * @param string $command The comment command
175
+	 * @param string $data Any data added after the command
176
+	 * @return AbstractType|boolean
177
+	 * @throws Exception
178
+	 * @throws Exception
179
+	 * @throws Exception
180
+	 * @throws Exception
181
+	 * @throws Exception
182
+	 * @throws Exception
183
+	 * @throws Exception
184
+	 * @throws Exception
185
+	 * @throws Exception
186
+	 * @throws Exception
187
+	 * @throws Exception
188
+	 * @throws Exception
189
+	 */
190
+	public function handleCommand($command, $data = null)
191
+	{
192
+		switch (strtolower($command)) {
193
+			// type name description...
194
+			case 'additionalproperties':
195
+				$value = self::wordShift($data);
196
+				if (is_bool($value)) {
197
+					$type = $value;
198
+				} else if ($value === 'false') {
199
+					$type = false;
200
+				} else if ($value === 'true') {
201
+					$type = true;
202
+				} else {
203
+					$type = self::typeFactory($this, $value, "Unparseable additional properties definition: '%s'");
204
+				}
205
+				$this->setAdditionalProperties($type);
206
+				return $this;
207
+			case 'discriminator':
208
+				$discriminator = self::wordShift($data);
209
+				$this->setDiscriminator($discriminator);
210
+				return $this;
211
+			case 'property':
212
+			case 'property?':
213
+			case 'property!':
214
+				$definition = self::wordShift($data);
215
+				if (empty($definition)) {
216
+					throw new Exception("Missing property definition");
217
+				}
218 218
 
219
-                $name = self::wordShift($data);
220
-                if (empty($name)) {
221
-                    throw new Exception("Missing property name: '{$definition}'");
222
-                }
219
+				$name = self::wordShift($data);
220
+				if (empty($name)) {
221
+					throw new Exception("Missing property name: '{$definition}'");
222
+				}
223 223
 
224
-                $readOnly = null;
225
-                $required = false;
226
-                $propertySuffix = substr($command, -1);
227
-                if ($propertySuffix === '!') {
228
-                    $readOnly = true;
229
-                } else if ($propertySuffix !== '?') {
230
-                    $required = true;
231
-                }
224
+				$readOnly = null;
225
+				$required = false;
226
+				$propertySuffix = substr($command, -1);
227
+				if ($propertySuffix === '!') {
228
+					$readOnly = true;
229
+				} else if ($propertySuffix !== '?') {
230
+					$required = true;
231
+				}
232 232
 
233
-                if (($name === $this->discriminator) && !$required) {
234
-                    throw new Exception("Discriminator must be a required property, "
235
-                        . "property '{$name}' is not required");
236
-                }
233
+				if (($name === $this->discriminator) && !$required) {
234
+					throw new Exception("Discriminator must be a required property, "
235
+						. "property '{$name}' is not required");
236
+				}
237 237
 
238
-                unset($this->required[$name]);
239
-                if ($required) {
240
-                    $this->required[$name] = true;
241
-                }
238
+				unset($this->required[$name]);
239
+				if ($required) {
240
+					$this->required[$name] = true;
241
+				}
242 242
 
243
-                $this->mostRecentProperty = new Property($this, $definition, $data, $readOnly);
244
-                $this->properties[$name] = $this->mostRecentProperty;
245
-                return $this;
243
+				$this->mostRecentProperty = new Property($this, $definition, $data, $readOnly);
244
+				$this->properties[$name] = $this->mostRecentProperty;
245
+				return $this;
246 246
 
247
-            case 'min':
248
-                $this->minProperties = intval($data);
249
-                if ($this->minProperties < 0) {
250
-                    throw new Exception("Minimum less than zero: '{$data}'");
251
-                }
252
-                if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) {
253
-                    throw new Exception("Minimum greater than maximum: '{$data}'");
254
-                }
255
-                $this->minProperties = intval($data);
256
-                return $this;
247
+			case 'min':
248
+				$this->minProperties = intval($data);
249
+				if ($this->minProperties < 0) {
250
+					throw new Exception("Minimum less than zero: '{$data}'");
251
+				}
252
+				if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) {
253
+					throw new Exception("Minimum greater than maximum: '{$data}'");
254
+				}
255
+				$this->minProperties = intval($data);
256
+				return $this;
257 257
 
258
-            case 'max':
259
-                $this->maxProperties = intval($data);
260
-                if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) {
261
-                    throw new Exception("Maximum less than minimum: '{$data}'");
262
-                }
263
-                if ($this->maxProperties < 0) {
264
-                    throw new Exception("Maximum less than zero: '{$data}'");
265
-                }
266
-                return $this;
267
-        }
258
+			case 'max':
259
+				$this->maxProperties = intval($data);
260
+				if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) {
261
+					throw new Exception("Maximum less than minimum: '{$data}'");
262
+				}
263
+				if ($this->maxProperties < 0) {
264
+					throw new Exception("Maximum less than zero: '{$data}'");
265
+				}
266
+				return $this;
267
+		}
268 268
 
269
-        // Pass through to most recent Property
270
-        if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) {
271
-            return $this;
272
-        }
269
+		// Pass through to most recent Property
270
+		if ($this->mostRecentProperty && $this->mostRecentProperty->handleCommand($command, $data)) {
271
+			return $this;
272
+		}
273 273
 
274
-        return parent::handleCommand($command, $data);
275
-    }
274
+		return parent::handleCommand($command, $data);
275
+	}
276 276
 
277
-    public function toArray()
278
-    {
279
-        return self::arrayFilterNull(array_merge(array(
280
-            'type' => 'object',
281
-            'required' => array_keys($this->required),
282
-            'properties' => self::objectsToArray($this->properties),
283
-            'minProperties' => $this->minProperties,
284
-            'maxProperties' => $this->maxProperties,
285
-            'discriminator' => $this->discriminator,
286
-            'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties,
287
-        ), parent::toArray()));
288
-    }
277
+	public function toArray()
278
+	{
279
+		return self::arrayFilterNull(array_merge(array(
280
+			'type' => 'object',
281
+			'required' => array_keys($this->required),
282
+			'properties' => self::objectsToArray($this->properties),
283
+			'minProperties' => $this->minProperties,
284
+			'maxProperties' => $this->maxProperties,
285
+			'discriminator' => $this->discriminator,
286
+			'additionalProperties' => $this->additionalProperties instanceof AbstractType ? $this->additionalProperties->toArray() : $this->additionalProperties,
287
+		), parent::toArray()));
288
+	}
289 289
 
290
-    public function __toString()
291
-    {
292
-        return __CLASS__;
293
-    }
290
+	public function __toString()
291
+	{
292
+		return __CLASS__;
293
+	}
294 294
 
295 295
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/BooleanType.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -15,60 +15,60 @@
 block discarded – undo
15 15
 class BooleanType extends AbstractType
16 16
 {
17 17
 
18
-    const REGEX_DEFAULT = '(?:=(true|false|1|0))?';
18
+	const REGEX_DEFAULT = '(?:=(true|false|1|0))?';
19 19
 
20
-    private $default = null;
20
+	private $default = null;
21 21
 
22
-    /**
23
-     * @throws Exception
24
-     */
25
-    protected function parseDefinition($definition)
26
-    {
27
-        $match = array();
28
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
29
-            throw new Exception("Unparseable boolean definition: '{$definition}'");
30
-        }
22
+	/**
23
+	 * @throws Exception
24
+	 */
25
+	protected function parseDefinition($definition)
26
+	{
27
+		$match = array();
28
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
29
+			throw new Exception("Unparseable boolean definition: '{$definition}'");
30
+		}
31 31
 
32
-        if (strtolower($match[1]) !== 'boolean') {
33
-            throw new Exception("Not a boolean: '{$definition}'");
34
-        }
32
+		if (strtolower($match[1]) !== 'boolean') {
33
+			throw new Exception("Not a boolean: '{$definition}'");
34
+		}
35 35
 
36
-        if (isset($match[2])) {
37
-            $this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true');
38
-        }
39
-    }
36
+		if (isset($match[2])) {
37
+			$this->default = ($match[2] === '1') || (strtolower($match[2]) === 'true');
38
+		}
39
+	}
40 40
 
41
-    /**
42
-     * @param string $command The comment command
43
-     * @param string $data Any data added after the command
44
-     * @return AbstractType|boolean
45
-     * @throws Exception
46
-     * @throws Exception
47
-     */
48
-    public function handleCommand($command, $data = null)
49
-    {
50
-        if (strtolower($command) === 'default') {
51
-            if (!in_array($data, array('0', '1', 'true', 'false'))) {
52
-                throw new Exception("Invalid boolean default: '{$data}'");
53
-            }
54
-            $this->default = ($data == '1') || (strtolower($data) === 'true');
55
-            return $this;
56
-        }
41
+	/**
42
+	 * @param string $command The comment command
43
+	 * @param string $data Any data added after the command
44
+	 * @return AbstractType|boolean
45
+	 * @throws Exception
46
+	 * @throws Exception
47
+	 */
48
+	public function handleCommand($command, $data = null)
49
+	{
50
+		if (strtolower($command) === 'default') {
51
+			if (!in_array($data, array('0', '1', 'true', 'false'))) {
52
+				throw new Exception("Invalid boolean default: '{$data}'");
53
+			}
54
+			$this->default = ($data == '1') || (strtolower($data) === 'true');
55
+			return $this;
56
+		}
57 57
 
58
-        return parent::handleCommand($command, $data);
59
-    }
58
+		return parent::handleCommand($command, $data);
59
+	}
60 60
 
61
-    public function toArray()
62
-    {
63
-        return self::arrayFilterNull(array_merge(array(
64
-            'type' => 'boolean',
65
-            'default' => $this->default,
66
-        ), parent::toArray()));
67
-    }
61
+	public function toArray()
62
+	{
63
+		return self::arrayFilterNull(array_merge(array(
64
+			'type' => 'boolean',
65
+			'default' => $this->default,
66
+		), parent::toArray()));
67
+	}
68 68
 
69
-    public function __toString()
70
-    {
71
-        return __CLASS__;
72
-    }
69
+	public function __toString()
70
+	{
71
+		return __CLASS__;
72
+	}
73 73
 
74 74
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/Property.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -16,82 +16,82 @@
 block discarded – undo
16 16
 class Property extends AbstractObject
17 17
 {
18 18
 
19
-    /**
20
-     * Description of this property
21
-     * @var string
22
-     */
23
-    private $description;
19
+	/**
20
+	 * Description of this property
21
+	 * @var string
22
+	 */
23
+	private $description;
24 24
 
25
-    /**
26
-     * Whether property is read only
27
-     * @var bool
28
-     */
29
-    private $readOnly;
25
+	/**
26
+	 * Whether property is read only
27
+	 * @var bool
28
+	 */
29
+	private $readOnly;
30 30
 
31
-    /**
32
-     * Type definition of this property
33
-     * @var AbstractType
34
-     */
35
-    private $Type;
31
+	/**
32
+	 * Type definition of this property
33
+	 * @var AbstractType
34
+	 */
35
+	private $Type;
36 36
 
37
-    /**
38
-     * Create a new property
39
-     * @param AbstractObject $parent
40
-     * @param string $definition Either a built-in type or a definition name
41
-     * @param string $description description of the property
42
-     * @param bool $readOnly Whether the property is read only
43
-     * @throws Exception
44
-     */
45
-    public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null)
46
-    {
47
-        parent::__construct($parent);
48
-        $this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'");
49
-        $this->description = $description;
50
-        $this->readOnly = $readOnly;
51
-    }
37
+	/**
38
+	 * Create a new property
39
+	 * @param AbstractObject $parent
40
+	 * @param string $definition Either a built-in type or a definition name
41
+	 * @param string $description description of the property
42
+	 * @param bool $readOnly Whether the property is read only
43
+	 * @throws Exception
44
+	 */
45
+	public function __construct(AbstractObject $parent, $definition, $description = null, $readOnly = null)
46
+	{
47
+		parent::__construct($parent);
48
+		$this->Type = AbstractType::typeFactory($this, $definition, "Not a property: '%s'");
49
+		$this->description = $description;
50
+		$this->readOnly = $readOnly;
51
+	}
52 52
 
53
-    /**
54
-     * @param string $command The comment command
55
-     * @param string $data Any data added after the command
56
-     * @return self|boolean
57
-     * @throws Exception
58
-     */
59
-    public function handleCommand($command, $data = null)
60
-    {
61
-        // Pass through to Type
62
-        if ($this->Type && $this->Type->handleCommand($command, $data)) {
63
-            return $this;
64
-        }
53
+	/**
54
+	 * @param string $command The comment command
55
+	 * @param string $data Any data added after the command
56
+	 * @return self|boolean
57
+	 * @throws Exception
58
+	 */
59
+	public function handleCommand($command, $data = null)
60
+	{
61
+		// Pass through to Type
62
+		if ($this->Type && $this->Type->handleCommand($command, $data)) {
63
+			return $this;
64
+		}
65 65
 
66
-        return parent::handleCommand($command, $data);
67
-    }
66
+		return parent::handleCommand($command, $data);
67
+	}
68 68
 
69
-    public function toArray()
70
-    {
71
-        // Reference + readonly/description result in allOf construct
72
-        // as it's semantically the same and that's what swagger tools
73
-        // like swagger-ui can understand
74
-        $requiresWrap =
75
-            $this->Type instanceof ReferenceObjectType
76
-            && (!empty($this->description) || !is_null($this->readOnly));
69
+	public function toArray()
70
+	{
71
+		// Reference + readonly/description result in allOf construct
72
+		// as it's semantically the same and that's what swagger tools
73
+		// like swagger-ui can understand
74
+		$requiresWrap =
75
+			$this->Type instanceof ReferenceObjectType
76
+			&& (!empty($this->description) || !is_null($this->readOnly));
77 77
 
78
-        $valueType = $this->Type->toArray();
78
+		$valueType = $this->Type->toArray();
79 79
 
80
-        if ($requiresWrap) {
81
-            $valueType = array(
82
-                'allOf' => array($valueType),
83
-            );
84
-        }
80
+		if ($requiresWrap) {
81
+			$valueType = array(
82
+				'allOf' => array($valueType),
83
+			);
84
+		}
85 85
 
86
-        return self::arrayFilterNull(array_merge($valueType, array(
87
-            'description' => empty($this->description) ? null : $this->description,
88
-            'readOnly' => $this->readOnly
89
-        ), parent::toArray()));
90
-    }
86
+		return self::arrayFilterNull(array_merge($valueType, array(
87
+			'description' => empty($this->description) ? null : $this->description,
88
+			'readOnly' => $this->readOnly
89
+		), parent::toArray()));
90
+	}
91 91
 
92
-    public function __toString()
93
-    {
94
-        return __CLASS__;
95
-    }
92
+	public function __toString()
93
+	{
94
+		return __CLASS__;
95
+	}
96 96
 
97 97
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/NumberType.php 1 patch
Indentation   +149 added lines, -149 removed lines patch added patch discarded remove patch
@@ -15,154 +15,154 @@
 block discarded – undo
15 15
 class NumberType extends AbstractType
16 16
 {
17 17
 
18
-    const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?';
19
-    const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?';
20
-
21
-    private static $formats = array(
22
-        'float' => 'float',
23
-        'double' => 'double',
24
-    );
25
-    private $format;
26
-    private $default = null;
27
-    private $maximum = null;
28
-    private $exclusiveMaximum = null;
29
-    private $minimum = null;
30
-    private $exclusiveMinimum = null;
31
-    private $enum = array();
32
-    private $multipleOf = null;
33
-
34
-    /**
35
-     * @throws Exception
36
-     */
37
-    protected function parseDefinition($definition)
38
-    {
39
-        $match = array();
40
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
41
-            throw new Exception("Unparseable number definition: '{$definition}'");
42
-        }
43
-
44
-        $this->parseFormat($definition, $match);
45
-        $this->parseRange($definition, $match);
46
-        $this->parseDefault($definition, $match);
47
-    }
48
-
49
-    /**
50
-     * @param string[] $match
51
-     * @throws Exception
52
-     */
53
-    private function parseFormat($definition, $match)
54
-    {
55
-        if (!isset(self::$formats[strtolower($match[1])])) {
56
-            throw new Exception("Not a number: '{$definition}'");
57
-        }
58
-        $this->format = self::$formats[strtolower($match[1])];
59
-    }
60
-
61
-    /**
62
-     * @param string[] $match
63
-     * @throws Exception
64
-     */
65
-    private function parseRange($definition, $match)
66
-    {
67
-        if (!empty($match[2])) {
68
-            if ($match[3] === '' && $match[4] === '') {
69
-                throw new Exception("Empty number range: '{$definition}'");
70
-            }
71
-
72
-            $this->exclusiveMinimum = $match[2] == '<';
73
-            $this->minimum = $match[3] === '' ? null : doubleval($match[3]);
74
-            $this->maximum = $match[4] === '' ? null : doubleval($match[4]);
75
-            $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
76
-            if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
77
-                self::swap($this->minimum, $this->maximum);
78
-                self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
79
-            }
80
-        }
81
-    }
82
-
83
-    /**
84
-     * @param string[] $match
85
-     * @throws Exception
86
-     */
87
-    private function parseDefault($definition, $match)
88
-    {
89
-        $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
90
-    }
91
-
92
-    /**
93
-     * @param string $command The comment command
94
-     * @param string $data Any data added after the command
95
-     * @return AbstractType|boolean
96
-     * @throws Exception
97
-     * @throws Exception
98
-     * @throws Exception
99
-     */
100
-    public function handleCommand($command, $data = null)
101
-    {
102
-        switch (strtolower($command)) {
103
-            case 'default':
104
-                $this->default = $this->validateDefault($data);
105
-                return $this;
106
-
107
-            case 'enum':
108
-                $words = self::wordSplit($data);
109
-                foreach ($words as &$word) {
110
-                    $word = $this->validateDefault($word);
111
-                }
112
-                $this->enum = array_merge($this->enum, $words);
113
-                return $this;
114
-
115
-            case 'step':
116
-                if (($step = doubleval($data)) > 0) {
117
-                    $this->multipleOf = $step;
118
-                }
119
-                return $this;
120
-        }
121
-
122
-        return parent::handleCommand($command, $data);
123
-    }
124
-
125
-    public function toArray()
126
-    {
127
-        return self::arrayFilterNull(array_merge(array(
128
-            'type' => 'number',
129
-            'format' => $this->format,
130
-            'default' => $this->default,
131
-            'minimum' => $this->minimum,
132
-            'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
133
-            'maximum' => $this->maximum,
134
-            'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
135
-            'enum' => $this->enum,
136
-            'multipleOf' => $this->multipleOf,
137
-        ), parent::toArray()));
138
-    }
139
-
140
-    public function __toString()
141
-    {
142
-        return __CLASS__;
143
-    }
144
-
145
-    /**
146
-     * @throws Exception
147
-     */
148
-    private function validateDefault($value)
149
-    {
150
-        if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) {
151
-            throw new Exception("Invalid number default: '{$value}'");
152
-        }
153
-
154
-        if ($this->maximum) {
155
-            if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
156
-                throw new Exception("Default number beyond maximum: '{$value}'");
157
-            }
158
-        }
159
-        if ($this->minimum) {
160
-            if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
161
-                throw new Exception("Default number beyond minimum: '{$value}'");
162
-            }
163
-        }
164
-
165
-        return doubleval($value);
166
-    }
18
+	const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?';
19
+	const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?';
20
+
21
+	private static $formats = array(
22
+		'float' => 'float',
23
+		'double' => 'double',
24
+	);
25
+	private $format;
26
+	private $default = null;
27
+	private $maximum = null;
28
+	private $exclusiveMaximum = null;
29
+	private $minimum = null;
30
+	private $exclusiveMinimum = null;
31
+	private $enum = array();
32
+	private $multipleOf = null;
33
+
34
+	/**
35
+	 * @throws Exception
36
+	 */
37
+	protected function parseDefinition($definition)
38
+	{
39
+		$match = array();
40
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
41
+			throw new Exception("Unparseable number definition: '{$definition}'");
42
+		}
43
+
44
+		$this->parseFormat($definition, $match);
45
+		$this->parseRange($definition, $match);
46
+		$this->parseDefault($definition, $match);
47
+	}
48
+
49
+	/**
50
+	 * @param string[] $match
51
+	 * @throws Exception
52
+	 */
53
+	private function parseFormat($definition, $match)
54
+	{
55
+		if (!isset(self::$formats[strtolower($match[1])])) {
56
+			throw new Exception("Not a number: '{$definition}'");
57
+		}
58
+		$this->format = self::$formats[strtolower($match[1])];
59
+	}
60
+
61
+	/**
62
+	 * @param string[] $match
63
+	 * @throws Exception
64
+	 */
65
+	private function parseRange($definition, $match)
66
+	{
67
+		if (!empty($match[2])) {
68
+			if ($match[3] === '' && $match[4] === '') {
69
+				throw new Exception("Empty number range: '{$definition}'");
70
+			}
71
+
72
+			$this->exclusiveMinimum = $match[2] == '<';
73
+			$this->minimum = $match[3] === '' ? null : doubleval($match[3]);
74
+			$this->maximum = $match[4] === '' ? null : doubleval($match[4]);
75
+			$this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
76
+			if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
77
+				self::swap($this->minimum, $this->maximum);
78
+				self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
79
+			}
80
+		}
81
+	}
82
+
83
+	/**
84
+	 * @param string[] $match
85
+	 * @throws Exception
86
+	 */
87
+	private function parseDefault($definition, $match)
88
+	{
89
+		$this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
90
+	}
91
+
92
+	/**
93
+	 * @param string $command The comment command
94
+	 * @param string $data Any data added after the command
95
+	 * @return AbstractType|boolean
96
+	 * @throws Exception
97
+	 * @throws Exception
98
+	 * @throws Exception
99
+	 */
100
+	public function handleCommand($command, $data = null)
101
+	{
102
+		switch (strtolower($command)) {
103
+			case 'default':
104
+				$this->default = $this->validateDefault($data);
105
+				return $this;
106
+
107
+			case 'enum':
108
+				$words = self::wordSplit($data);
109
+				foreach ($words as &$word) {
110
+					$word = $this->validateDefault($word);
111
+				}
112
+				$this->enum = array_merge($this->enum, $words);
113
+				return $this;
114
+
115
+			case 'step':
116
+				if (($step = doubleval($data)) > 0) {
117
+					$this->multipleOf = $step;
118
+				}
119
+				return $this;
120
+		}
121
+
122
+		return parent::handleCommand($command, $data);
123
+	}
124
+
125
+	public function toArray()
126
+	{
127
+		return self::arrayFilterNull(array_merge(array(
128
+			'type' => 'number',
129
+			'format' => $this->format,
130
+			'default' => $this->default,
131
+			'minimum' => $this->minimum,
132
+			'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
133
+			'maximum' => $this->maximum,
134
+			'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
135
+			'enum' => $this->enum,
136
+			'multipleOf' => $this->multipleOf,
137
+		), parent::toArray()));
138
+	}
139
+
140
+	public function __toString()
141
+	{
142
+		return __CLASS__;
143
+	}
144
+
145
+	/**
146
+	 * @throws Exception
147
+	 */
148
+	private function validateDefault($value)
149
+	{
150
+		if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) {
151
+			throw new Exception("Invalid number default: '{$value}'");
152
+		}
153
+
154
+		if ($this->maximum) {
155
+			if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
156
+				throw new Exception("Default number beyond maximum: '{$value}'");
157
+			}
158
+		}
159
+		if ($this->minimum) {
160
+			if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
161
+				throw new Exception("Default number beyond minimum: '{$value}'");
162
+			}
163
+		}
164
+
165
+		return doubleval($value);
166
+	}
167 167
 
168 168
 }
Please login to merge, or discard this patch.