Parameter.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace Swagger\Annotations;
  6. use \Swagger\Logger;
  7. /**
  8. * @Annotation
  9. * Describes a single operation parameter.
  10. *
  11. * A Swagger "Parameter Object": https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#parameterObject
  12. */
  13. class Parameter extends AbstractAnnotation
  14. {
  15. /**
  16. * $ref See http://json-schema.org/latest/json-schema-core.html#rfc.section.7
  17. * @var string
  18. */
  19. public $ref;
  20. /**
  21. * The key into Swagger->parameters or Path->parameters array.
  22. * @var string
  23. */
  24. public $parameter;
  25. /**
  26. * The name of the parameter. Parameter names are case sensitive. If in is "path", the name field MUST correspond to the associated path segment from the path field in the Paths Object. See Path Templating for further information. For all other cases, the name corresponds to the parameter name used based on the in property.
  27. * @var string
  28. */
  29. public $name;
  30. /**
  31. * The location of the parameter. Possible values are "query", "header", "path", "formData" or "body".
  32. * @var string
  33. */
  34. public $in;
  35. /**
  36. * A brief description of the parameter. This could contain examples of use. GFM syntax can be used for rich text representation.
  37. * @var string
  38. */
  39. public $description;
  40. /**
  41. * Determines whether this parameter is mandatory. If the parameter is in "path", this property is required and its value MUST be true. Otherwise, the property MAY be included and its default value is false.
  42. * @var boolean
  43. */
  44. public $required;
  45. /**
  46. * The schema defining the type used for the body parameter.
  47. * @var Schema
  48. */
  49. public $schema;
  50. /**
  51. * The type of the parameter. Since the parameter is not located at the request body, it is limited to simple types (that is, not an object). The value MUST be one of "string", "number", "integer", "boolean", "array" or "file". If type is "file", the consumes MUST be either "multipart/form-data" or " application/x-www-form-urlencoded" and the parameter MUST be in "formData".
  52. * @var string
  53. */
  54. public $type;
  55. /**
  56. * The extending format for the previously mentioned type. See Data Type Formats for further details.
  57. * @var string
  58. */
  59. public $format;
  60. /**
  61. * Sets the ability to pass empty-valued parameters. This is valid only for either query or formData parameters and allows you to send a parameter with a name only or an empty value. Default value is false.
  62. * @var boolean
  63. */
  64. public $allowEmptyValue;
  65. /**
  66. * Required if type is "array". Describes the type of items in the array.
  67. * @var \Swagger\Annotations\Items
  68. */
  69. public $items;
  70. /**
  71. * Determines the format of the array if type array is used. Possible values are: csv - comma separated values foo,bar. ssv - space separated values foo bar. tsv - tab separated values foo\tbar. pipes - pipe separated values foo|bar. multi - corresponds to multiple parameter instances instead of multiple values for a single instance foo=bar&foo=baz. This is valid only for parameters in "query" or "formData". Default value is csv.
  72. * @var string
  73. */
  74. public $collectionFormat;
  75. /**
  76. * Sets a default value to the parameter. The type of the value depends on the defined type. See http://json-schema.org/latest/json-schema-validation.html#anchor101.
  77. * @var mixed
  78. */
  79. public $default = UNDEFINED;
  80. /**
  81. * See http://json-schema.org/latest/json-schema-validation.html#anchor17.
  82. * @var number
  83. */
  84. public $maximum;
  85. /**
  86. * See http://json-schema.org/latest/json-schema-validation.html#anchor17.
  87. * @var boolean
  88. */
  89. public $exclusiveMaximum;
  90. /**
  91. * See http://json-schema.org/latest/json-schema-validation.html#anchor21.
  92. * @var number
  93. */
  94. public $minimum;
  95. /**
  96. * See http://json-schema.org/latest/json-schema-validation.html#anchor21.
  97. * @var boolean
  98. */
  99. public $exclusiveMinimum;
  100. /**
  101. * See http://json-schema.org/latest/json-schema-validation.html#anchor26.
  102. * @var integer
  103. */
  104. public $maxLength;
  105. /**
  106. * See http://json-schema.org/latest/json-schema-validation.html#anchor29.
  107. * @var integer
  108. */
  109. public $minLength;
  110. /**
  111. * See http://json-schema.org/latest/json-schema-validation.html#anchor33.
  112. * @var string
  113. */
  114. public $pattern;
  115. /**
  116. * See http://json-schema.org/latest/json-schema-validation.html#anchor42.
  117. * @var integer
  118. */
  119. public $maxItems;
  120. /**
  121. * See http://json-schema.org/latest/json-schema-validation.html#anchor45.
  122. * @var integer
  123. */
  124. public $minItems;
  125. /**
  126. * See http://json-schema.org/latest/json-schema-validation.html#anchor49.
  127. * @var boolean
  128. */
  129. public $uniqueItems;
  130. /**
  131. * See http://json-schema.org/latest/json-schema-validation.html#anchor76.
  132. * @var array
  133. */
  134. public $enum;
  135. /**
  136. * See http://json-schema.org/latest/json-schema-validation.html#anchor14.
  137. * @var number
  138. */
  139. public $multipleOf;
  140. /** @inheritdoc */
  141. public static $_required = ['name', 'in'];
  142. /** @inheritdoc */
  143. public static $_types = [
  144. 'name' => 'string',
  145. 'in' => ['query', 'header', 'path', 'formData', 'body'],
  146. 'description' => 'string',
  147. 'required' => 'boolean',
  148. 'format' => 'string',
  149. 'collectionFormat' => ['csv', 'ssv', 'tsv', 'pipes', 'multi'],
  150. 'maximum' => 'number',
  151. 'exclusiveMaximum' => 'boolean',
  152. 'minimum' => 'number',
  153. 'exclusiveMinimum' => 'boolean',
  154. 'maxLength' => 'integer',
  155. 'minLength' => 'integer',
  156. 'pattern' => 'string',
  157. 'maxItems' => 'integer',
  158. 'minItems' => 'integer',
  159. 'uniqueItems' => 'boolean',
  160. 'multipleOf' => 'integer',
  161. ];
  162. /** @inheritdoc */
  163. public static $_nested = [
  164. 'Swagger\Annotations\Items' => 'items',
  165. 'Swagger\Annotations\Schema' => 'schema'
  166. ];
  167. /** @inheritdoc */
  168. public static $_parents = [
  169. 'Swagger\Annotations\Operation',
  170. 'Swagger\Annotations\Get',
  171. 'Swagger\Annotations\Post',
  172. 'Swagger\Annotations\Put',
  173. 'Swagger\Annotations\Delete',
  174. 'Swagger\Annotations\Patch',
  175. 'Swagger\Annotations\Path',
  176. 'Swagger\Annotations\Head',
  177. 'Swagger\Annotations\Options',
  178. 'Swagger\Annotations\Swagger'
  179. ];
  180. /** @inheritdoc */
  181. public function validate($parents = [], $skip = [], $ref = '')
  182. {
  183. if (in_array($this, $skip, true)) {
  184. return true;
  185. }
  186. $valid = parent::validate($parents, $skip, $ref);
  187. if (empty($this->ref)) {
  188. if ($this->in === 'body') {
  189. if ($this->schema === null) {
  190. Logger::notice('Field "schema" is required when ' . $this->identity() . ' is in "' . $this->in . '" in ' . $this->_context);
  191. $valid = false;
  192. }
  193. } else {
  194. $validTypes = ['string', 'number', 'integer', 'boolean', 'array', 'file'];
  195. if ($this->type === null) {
  196. Logger::notice($this->identity() . '->type is required when ' . $this->_identity([]) . '->in == "' . $this->in . '" in ' . $this->_context);
  197. $valid = false;
  198. } elseif ($this->type === 'array' && $this->items === null) {
  199. Logger::notice($this->identity() . '->items required when ' . $this->_identity([]) . '->type == "array" in ' . $this->_context);
  200. $valid = false;
  201. } elseif (in_array($this->type, $validTypes) === false) {
  202. $valid = false;
  203. Logger::notice($this->identity() . '->type must be "' . implode('", "', $validTypes) . '" when ' . $this->_identity([]) . '->in != "body" in ' . $this->_context);
  204. } elseif ($this->type === 'file' && $this->in !== 'formData') {
  205. Logger::notice($this->identity() . '->in must be "formData" when ' . $this->_identity([]) . '->type == "file" in ' . $this->_context);
  206. $valid = false;
  207. }
  208. }
  209. }
  210. return $valid;
  211. }
  212. /** @inheritdoc */
  213. public function identity()
  214. {
  215. return parent::_identity(['name', 'in']);
  216. }
  217. }