Completed
Pull Request — master (#51)
by
unknown
20s
created
SwaggerGen/Swagger/Type/ReferenceObjectType.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -15,48 +15,48 @@
 block discarded – undo
15 15
 class ReferenceObjectType extends AbstractType
16 16
 {
17 17
 
18
-    private $reference = null;
19
-
20
-    /**
21
-     * @throws Exception
22
-     */
23
-    protected function parseDefinition($definition)
24
-    {
25
-        $definition = self::trim($definition);
26
-
27
-        $match = array();
28
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
29
-            throw new Exception('Unparseable string definition: \'' . $definition . '\'');
30
-        }
31
-
32
-        $type = strtolower($match[1]);
33
-
34
-        $reference = null;
35
-        if ($type === 'refobject') {
36
-            if (isset($match[2])) {
37
-                $reference = $match[2];
38
-            }
39
-        } else {
40
-            $reference = $match[1];
41
-        }
42
-
43
-        if (empty($reference)) {
44
-            throw new Exception('Referenced object name missing: \'' . $definition . '\'');
45
-        }
46
-
47
-        $this->reference = $reference;
48
-    }
49
-
50
-    public function toArray()
51
-    {
52
-        return self::arrayFilterNull(array_merge(array(
53
-            '$ref' => '#/definitions/' . $this->reference,
54
-        ), parent::toArray()));
55
-    }
56
-
57
-    public function __toString()
58
-    {
59
-        return __CLASS__ . ' ' . $this->reference;
60
-    }
18
+	private $reference = null;
19
+
20
+	/**
21
+	 * @throws Exception
22
+	 */
23
+	protected function parseDefinition($definition)
24
+	{
25
+		$definition = self::trim($definition);
26
+
27
+		$match = array();
28
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
29
+			throw new Exception('Unparseable string definition: \'' . $definition . '\'');
30
+		}
31
+
32
+		$type = strtolower($match[1]);
33
+
34
+		$reference = null;
35
+		if ($type === 'refobject') {
36
+			if (isset($match[2])) {
37
+				$reference = $match[2];
38
+			}
39
+		} else {
40
+			$reference = $match[1];
41
+		}
42
+
43
+		if (empty($reference)) {
44
+			throw new Exception('Referenced object name missing: \'' . $definition . '\'');
45
+		}
46
+
47
+		$this->reference = $reference;
48
+	}
49
+
50
+	public function toArray()
51
+	{
52
+		return self::arrayFilterNull(array_merge(array(
53
+			'$ref' => '#/definitions/' . $this->reference,
54
+		), parent::toArray()));
55
+	}
56
+
57
+	public function __toString()
58
+	{
59
+		return __CLASS__ . ' ' . $this->reference;
60
+	}
61 61
 
62 62
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/AllOfType.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -14,54 +14,54 @@
 block discarded – undo
14 14
  */
15 15
 class AllOfType extends AbstractType
16 16
 {
17
-    private $allOfItems = array();
18
-    private $mostRecentItem;
17
+	private $allOfItems = array();
18
+	private $mostRecentItem;
19 19
 
20
-    /**
21
-     * @throws Exception
22
-     */
23
-    protected function parseDefinition($definition)
24
-    {
25
-        $pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END;
26
-        $inlineDef = '';
27
-        if (preg_match($pattern, $definition, $matches)) {
28
-            if (isset($matches[1])) {
29
-                $inlineDef = $matches[1];
30
-            }
31
-        }
32
-        if ($inlineDef) {
33
-            foreach ($this->parseList($inlineDef) as $item) {
34
-                $this->handleCommand('item', $item);
35
-            }
36
-        }
37
-    }
20
+	/**
21
+	 * @throws Exception
22
+	 */
23
+	protected function parseDefinition($definition)
24
+	{
25
+		$pattern = self::REGEX_START . 'allof' . self::REGEX_CONTENT . self::REGEX_END;
26
+		$inlineDef = '';
27
+		if (preg_match($pattern, $definition, $matches)) {
28
+			if (isset($matches[1])) {
29
+				$inlineDef = $matches[1];
30
+			}
31
+		}
32
+		if ($inlineDef) {
33
+			foreach ($this->parseList($inlineDef) as $item) {
34
+				$this->handleCommand('item', $item);
35
+			}
36
+		}
37
+	}
38 38
 
39
-    /**
40
-     * @throws Exception
41
-     */
42
-    public function handleCommand($command, $data = null)
43
-    {
44
-        if (strtolower($command) === 'item') {
45
-            $this->mostRecentItem = self::typeFactory($this, $data);
46
-            $this->allOfItems[] = $this->mostRecentItem;
47
-            return $this;
48
-        }
49
-        if (isset($this->mostRecentItem)) {
50
-            if ($this->mostRecentItem->handleCommand($command, $data)) {
51
-                return $this;
52
-            }
53
-        }
54
-        return parent::handleCommand($command, $data);
55
-    }
39
+	/**
40
+	 * @throws Exception
41
+	 */
42
+	public function handleCommand($command, $data = null)
43
+	{
44
+		if (strtolower($command) === 'item') {
45
+			$this->mostRecentItem = self::typeFactory($this, $data);
46
+			$this->allOfItems[] = $this->mostRecentItem;
47
+			return $this;
48
+		}
49
+		if (isset($this->mostRecentItem)) {
50
+			if ($this->mostRecentItem->handleCommand($command, $data)) {
51
+				return $this;
52
+			}
53
+		}
54
+		return parent::handleCommand($command, $data);
55
+	}
56 56
 
57
-    public function toArray()
58
-    {
59
-        $allOf = array();
60
-        foreach ($this->allOfItems as $item) {
61
-            $allOf[] = $item->toArray();
62
-        }
63
-        return self::arrayFilterNull(array_merge(array(
64
-            'allOf' => $allOf,
65
-        ), parent::toArray()));
66
-    }
57
+	public function toArray()
58
+	{
59
+		$allOf = array();
60
+		foreach ($this->allOfItems as $item) {
61
+			$allOf[] = $item->toArray();
62
+		}
63
+		return self::arrayFilterNull(array_merge(array(
64
+			'allOf' => $allOf,
65
+		), parent::toArray()));
66
+	}
67 67
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/FileType.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -17,45 +17,45 @@
 block discarded – undo
17 17
 class FileType extends AbstractType
18 18
 {
19 19
 
20
-    /**
21
-     * @throws Exception
22
-     */
23
-    protected function parseDefinition($definition)
24
-    {
25
-        $type = strtolower($definition);
26
-
27
-        if ($type !== 'file') {
28
-            throw new Exception("Not a file: '{$definition}'");
29
-        }
30
-
31
-        $parent = $this->getParent();
32
-        if (!($parent instanceof Parameter) || !$parent->isForm()) {
33
-            throw new Exception("File type '{$definition}' only allowed on form parameter");
34
-        }
35
-
36
-        /** @var Operation $parentOperation */
37
-        $parentOperation = $this->getParentClass(Operation::class);
38
-        $consumes = $parentOperation->getConsumes();
39
-        if (empty($consumes)) {
40
-            $consumes = $this->getSwagger()->getConsumes();
41
-        }
42
-
43
-        $valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes));
44
-        if (empty($consumes) || $valid_consumes !== count($consumes)) {
45
-            throw new Exception("File type '{$definition}' without valid consume");
46
-        }
47
-    }
48
-
49
-    public function toArray()
50
-    {
51
-        return self::arrayFilterNull(array_merge(array(
52
-            'type' => 'file',
53
-        ), parent::toArray()));
54
-    }
55
-
56
-    public function __toString()
57
-    {
58
-        return __CLASS__;
59
-    }
20
+	/**
21
+	 * @throws Exception
22
+	 */
23
+	protected function parseDefinition($definition)
24
+	{
25
+		$type = strtolower($definition);
26
+
27
+		if ($type !== 'file') {
28
+			throw new Exception("Not a file: '{$definition}'");
29
+		}
30
+
31
+		$parent = $this->getParent();
32
+		if (!($parent instanceof Parameter) || !$parent->isForm()) {
33
+			throw new Exception("File type '{$definition}' only allowed on form parameter");
34
+		}
35
+
36
+		/** @var Operation $parentOperation */
37
+		$parentOperation = $this->getParentClass(Operation::class);
38
+		$consumes = $parentOperation->getConsumes();
39
+		if (empty($consumes)) {
40
+			$consumes = $this->getSwagger()->getConsumes();
41
+		}
42
+
43
+		$valid_consumes = ((int)in_array('multipart/form-data', $consumes)) + ((int)in_array('application/x-www-form-urlencoded', $consumes));
44
+		if (empty($consumes) || $valid_consumes !== count($consumes)) {
45
+			throw new Exception("File type '{$definition}' without valid consume");
46
+		}
47
+	}
48
+
49
+	public function toArray()
50
+	{
51
+		return self::arrayFilterNull(array_merge(array(
52
+			'type' => 'file',
53
+		), parent::toArray()));
54
+	}
55
+
56
+	public function __toString()
57
+	{
58
+		return __CLASS__;
59
+	}
60 60
 
61 61
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/DateType.php 1 patch
Indentation   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -16,97 +16,97 @@
 block discarded – undo
16 16
 class DateType extends AbstractType
17 17
 {
18 18
 
19
-    const REGEX_DEFAULT = '(?:=(\S+))?';
20
-
21
-    /**
22
-     * Map of recognized format names to Swagger formats
23
-     * @var array
24
-     */
25
-    private static $formats = array(
26
-        'date' => 'date',
27
-        'date-time' => 'date-time',
28
-        'datetime' => 'date-time',
29
-    );
30
-    private static $datetime_formats = array(
31
-        'date' => 'Y-m-d',
32
-        'date-time' => DateTime::RFC3339,
33
-    );
34
-
35
-    /**
36
-     * @var String
37
-     */
38
-    private $format;
39
-
40
-    /**
41
-     * @var DateTime
42
-     */
43
-    private $default = null;
44
-
45
-    /**
46
-     * @throws Exception
47
-     */
48
-    protected function parseDefinition($definition)
49
-    {
50
-        $match = array();
51
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
52
-            throw new Exception("Unparseable date definition: '{$definition}'");
53
-        }
54
-
55
-        $type = strtolower($match[1]);
56
-
57
-        if (!isset(self::$formats[$type])) {
58
-            throw new Exception("Not a date: '{$definition}'");
59
-        }
60
-        $this->format = self::$formats[$type];
61
-
62
-        $this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
63
-    }
64
-
65
-    /**
66
-     * @param string $command The comment command
67
-     * @param string $data Any data added after the command
68
-     * @return AbstractType|boolean
69
-     * @throws Exception
70
-     * @throws Exception
71
-     */
72
-    public function handleCommand($command, $data = null)
73
-    {
74
-        if (strtolower($command) === 'default') {
75
-            $this->default = $this->validateDefault($data);
76
-            return $this;
77
-        }
78
-
79
-        return parent::handleCommand($command, $data);
80
-    }
81
-
82
-    public function toArray()
83
-    {
84
-        return self::arrayFilterNull(array_merge(array(
85
-            'type' => 'string',
86
-            'format' => $this->format,
87
-            'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null,
88
-        ), parent::toArray()));
89
-    }
90
-
91
-    /**
92
-     * @throws Exception
93
-     */
94
-    private function validateDefault($value)
95
-    {
96
-        if (empty($value)) {
97
-            throw new Exception("Empty date default");
98
-        }
99
-
100
-        if (($DateTime = date_create($value)) !== false) {
101
-            return $DateTime;
102
-        }
103
-
104
-        throw new Exception("Invalid '{$this->format}' default: '{$value}'");
105
-    }
106
-
107
-    public function __toString()
108
-    {
109
-        return __CLASS__;
110
-    }
19
+	const REGEX_DEFAULT = '(?:=(\S+))?';
20
+
21
+	/**
22
+	 * Map of recognized format names to Swagger formats
23
+	 * @var array
24
+	 */
25
+	private static $formats = array(
26
+		'date' => 'date',
27
+		'date-time' => 'date-time',
28
+		'datetime' => 'date-time',
29
+	);
30
+	private static $datetime_formats = array(
31
+		'date' => 'Y-m-d',
32
+		'date-time' => DateTime::RFC3339,
33
+	);
34
+
35
+	/**
36
+	 * @var String
37
+	 */
38
+	private $format;
39
+
40
+	/**
41
+	 * @var DateTime
42
+	 */
43
+	private $default = null;
44
+
45
+	/**
46
+	 * @throws Exception
47
+	 */
48
+	protected function parseDefinition($definition)
49
+	{
50
+		$match = array();
51
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
52
+			throw new Exception("Unparseable date definition: '{$definition}'");
53
+		}
54
+
55
+		$type = strtolower($match[1]);
56
+
57
+		if (!isset(self::$formats[$type])) {
58
+			throw new Exception("Not a date: '{$definition}'");
59
+		}
60
+		$this->format = self::$formats[$type];
61
+
62
+		$this->default = isset($match[2]) && $match[2] !== '' ? $this->validateDefault($match[2]) : null;
63
+	}
64
+
65
+	/**
66
+	 * @param string $command The comment command
67
+	 * @param string $data Any data added after the command
68
+	 * @return AbstractType|boolean
69
+	 * @throws Exception
70
+	 * @throws Exception
71
+	 */
72
+	public function handleCommand($command, $data = null)
73
+	{
74
+		if (strtolower($command) === 'default') {
75
+			$this->default = $this->validateDefault($data);
76
+			return $this;
77
+		}
78
+
79
+		return parent::handleCommand($command, $data);
80
+	}
81
+
82
+	public function toArray()
83
+	{
84
+		return self::arrayFilterNull(array_merge(array(
85
+			'type' => 'string',
86
+			'format' => $this->format,
87
+			'default' => $this->default ? $this->default->format(self::$datetime_formats[$this->format]) : null,
88
+		), parent::toArray()));
89
+	}
90
+
91
+	/**
92
+	 * @throws Exception
93
+	 */
94
+	private function validateDefault($value)
95
+	{
96
+		if (empty($value)) {
97
+			throw new Exception("Empty date default");
98
+		}
99
+
100
+		if (($DateTime = date_create($value)) !== false) {
101
+			return $DateTime;
102
+		}
103
+
104
+		throw new Exception("Invalid '{$this->format}' default: '{$value}'");
105
+	}
106
+
107
+	public function __toString()
108
+	{
109
+		return __CLASS__;
110
+	}
111 111
 
112 112
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/StringUuidType.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -15,15 +15,15 @@
 block discarded – undo
15 15
 class StringUuidType extends AbstractRegexType
16 16
 {
17 17
 
18
-    /**
19
-     * Construct and set up the regular expression for this type
20
-     *
21
-     * @param AbstractObject $parent
22
-     * @param string $definition
23
-     */
24
-    public function __construct(AbstractObject $parent, $definition)
25
-    {
26
-        parent::__construct($parent, $definition, 'uuid', '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}');
27
-    }
18
+	/**
19
+	 * Construct and set up the regular expression for this type
20
+	 *
21
+	 * @param AbstractObject $parent
22
+	 * @param string $definition
23
+	 */
24
+	public function __construct(AbstractObject $parent, $definition)
25
+	{
26
+		parent::__construct($parent, $definition, 'uuid', '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}');
27
+	}
28 28
 
29 29
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/StringType.php 1 patch
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -15,187 +15,187 @@
 block discarded – undo
15 15
 class StringType extends AbstractType
16 16
 {
17 17
 
18
-    private static $formats = array(
19
-        'string' => '',
20
-        'byte' => 'byte',
21
-        'binary' => 'binary',
22
-        'password' => 'password',
23
-        'enum' => '',
24
-    );
25
-
26
-    /**
27
-     * Name of the type
28
-     * @var string
29
-     */
30
-    protected $format = '';
31
-    protected $pattern = null;
32
-    protected $default = null;
33
-    protected $maxLength = null;
34
-    protected $minLength = null;
35
-    protected $enum = array();
36
-
37
-    /**
38
-     * @throws Exception
39
-     */
40
-    protected function parseDefinition($definition)
41
-    {
42
-        $definition = self::trim($definition);
43
-
44
-        $match = array();
45
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
46
-            throw new Exception("Unparseable string definition: '{$definition}'");
47
-        }
48
-
49
-        $this->parseFormat($definition, $match);
50
-        $this->parseContent($definition, $match);
51
-        $this->parseRange($definition, $match);
52
-        $this->parseDefault($definition, $match);
53
-    }
54
-
55
-    /**
56
-     * @param string $definition
57
-     * @param string[] $match
58
-     * @throws Exception
59
-     */
60
-    private function parseFormat($definition, $match)
61
-    {
62
-        $type = strtolower($match[1]);
63
-        if (!isset(self::$formats[$type])) {
64
-            throw new Exception("Not a string: '{$definition}'");
65
-        }
66
-        $this->format = self::$formats[$type];
67
-    }
68
-
69
-    /**
70
-     * @param string $definition
71
-     * @param string[] $match
72
-     */
73
-    private function parseContent($definition, $match)
74
-    {
75
-        if (strtolower($match[1]) === 'enum') {
76
-            $this->enum = explode(',', $match[2]);
77
-        } else {
78
-            $this->pattern = empty($match[2]) ? null : $match[2];
79
-        }
80
-    }
81
-
82
-    /**
83
-     * @param string $definition
84
-     * @param string[] $match
85
-     * @throws Exception
86
-     * @throws Exception
87
-     */
88
-    private function parseRange($definition, $match)
89
-    {
90
-
91
-        if (!empty($match[3])) {
92
-            if ($match[1] === 'enum') {
93
-                throw new Exception("Range not allowed in enumeration definition: '{$definition}'");
94
-            }
95
-            if ($match[4] === '' && $match[5] === '') {
96
-                throw new Exception("Empty string range: '{$definition}'");
97
-            }
98
-            $exclusiveMinimum = $match[3] == '<';
99
-            $this->minLength = $match[4] === '' ? null : $match[4];
100
-            $this->maxLength = $match[5] === '' ? null : $match[5];
101
-            $exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
102
-            if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) {
103
-                self::swap($this->minLength, $this->maxLength);
104
-                self::swap($exclusiveMinimum, $exclusiveMaximum);
105
-            }
106
-            $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength);
107
-            $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength);
108
-        }
109
-    }
110
-
111
-    /**
112
-     * @param string $definition
113
-     * @param string[] $match
114
-     * @throws Exception
115
-     */
116
-    private function parseDefault($definition, $match)
117
-    {
118
-        $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null;
119
-    }
120
-
121
-    /**
122
-     * @param string $command The comment command
123
-     * @param string $data Any data added after the command
124
-     * @return AbstractType|boolean
125
-     * @throws Exception
126
-     * @throws Exception
127
-     * @throws Exception
128
-     */
129
-    public function handleCommand($command, $data = null)
130
-    {
131
-        switch (strtolower($command)) {
132
-            case 'default':
133
-                $this->default = $this->validateDefault($data);
134
-                return $this;
135
-
136
-            case 'pattern':
137
-                $this->pattern = $data;
138
-                return $this;
139
-
140
-            case 'enum':
141
-                if ($this->minLength !== null || $this->maxLength !== null) {
142
-                    throw new Exception("Enumeration not allowed in ranged string: '{$data}'");
143
-                }
144
-                $words = self::wordSplit($data);
145
-                $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words;
146
-                return $this;
147
-        }
148
-
149
-        return parent::handleCommand($command, $data);
150
-    }
151
-
152
-    public function toArray()
153
-    {
154
-        return self::arrayFilterNull(array_merge(array(
155
-            'type' => 'string',
156
-            'format' => empty($this->format) ? null : $this->format,
157
-            'pattern' => $this->pattern,
158
-            'default' => $this->default,
159
-            'minLength' => $this->minLength ? intval($this->minLength) : null,
160
-            'maxLength' => $this->maxLength ? intval($this->maxLength) : null,
161
-            'enum' => $this->enum,
162
-        ), parent::toArray()));
163
-    }
164
-
165
-    /**
166
-     * Validate a default string value, depending on subtype
167
-     *
168
-     * @param string $value the value to validate
169
-     * @return string the value after validation (might become trimmed)
170
-     * @throws Exception
171
-     */
172
-    protected function validateDefault($value)
173
-    {
174
-        if (empty($value)) {
175
-            $type = $this->format ?: ($this->enum ? 'enum' : 'string');
176
-            throw new Exception("Empty {$type} default");
177
-        }
178
-
179
-        if (!empty($this->enum) && !in_array($value, $this->enum)) {
180
-            throw new Exception("Invalid enum default: '{$value}'");
181
-        }
182
-
183
-        if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) {
184
-            $type = $this->format ?: ($this->enum ? 'enum' : 'string');
185
-            throw new Exception("Default {$type} length beyond maximum: '{$value}'");
186
-        }
187
-
188
-        if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
189
-            $type = $this->format ?: ($this->enum ? 'enum' : 'string');
190
-            throw new Exception("Default {$type} length beyond minimum: '{$value}'");
191
-        }
192
-
193
-        return $value;
194
-    }
195
-
196
-    public function __toString()
197
-    {
198
-        return __CLASS__;
199
-    }
18
+	private static $formats = array(
19
+		'string' => '',
20
+		'byte' => 'byte',
21
+		'binary' => 'binary',
22
+		'password' => 'password',
23
+		'enum' => '',
24
+	);
25
+
26
+	/**
27
+	 * Name of the type
28
+	 * @var string
29
+	 */
30
+	protected $format = '';
31
+	protected $pattern = null;
32
+	protected $default = null;
33
+	protected $maxLength = null;
34
+	protected $minLength = null;
35
+	protected $enum = array();
36
+
37
+	/**
38
+	 * @throws Exception
39
+	 */
40
+	protected function parseDefinition($definition)
41
+	{
42
+		$definition = self::trim($definition);
43
+
44
+		$match = array();
45
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
46
+			throw new Exception("Unparseable string definition: '{$definition}'");
47
+		}
48
+
49
+		$this->parseFormat($definition, $match);
50
+		$this->parseContent($definition, $match);
51
+		$this->parseRange($definition, $match);
52
+		$this->parseDefault($definition, $match);
53
+	}
54
+
55
+	/**
56
+	 * @param string $definition
57
+	 * @param string[] $match
58
+	 * @throws Exception
59
+	 */
60
+	private function parseFormat($definition, $match)
61
+	{
62
+		$type = strtolower($match[1]);
63
+		if (!isset(self::$formats[$type])) {
64
+			throw new Exception("Not a string: '{$definition}'");
65
+		}
66
+		$this->format = self::$formats[$type];
67
+	}
68
+
69
+	/**
70
+	 * @param string $definition
71
+	 * @param string[] $match
72
+	 */
73
+	private function parseContent($definition, $match)
74
+	{
75
+		if (strtolower($match[1]) === 'enum') {
76
+			$this->enum = explode(',', $match[2]);
77
+		} else {
78
+			$this->pattern = empty($match[2]) ? null : $match[2];
79
+		}
80
+	}
81
+
82
+	/**
83
+	 * @param string $definition
84
+	 * @param string[] $match
85
+	 * @throws Exception
86
+	 * @throws Exception
87
+	 */
88
+	private function parseRange($definition, $match)
89
+	{
90
+
91
+		if (!empty($match[3])) {
92
+			if ($match[1] === 'enum') {
93
+				throw new Exception("Range not allowed in enumeration definition: '{$definition}'");
94
+			}
95
+			if ($match[4] === '' && $match[5] === '') {
96
+				throw new Exception("Empty string range: '{$definition}'");
97
+			}
98
+			$exclusiveMinimum = $match[3] == '<';
99
+			$this->minLength = $match[4] === '' ? null : $match[4];
100
+			$this->maxLength = $match[5] === '' ? null : $match[5];
101
+			$exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
102
+			if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) {
103
+				self::swap($this->minLength, $this->maxLength);
104
+				self::swap($exclusiveMinimum, $exclusiveMaximum);
105
+			}
106
+			$this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength);
107
+			$this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength);
108
+		}
109
+	}
110
+
111
+	/**
112
+	 * @param string $definition
113
+	 * @param string[] $match
114
+	 * @throws Exception
115
+	 */
116
+	private function parseDefault($definition, $match)
117
+	{
118
+		$this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null;
119
+	}
120
+
121
+	/**
122
+	 * @param string $command The comment command
123
+	 * @param string $data Any data added after the command
124
+	 * @return AbstractType|boolean
125
+	 * @throws Exception
126
+	 * @throws Exception
127
+	 * @throws Exception
128
+	 */
129
+	public function handleCommand($command, $data = null)
130
+	{
131
+		switch (strtolower($command)) {
132
+			case 'default':
133
+				$this->default = $this->validateDefault($data);
134
+				return $this;
135
+
136
+			case 'pattern':
137
+				$this->pattern = $data;
138
+				return $this;
139
+
140
+			case 'enum':
141
+				if ($this->minLength !== null || $this->maxLength !== null) {
142
+					throw new Exception("Enumeration not allowed in ranged string: '{$data}'");
143
+				}
144
+				$words = self::wordSplit($data);
145
+				$this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words;
146
+				return $this;
147
+		}
148
+
149
+		return parent::handleCommand($command, $data);
150
+	}
151
+
152
+	public function toArray()
153
+	{
154
+		return self::arrayFilterNull(array_merge(array(
155
+			'type' => 'string',
156
+			'format' => empty($this->format) ? null : $this->format,
157
+			'pattern' => $this->pattern,
158
+			'default' => $this->default,
159
+			'minLength' => $this->minLength ? intval($this->minLength) : null,
160
+			'maxLength' => $this->maxLength ? intval($this->maxLength) : null,
161
+			'enum' => $this->enum,
162
+		), parent::toArray()));
163
+	}
164
+
165
+	/**
166
+	 * Validate a default string value, depending on subtype
167
+	 *
168
+	 * @param string $value the value to validate
169
+	 * @return string the value after validation (might become trimmed)
170
+	 * @throws Exception
171
+	 */
172
+	protected function validateDefault($value)
173
+	{
174
+		if (empty($value)) {
175
+			$type = $this->format ?: ($this->enum ? 'enum' : 'string');
176
+			throw new Exception("Empty {$type} default");
177
+		}
178
+
179
+		if (!empty($this->enum) && !in_array($value, $this->enum)) {
180
+			throw new Exception("Invalid enum default: '{$value}'");
181
+		}
182
+
183
+		if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) {
184
+			$type = $this->format ?: ($this->enum ? 'enum' : 'string');
185
+			throw new Exception("Default {$type} length beyond maximum: '{$value}'");
186
+		}
187
+
188
+		if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
189
+			$type = $this->format ?: ($this->enum ? 'enum' : 'string');
190
+			throw new Exception("Default {$type} length beyond minimum: '{$value}'");
191
+		}
192
+
193
+		return $value;
194
+	}
195
+
196
+	public function __toString()
197
+	{
198
+		return __CLASS__;
199
+	}
200 200
 
201 201
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/IntegerType.php 1 patch
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -15,163 +15,163 @@
 block discarded – undo
15 15
 class IntegerType extends AbstractType
16 16
 {
17 17
 
18
-    const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?';
19
-    const REGEX_DEFAULT = '(?:=(-?\d+))?';
20
-
21
-    private static $formats = array(
22
-        'int32' => 'int32',
23
-        'integer' => 'int32',
24
-        'int' => 'int32',
25
-        'int64' => 'int64',
26
-        'long' => 'int64',
27
-    );
28
-    private $format;
29
-    private $default = null;
30
-    private $maximum = null;
31
-    private $exclusiveMaximum = null;
32
-    private $minimum = null;
33
-    private $exclusiveMinimum = null;
34
-    private $enum = array();
35
-    private $multipleOf = null;
36
-
37
-    /**
38
-     * @throws Exception
39
-     */
40
-    protected function parseDefinition($definition)
41
-    {
42
-        $definition = self::trim($definition);
43
-
44
-        $match = array();
45
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
46
-            throw new Exception("Unparseable integer definition: '{$definition}'");
47
-        }
48
-
49
-        $this->parseFormat($definition, $match);
50
-        $this->parseRange($definition, $match);
51
-        $this->parseDefault($definition, $match);
52
-    }
53
-
54
-    /**
55
-     * @param string $definition
56
-     * @param string[] $match
57
-     * @throws Exception
58
-     */
59
-    private function parseFormat($definition, $match)
60
-    {
61
-        $type = strtolower($match[1]);
62
-        if (!isset(self::$formats[$type])) {
63
-            throw new Exception("Not an integer: '{$definition}'");
64
-        }
65
-        $this->format = self::$formats[$type];
66
-    }
67
-
68
-    /**
69
-     * @param string $definition
70
-     * @param string[] $match
71
-     * @throws Exception
72
-     */
73
-    private function parseRange($definition, $match)
74
-    {
75
-        if (!empty($match[2])) {
76
-            if ($match[3] === '' && $match[4] === '') {
77
-                throw new Exception("Empty integer range: '{$definition}'");
78
-            }
79
-
80
-            $this->exclusiveMinimum = $match[2] == '<';
81
-            $this->minimum = $match[3] === '' ? null : intval($match[3]);
82
-            $this->maximum = $match[4] === '' ? null : intval($match[4]);
83
-            $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
84
-            if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
85
-                self::swap($this->minimum, $this->maximum);
86
-                self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
87
-            }
88
-        }
89
-    }
90
-
91
-    /**
92
-     * @param string $definition
93
-     * @param string[] $match
94
-     * @throws Exception
95
-     */
96
-    private function parseDefault($definition, $match)
97
-    {
98
-        $this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
99
-    }
100
-
101
-    /**
102
-     * @param string $command The comment command
103
-     * @param string $data Any data added after the command
104
-     * @return AbstractType|boolean
105
-     * @throws Exception
106
-     * @throws Exception
107
-     * @throws Exception
108
-     */
109
-    public function handleCommand($command, $data = null)
110
-    {
111
-        switch (strtolower($command)) {
112
-            case 'default':
113
-                $this->default = $this->validateDefault($data);
114
-                return $this;
115
-
116
-            case 'enum':
117
-                $words = self::wordSplit($data);
118
-                foreach ($words as &$word) {
119
-                    $word = $this->validateDefault($word);
120
-                }
121
-                $this->enum = array_merge($this->enum, $words);
122
-                return $this;
123
-
124
-            case 'step':
125
-                if (($step = intval($data)) > 0) {
126
-                    $this->multipleOf = $step;
127
-                }
128
-                return $this;
129
-        }
130
-
131
-        return parent::handleCommand($command, $data);
132
-    }
133
-
134
-    public function toArray()
135
-    {
136
-        return self::arrayFilterNull(array_merge(array(
137
-            'type' => 'integer',
138
-            'format' => $this->format,
139
-            'default' => $this->default,
140
-            'minimum' => $this->minimum,
141
-            'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
142
-            'maximum' => $this->maximum,
143
-            'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
144
-            'enum' => $this->enum,
145
-            'multipleOf' => $this->multipleOf,
146
-        ), parent::toArray()));
147
-    }
148
-
149
-    public function __toString()
150
-    {
151
-        return __CLASS__;
152
-    }
153
-
154
-    /**
155
-     * @throws Exception
156
-     */
157
-    private function validateDefault($value)
158
-    {
159
-        if (preg_match('~^-?\d+$~', $value) !== 1) {
160
-            throw new Exception("Invalid integer default: '{$value}'");
161
-        }
162
-
163
-        if ($this->maximum) {
164
-            if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
165
-                throw new Exception("Default integer beyond maximum: '{$value}'");
166
-            }
167
-        }
168
-        if ($this->minimum) {
169
-            if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
170
-                throw new Exception("Default integer beyond minimum: '{$value}'");
171
-            }
172
-        }
173
-
174
-        return intval($value);
175
-    }
18
+	const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?';
19
+	const REGEX_DEFAULT = '(?:=(-?\d+))?';
20
+
21
+	private static $formats = array(
22
+		'int32' => 'int32',
23
+		'integer' => 'int32',
24
+		'int' => 'int32',
25
+		'int64' => 'int64',
26
+		'long' => 'int64',
27
+	);
28
+	private $format;
29
+	private $default = null;
30
+	private $maximum = null;
31
+	private $exclusiveMaximum = null;
32
+	private $minimum = null;
33
+	private $exclusiveMinimum = null;
34
+	private $enum = array();
35
+	private $multipleOf = null;
36
+
37
+	/**
38
+	 * @throws Exception
39
+	 */
40
+	protected function parseDefinition($definition)
41
+	{
42
+		$definition = self::trim($definition);
43
+
44
+		$match = array();
45
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
46
+			throw new Exception("Unparseable integer definition: '{$definition}'");
47
+		}
48
+
49
+		$this->parseFormat($definition, $match);
50
+		$this->parseRange($definition, $match);
51
+		$this->parseDefault($definition, $match);
52
+	}
53
+
54
+	/**
55
+	 * @param string $definition
56
+	 * @param string[] $match
57
+	 * @throws Exception
58
+	 */
59
+	private function parseFormat($definition, $match)
60
+	{
61
+		$type = strtolower($match[1]);
62
+		if (!isset(self::$formats[$type])) {
63
+			throw new Exception("Not an integer: '{$definition}'");
64
+		}
65
+		$this->format = self::$formats[$type];
66
+	}
67
+
68
+	/**
69
+	 * @param string $definition
70
+	 * @param string[] $match
71
+	 * @throws Exception
72
+	 */
73
+	private function parseRange($definition, $match)
74
+	{
75
+		if (!empty($match[2])) {
76
+			if ($match[3] === '' && $match[4] === '') {
77
+				throw new Exception("Empty integer range: '{$definition}'");
78
+			}
79
+
80
+			$this->exclusiveMinimum = $match[2] == '<';
81
+			$this->minimum = $match[3] === '' ? null : intval($match[3]);
82
+			$this->maximum = $match[4] === '' ? null : intval($match[4]);
83
+			$this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null;
84
+			if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) {
85
+				self::swap($this->minimum, $this->maximum);
86
+				self::swap($this->exclusiveMinimum, $this->exclusiveMaximum);
87
+			}
88
+		}
89
+	}
90
+
91
+	/**
92
+	 * @param string $definition
93
+	 * @param string[] $match
94
+	 * @throws Exception
95
+	 */
96
+	private function parseDefault($definition, $match)
97
+	{
98
+		$this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null;
99
+	}
100
+
101
+	/**
102
+	 * @param string $command The comment command
103
+	 * @param string $data Any data added after the command
104
+	 * @return AbstractType|boolean
105
+	 * @throws Exception
106
+	 * @throws Exception
107
+	 * @throws Exception
108
+	 */
109
+	public function handleCommand($command, $data = null)
110
+	{
111
+		switch (strtolower($command)) {
112
+			case 'default':
113
+				$this->default = $this->validateDefault($data);
114
+				return $this;
115
+
116
+			case 'enum':
117
+				$words = self::wordSplit($data);
118
+				foreach ($words as &$word) {
119
+					$word = $this->validateDefault($word);
120
+				}
121
+				$this->enum = array_merge($this->enum, $words);
122
+				return $this;
123
+
124
+			case 'step':
125
+				if (($step = intval($data)) > 0) {
126
+					$this->multipleOf = $step;
127
+				}
128
+				return $this;
129
+		}
130
+
131
+		return parent::handleCommand($command, $data);
132
+	}
133
+
134
+	public function toArray()
135
+	{
136
+		return self::arrayFilterNull(array_merge(array(
137
+			'type' => 'integer',
138
+			'format' => $this->format,
139
+			'default' => $this->default,
140
+			'minimum' => $this->minimum,
141
+			'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null,
142
+			'maximum' => $this->maximum,
143
+			'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null,
144
+			'enum' => $this->enum,
145
+			'multipleOf' => $this->multipleOf,
146
+		), parent::toArray()));
147
+	}
148
+
149
+	public function __toString()
150
+	{
151
+		return __CLASS__;
152
+	}
153
+
154
+	/**
155
+	 * @throws Exception
156
+	 */
157
+	private function validateDefault($value)
158
+	{
159
+		if (preg_match('~^-?\d+$~', $value) !== 1) {
160
+			throw new Exception("Invalid integer default: '{$value}'");
161
+		}
162
+
163
+		if ($this->maximum) {
164
+			if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) {
165
+				throw new Exception("Default integer beyond maximum: '{$value}'");
166
+			}
167
+		}
168
+		if ($this->minimum) {
169
+			if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) {
170
+				throw new Exception("Default integer beyond minimum: '{$value}'");
171
+			}
172
+		}
173
+
174
+		return intval($value);
175
+	}
176 176
 
177 177
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/AbstractType.php 1 patch
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -16,193 +16,193 @@
 block discarded – undo
16 16
 abstract class AbstractType extends AbstractObject
17 17
 {
18 18
 
19
-    const REGEX_START = '/^';
20
-    const REGEX_FORMAT = '([a-z][a-z0-9]*)';
21
-    const REGEX_CONTENT = '(?:\((.*)\))?';
22
-    const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?';
23
-    const REGEX_DEFAULT = '(?:=(.+))?';
24
-    const REGEX_END = '$/i';
25
-
26
-    private static $classTypes = array(
27
-        'integer' => 'Integer',
28
-        'int' => 'Integer',
29
-        'int32' => 'Integer',
30
-        'int64' => 'Integer',
31
-        'long' => 'Integer',
32
-        'float' => 'Number',
33
-        'double' => 'Number',
34
-        'string' => 'String',
35
-        'uuid' => 'StringUuid',
36
-        'byte' => 'String',
37
-        'binary' => 'String',
38
-        'password' => 'String',
39
-        'enum' => 'String',
40
-        'boolean' => 'Boolean',
41
-        'bool' => 'Boolean',
42
-        'array' => 'Array',
43
-        'csv' => 'Array',
44
-        'ssv' => 'Array',
45
-        'tsv' => 'Array',
46
-        'pipes' => 'Array',
47
-        'date' => 'Date',
48
-        'datetime' => 'Date',
49
-        'date-time' => 'Date',
50
-        'object' => 'Object',
51
-        'refobject' => 'ReferenceObject',
52
-        'allof' => 'AllOf',
53
-    );
54
-
55
-    private $example = null;
56
-
57
-    /**
58
-     * Swap values of two variables.
59
-     * Used for sorting.
60
-     * @param mixed $a
61
-     * @param mixed $b
62
-     */
63
-    protected static function swap(&$a, &$b)
64
-    {
65
-        $tmp = $a;
66
-        $a = $b;
67
-        $b = $tmp;
68
-    }
69
-
70
-    /**
71
-     * @param string $list
72
-     * @return array
73
-     */
74
-    protected static function parseList($list)
75
-    {
76
-        $ret = array();
77
-        while ($item = self::parseListItem($list)) {
78
-            $ret[] = $item;
79
-        }
80
-        return $ret;
81
-    }
82
-
83
-    /**
84
-     * Extract an item from a comma-separated list of items.
85
-     *
86
-     * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`.
87
-     * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item.
88
-     *
89
-     * @param string $list the list to parse
90
-     * @return string the extracted item
91
-     */
92
-    protected static function parseListItem(&$list)
93
-    {
94
-        $item = '';
95
-
96
-        $depth = 0;
97
-        $index = 0;
98
-        while ($index < strlen($list)) {
99
-            $c = $list[$index++];
100
-
101
-            if (strpos('{([<', $c) !== false) {
102
-                ++$depth;
103
-            } elseif (strpos('})]>', $c) !== false) {
104
-                --$depth;
105
-            } elseif ($c === ',' && $depth === 0) {
106
-                break;
107
-            }
108
-
109
-            $item .= $c;
110
-        }
111
-        $list = substr($list, $index);
112
-
113
-        return $item;
114
-    }
115
-
116
-    /**
117
-     * @param AbstractObject $parent
118
-     * @param $definition
119
-     * @constructor
120
-     */
121
-    public function __construct(AbstractObject $parent, $definition)
122
-    {
123
-        parent::__construct($parent);
124
-
125
-        $this->parseDefinition($definition);
126
-    }
127
-
128
-    abstract protected function parseDefinition($definition);
129
-
130
-    /**
131
-     * Overwrites default AbstractObject parser, since Types should not handle
132
-     * extensions themselves.
133
-     *
134
-     * @param string $command
135
-     * @param string $data
136
-     * @return AbstractType|boolean
137
-     * @throws Exception
138
-     */
139
-    public function handleCommand($command, $data = null)
140
-    {
141
-        if (strtolower($command) === 'example') {
142
-            if ($data === '') {
143
-                throw new Exception("Missing content for type example");
144
-            }
145
-            $json = preg_replace_callback('/([^{}:,]+)/', function ($match) {
146
-                json_decode($match[1]);
147
-                return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]);
148
-            }, trim($data));
149
-            $this->example = json_decode($json, true);
150
-
151
-            // In case input contains special chars, the above preg_replace would fail
152
-            //   Input could be a well-formed json already though
153
-            if ($this->example === null) {
154
-                $this->example = json_decode($data, true);
155
-            }
156
-            // If all fails, use input as-is
157
-            if ($this->example === null) {
158
-                $this->example = $data;
159
-            }
160
-
161
-            return $this;
162
-        }
163
-
164
-        return false;
165
-    }
166
-
167
-    public function toArray()
168
-    {
169
-        return self::arrayFilterNull(array_merge(array(
170
-            'example' => $this->example,
171
-        ), parent::toArray()));
172
-    }
173
-
174
-    /**
175
-     * @param AbstractObject $parent
176
-     * @param string $definition
177
-     * @param string $error
178
-     * @return self
179
-     * @throws Exception
180
-     */
181
-    public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'")
182
-    {
183
-        // Parse regex
184
-        $match = array();
185
-        if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) {
186
-            $format = strtolower($match[1]);
187
-        } elseif (preg_match('/^(\[)(?:.*?)\]$/i', $definition, $match) === 1) {
188
-            $format = 'array';
189
-        } elseif (preg_match('/^(\{)(?:.*?)\}$/i', $definition, $match) === 1) {
190
-            $format = 'object';
191
-        } else {
192
-            throw new Exception(sprintf($error, $definition));
193
-        }
194
-
195
-        // Internal type if type known and not overwritten by definition
196
-        if ($parent->getTypeRegistry()->has($format)) {
197
-            $class = $parent->getTypeRegistry()->get($format);
198
-            return new $class($parent, $definition);
199
-        } elseif (isset(self::$classTypes[$format])) {
200
-            $type = self::$classTypes[$format];
201
-            $class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type";
202
-            return new $class($parent, $definition);
203
-        } else {
204
-            return new ReferenceObjectType($parent, $definition);
205
-        }
206
-    }
19
+	const REGEX_START = '/^';
20
+	const REGEX_FORMAT = '([a-z][a-z0-9]*)';
21
+	const REGEX_CONTENT = '(?:\((.*)\))?';
22
+	const REGEX_RANGE = '(?:([[<])(\\d*),(\\d*)([\\]>]))?';
23
+	const REGEX_DEFAULT = '(?:=(.+))?';
24
+	const REGEX_END = '$/i';
25
+
26
+	private static $classTypes = array(
27
+		'integer' => 'Integer',
28
+		'int' => 'Integer',
29
+		'int32' => 'Integer',
30
+		'int64' => 'Integer',
31
+		'long' => 'Integer',
32
+		'float' => 'Number',
33
+		'double' => 'Number',
34
+		'string' => 'String',
35
+		'uuid' => 'StringUuid',
36
+		'byte' => 'String',
37
+		'binary' => 'String',
38
+		'password' => 'String',
39
+		'enum' => 'String',
40
+		'boolean' => 'Boolean',
41
+		'bool' => 'Boolean',
42
+		'array' => 'Array',
43
+		'csv' => 'Array',
44
+		'ssv' => 'Array',
45
+		'tsv' => 'Array',
46
+		'pipes' => 'Array',
47
+		'date' => 'Date',
48
+		'datetime' => 'Date',
49
+		'date-time' => 'Date',
50
+		'object' => 'Object',
51
+		'refobject' => 'ReferenceObject',
52
+		'allof' => 'AllOf',
53
+	);
54
+
55
+	private $example = null;
56
+
57
+	/**
58
+	 * Swap values of two variables.
59
+	 * Used for sorting.
60
+	 * @param mixed $a
61
+	 * @param mixed $b
62
+	 */
63
+	protected static function swap(&$a, &$b)
64
+	{
65
+		$tmp = $a;
66
+		$a = $b;
67
+		$b = $tmp;
68
+	}
69
+
70
+	/**
71
+	 * @param string $list
72
+	 * @return array
73
+	 */
74
+	protected static function parseList($list)
75
+	{
76
+		$ret = array();
77
+		while ($item = self::parseListItem($list)) {
78
+			$ret[] = $item;
79
+		}
80
+		return $ret;
81
+	}
82
+
83
+	/**
84
+	 * Extract an item from a comma-separated list of items.
85
+	 *
86
+	 * i.e. `a(x(x,x)),b(x)` returns `a(x(x,x))` and changes `$list` into `b(x)`.
87
+	 * Note: brace nesting is not checked, e.g. `a{b(})` is a valid list item.
88
+	 *
89
+	 * @param string $list the list to parse
90
+	 * @return string the extracted item
91
+	 */
92
+	protected static function parseListItem(&$list)
93
+	{
94
+		$item = '';
95
+
96
+		$depth = 0;
97
+		$index = 0;
98
+		while ($index < strlen($list)) {
99
+			$c = $list[$index++];
100
+
101
+			if (strpos('{([<', $c) !== false) {
102
+				++$depth;
103
+			} elseif (strpos('})]>', $c) !== false) {
104
+				--$depth;
105
+			} elseif ($c === ',' && $depth === 0) {
106
+				break;
107
+			}
108
+
109
+			$item .= $c;
110
+		}
111
+		$list = substr($list, $index);
112
+
113
+		return $item;
114
+	}
115
+
116
+	/**
117
+	 * @param AbstractObject $parent
118
+	 * @param $definition
119
+	 * @constructor
120
+	 */
121
+	public function __construct(AbstractObject $parent, $definition)
122
+	{
123
+		parent::__construct($parent);
124
+
125
+		$this->parseDefinition($definition);
126
+	}
127
+
128
+	abstract protected function parseDefinition($definition);
129
+
130
+	/**
131
+	 * Overwrites default AbstractObject parser, since Types should not handle
132
+	 * extensions themselves.
133
+	 *
134
+	 * @param string $command
135
+	 * @param string $data
136
+	 * @return AbstractType|boolean
137
+	 * @throws Exception
138
+	 */
139
+	public function handleCommand($command, $data = null)
140
+	{
141
+		if (strtolower($command) === 'example') {
142
+			if ($data === '') {
143
+				throw new Exception("Missing content for type example");
144
+			}
145
+			$json = preg_replace_callback('/([^{}:,]+)/', function ($match) {
146
+				json_decode($match[1]);
147
+				return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]);
148
+			}, trim($data));
149
+			$this->example = json_decode($json, true);
150
+
151
+			// In case input contains special chars, the above preg_replace would fail
152
+			//   Input could be a well-formed json already though
153
+			if ($this->example === null) {
154
+				$this->example = json_decode($data, true);
155
+			}
156
+			// If all fails, use input as-is
157
+			if ($this->example === null) {
158
+				$this->example = $data;
159
+			}
160
+
161
+			return $this;
162
+		}
163
+
164
+		return false;
165
+	}
166
+
167
+	public function toArray()
168
+	{
169
+		return self::arrayFilterNull(array_merge(array(
170
+			'example' => $this->example,
171
+		), parent::toArray()));
172
+	}
173
+
174
+	/**
175
+	 * @param AbstractObject $parent
176
+	 * @param string $definition
177
+	 * @param string $error
178
+	 * @return self
179
+	 * @throws Exception
180
+	 */
181
+	public static function typeFactory($parent, $definition, $error = "Unparseable schema type definition: '%s'")
182
+	{
183
+		// Parse regex
184
+		$match = array();
185
+		if (preg_match('/^([a-z]+)/i', $definition, $match) === 1) {
186
+			$format = strtolower($match[1]);
187
+		} elseif (preg_match('/^(\[)(?:.*?)\]$/i', $definition, $match) === 1) {
188
+			$format = 'array';
189
+		} elseif (preg_match('/^(\{)(?:.*?)\}$/i', $definition, $match) === 1) {
190
+			$format = 'object';
191
+		} else {
192
+			throw new Exception(sprintf($error, $definition));
193
+		}
194
+
195
+		// Internal type if type known and not overwritten by definition
196
+		if ($parent->getTypeRegistry()->has($format)) {
197
+			$class = $parent->getTypeRegistry()->get($format);
198
+			return new $class($parent, $definition);
199
+		} elseif (isset(self::$classTypes[$format])) {
200
+			$type = self::$classTypes[$format];
201
+			$class = "\\SwaggerGen\\Swagger\\Type\\{$type}Type";
202
+			return new $class($parent, $definition);
203
+		} else {
204
+			return new ReferenceObjectType($parent, $definition);
205
+		}
206
+	}
207 207
 
208 208
 }
Please login to merge, or discard this patch.
SwaggerGen/Swagger/Type/Custom/MacType.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 MacType extends StringType implements ICustomType
18 18
 {
19 19
 
20
-    const PATTERN = '^([0-9A-F]){2}(:[0-9A-F]{2}){5}$';
21
-
22
-    /**
23
-     * List of formats recognized by this class
24
-     * @var string[]
25
-     */
26
-    private static $formats = array('mac');
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 MAC definition: '{$definition}'");
54
-        }
55
-
56
-        if (!in_array(strtolower($match[1]), self::$formats)) {
57
-            throw new Exception("Not a MAC: '{$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 MAC default");
74
-        }
75
-
76
-        if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) {
77
-            throw new Exception("Invalid MAC 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-F]){2}(:[0-9A-F]{2}){5}$';
21
+
22
+	/**
23
+	 * List of formats recognized by this class
24
+	 * @var string[]
25
+	 */
26
+	private static $formats = array('mac');
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 MAC definition: '{$definition}'");
54
+		}
55
+
56
+		if (!in_array(strtolower($match[1]), self::$formats)) {
57
+			throw new Exception("Not a MAC: '{$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 MAC default");
74
+		}
75
+
76
+		if (preg_match('/^([0-9A-F]){2}(:[0-9A-F]{2}){5}$/', $value) !== 1) {
77
+			throw new Exception("Invalid MAC 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.