vendor/zircote/swagger-php/src/Annotations/Operation.php line 216

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @license Apache 2.0
  4.  */
  5. namespace OpenApi\Annotations;
  6. use OpenApi\Generator;
  7. /**
  8.  * Base class for `@OA\Get`,  `@OA\Post`,  `@OA\Put`,  etc.
  9.  *
  10.  * Describes a single API operation on a path.
  11.  *
  12.  * @see [OAI Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operation-object)
  13.  *
  14.  * @Annotation
  15.  */
  16. abstract class Operation extends AbstractAnnotation
  17. {
  18.     /**
  19.      * Key in the OpenApi "Paths Object" for this operation.
  20.      *
  21.      * @var string
  22.      */
  23.     public $path Generator::UNDEFINED;
  24.     /**
  25.      * A list of tags for API documentation control.
  26.      *
  27.      * Tags can be used for logical grouping of operations by resources or any other qualifier.
  28.      *
  29.      * @var string[]
  30.      */
  31.     public $tags Generator::UNDEFINED;
  32.     /**
  33.      * Key in the OpenApi "Path Item Object" for this operation.
  34.      *
  35.      * Allowed values: 'get', 'post', put', 'patch', 'delete', 'options', 'head' and 'trace'.
  36.      *
  37.      * @var string
  38.      */
  39.     public $method Generator::UNDEFINED;
  40.     /**
  41.      * A short summary of what the operation does.
  42.      *
  43.      * @var string
  44.      */
  45.     public $summary Generator::UNDEFINED;
  46.     /**
  47.      * A verbose explanation of the operation behavior.
  48.      *
  49.      * CommonMark syntax MAY be used for rich text representation.
  50.      *
  51.      * @var string
  52.      */
  53.     public $description Generator::UNDEFINED;
  54.     /**
  55.      * Additional external documentation for this operation.
  56.      *
  57.      * @var ExternalDocumentation
  58.      */
  59.     public $externalDocs Generator::UNDEFINED;
  60.     /**
  61.      * Unique string used to identify the operation.
  62.      *
  63.      * The id must be unique among all operations described in the API.
  64.      * Tools and libraries may use the operationId to uniquely identify an operation, therefore, it is recommended to
  65.      * follow common programming naming conventions.
  66.      *
  67.      * @var string
  68.      */
  69.     public $operationId Generator::UNDEFINED;
  70.     /**
  71.      * A list of parameters that are applicable for this operation.
  72.      *
  73.      * If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
  74.      * The list must not include duplicated parameters.
  75.      *
  76.      * A unique parameter is defined by a combination of a name and location.
  77.      *
  78.      * The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object's
  79.      * components/parameters.
  80.      *
  81.      * @var Parameter[]
  82.      */
  83.     public $parameters Generator::UNDEFINED;
  84.     /**
  85.      * The request body applicable for this operation.
  86.      *
  87.      * The requestBody is only supported in HTTP methods where the HTTP 1.1 specification RFC7231 has explicitly
  88.      * defined semantics for request bodies. In other cases where the HTTP spec is vague, requestBody shall be ignored
  89.      * by consumers.
  90.      *
  91.      * @var RequestBody
  92.      */
  93.     public $requestBody Generator::UNDEFINED;
  94.     /**
  95.      * The list of possible responses as they are returned from executing this operation.
  96.      *
  97.      * @var Response[]
  98.      */
  99.     public $responses Generator::UNDEFINED;
  100.     /**
  101.      * A map of possible out-of band callbacks related to the parent operation.
  102.      *
  103.      * The key is a unique identifier for the Callback Object.
  104.      *
  105.      * Each value in the map is a Callback Object that describes a request that may be initiated by the API provider
  106.      * and the expected responses. The key value used to identify the callback object is an expression, evaluated at
  107.      * runtime, that identifies a URL to use for the callback operation.
  108.      *
  109.      * @var array
  110.      */
  111.     public $callbacks Generator::UNDEFINED;
  112.     /**
  113.      * Declares this operation to be deprecated.
  114.      *
  115.      * Consumers should refrain from usage of the declared operation.
  116.      *
  117.      * Default value is false.
  118.      *
  119.      * @var bool
  120.      */
  121.     public $deprecated Generator::UNDEFINED;
  122.     /**
  123.      * A declaration of which security mechanisms can be used for this operation.
  124.      *
  125.      * The list of values includes alternative security requirement objects that can be used.
  126.      *
  127.      * Only one of the security requirement objects need to be satisfied to authorize a request.
  128.      *
  129.      * This definition overrides any declared top-level security.
  130.      * To remove a top-level security declaration, an empty array can be used.
  131.      *
  132.      * @var array
  133.      */
  134.     public $security Generator::UNDEFINED;
  135.     /**
  136.      * An alternative server array to service this operation.
  137.      *
  138.      * If an alternative server object is specified at the Path Item Object or Root level, it will be overridden by
  139.      * this value.
  140.      *
  141.      * @var Server[]
  142.      */
  143.     public $servers Generator::UNDEFINED;
  144.     /**
  145.      * @inheritdoc
  146.      */
  147.     public static $_required = ['responses'];
  148.     /**
  149.      * @inheritdoc
  150.      */
  151.     public static $_types = [
  152.         'path' => 'string',
  153.         'method' => 'string',
  154.         'tags' => '[string]',
  155.         'summary' => 'string',
  156.         'description' => 'string',
  157.         'deprecated' => 'boolean',
  158.     ];
  159.     /**
  160.      * @inheritdoc
  161.      */
  162.     public static $_nested = [
  163.         Parameter::class => ['parameters'],
  164.         PathParameter::class => ['parameters'],
  165.         Response::class => ['responses''response'],
  166.         ExternalDocumentation::class => 'externalDocs',
  167.         Server::class => ['servers'],
  168.         RequestBody::class => 'requestBody',
  169.         Attachable::class => ['attachables'],
  170.     ];
  171.     /**
  172.      * @inheritdoc
  173.      */
  174.     #[\ReturnTypeWillChange]
  175.     public function jsonSerialize()
  176.     {
  177.         $data parent::jsonSerialize();
  178.         unset($data->method);
  179.         unset($data->path);
  180.         // ensure security elements are object
  181.         if (isset($data->security) && is_array($data->security)) {
  182.             foreach ($data->security as $key => $scheme) {
  183.                 $data->security[$key] = (object) $scheme;
  184.             }
  185.         }
  186.         return $data;
  187.     }
  188.     /**
  189.      * @inheritdoc
  190.      */
  191.     public function validate(array $stack = [], array $skip = [], string $ref ''$context null): bool
  192.     {
  193.         if (in_array($this$skiptrue)) {
  194.             return true;
  195.         }
  196.         $valid parent::validate($stack$skip$ref$context);
  197.         if (!Generator::isDefault($this->responses)) {
  198.             foreach ($this->responses as $response) {
  199.                 if (!Generator::isDefault($response->response) && $response->response !== 'default' && preg_match('/^([12345]{1}[0-9]{2})|([12345]{1}XX)$/', (string) $response->response) === 0) {
  200.                     $this->_context->logger->warning('Invalid value "' $response->response '" for ' $response->_identity([]) . '->response, expecting "default", a HTTP Status Code or HTTP Status Code range definition in ' $response->_context);
  201.                     $valid false;
  202.                 }
  203.             }
  204.         }
  205.         if (is_object($context) && !Generator::isDefault($this->operationId)) {
  206.             if (!property_exists($context'operationIds')) {
  207.                 $context->operationIds = [];
  208.             }
  209.             if (in_array($this->operationId$context->operationIds)) {
  210.                 $this->_context->logger->warning('operationId must be unique. Duplicate value found: "' $this->operationId '"');
  211.                 $valid false;
  212.             }
  213.             $context->operationIds[] = $this->operationId;
  214.         }
  215.         return $valid;
  216.     }
  217. }