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

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