Operation.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace Swagger\Annotations;
  6. use Swagger\Logger;
  7. /**
  8. * @Annotation
  9. * Base class for the @SWG\Get(), @SWG\Post(), @SWG\Put(), @SWG\Delete(), @SWG\Patch()
  10. *
  11. * A Swagger "Operation Object": https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#operationObject
  12. */
  13. abstract class Operation extends AbstractAnnotation
  14. {
  15. /**
  16. * key in the Swagger "Paths Object" for this operation
  17. * @var string
  18. */
  19. public $path;
  20. /**
  21. * Key in the Swagger "Path Item Object" for this operation.
  22. * Allowed values: 'get', 'post', put', 'delete', 'options', 'head' and 'patch'
  23. * @var string
  24. */
  25. public $method;
  26. /**
  27. * A list of tags for API documentation control. Tags can be used for logical grouping of operations by resources or any other qualifier.
  28. * @var array
  29. */
  30. public $tags;
  31. /**
  32. * A short summary of what the operation does. For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
  33. * @var string
  34. */
  35. public $summary;
  36. /**
  37. * A verbose explanation of the operation behavior. GFM syntax can be used for rich text representation.
  38. * @var string
  39. */
  40. public $description;
  41. /**
  42. * Additional external documentation for this operation.
  43. * @var ExternalDocumentation
  44. */
  45. public $externalDocs;
  46. /**
  47. * A friendly name for the operation.
  48. * The id MUST be unique among all operations described in the API.
  49. * Tools and libraries MAY use the operation id to uniquely identify an operation.
  50. * @var string
  51. */
  52. public $operationId;
  53. /**
  54. * A list of MIME types the operation can consume.
  55. * This overrides the [consumes](#swaggerConsumes) definition at the Swagger Object.
  56. * An empty value MAY be used to clear the global definition.
  57. * Value MUST be as described under Mime Types.
  58. * @var array
  59. */
  60. public $consumes;
  61. /**
  62. * A list of MIME types the operation can produce.
  63. * This overrides the [produces](#swaggerProduces) definition at the Swagger Object.
  64. * An empty value MAY be used to clear the global definition.
  65. * Value MUST be as described under Mime Types.
  66. * @var array
  67. */
  68. public $produces;
  69. /**
  70. * A list of parameters that are applicable for this operation.
  71. * If a parameter is already defined at the Path Item, the new definition will override it, but can never remove it. The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location.
  72. * The list can use the Reference Object to link to parameters that are defined at the Swagger Object's parameters.
  73. * There can be one "body" parameter at most.
  74. * @var Parameter[]
  75. */
  76. public $parameters;
  77. /**
  78. * The list of possible responses as they are returned from executing this operation.
  79. * @var array
  80. */
  81. public $responses;
  82. /**
  83. * The transfer protocol for the operation.
  84. * Values MUST be from the list: "http", "https", "ws", "wss".
  85. * The value overrides the Swagger Object schemes definition.
  86. * @var array
  87. */
  88. public $schemes;
  89. /**
  90. * Declares this operation to be deprecated.
  91. * Usage of the declared operation should be refrained. Default value is false.
  92. * @var boolean
  93. */
  94. public $deprecated;
  95. /**
  96. * A declaration of which security schemes are applied for this operation.
  97. * The list of values describes alternative security schemes that can be used (that is, there is a logical OR between the security requirements).
  98. * This definition overrides any declared top-level security.
  99. * To remove a top-level security declaration, an empty array can be used.
  100. * @var array
  101. */
  102. public $security;
  103. /** @inheritdoc */
  104. public static $_required = ['responses'];
  105. /** @inheritdoc */
  106. public static $_types = [
  107. 'path' => 'string',
  108. 'method' => 'string',
  109. 'tags' => '[string]',
  110. 'summary' => 'string',
  111. 'description' => 'string',
  112. 'consumes' => '[string]',
  113. 'produces' => '[string]',
  114. 'schemes' => '[scheme]',
  115. 'deprecated' => 'boolean'
  116. ];
  117. /** @inheritdoc */
  118. public static $_nested = [
  119. 'Swagger\Annotations\Parameter' => ['parameters'],
  120. 'Swagger\Annotations\Response' => ['responses', 'response'],
  121. 'Swagger\Annotations\ExternalDocumentation' => 'externalDocs'
  122. ];
  123. /** @inheritdoc */
  124. public function jsonSerialize()
  125. {
  126. $data = parent::jsonSerialize();
  127. unset($data->method);
  128. unset($data->path);
  129. return $data;
  130. }
  131. public function validate($parents = [], $skip = [], $ref = '')
  132. {
  133. if (in_array($this, $skip, true)) {
  134. return true;
  135. }
  136. $valid = parent::validate($parents, $skip);
  137. if ($this->responses !== null) {
  138. foreach ($this->responses as $response) {
  139. if ($response->response !== 'default' && preg_match('/^[12345]{1}[0-9]{2}$/', $response->response) === 0) {
  140. Logger::notice('Invalid value "' . $response->response . '" for ' . $response->_identity([]) . '->response, expecting "default" or a HTTP Status Code in ' . $response->_context);
  141. }
  142. }
  143. }
  144. return $valid;
  145. }
  146. }