-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExamplesController.php
177 lines (161 loc) · 5.82 KB
/
ExamplesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Dto\QueryDto;
use App\Dto\RequestBodyDto;
use App\Dto\Response;
use App\Form\ModellessForm;
use SwaggerBake\Lib\Attribute\OpenApiDto;
use SwaggerBake\Lib\Attribute\OpenApiForm;
use SwaggerBake\Lib\Attribute\OpenApiHeader;
use SwaggerBake\Lib\Attribute\OpenApiQueryParam;
use SwaggerBake\Lib\Attribute\OpenApiResponse;
use SwaggerBake\Lib\Attribute\OpenApiSecurity;
/**
* Examples Controller
*
* An assortment of examples
*
* @property \App\Model\Table\ActorsTable $Actors
* @method \App\Model\Entity\Actor[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class ExamplesController extends AppController
{
public function initialize() : void
{
parent::initialize();
$this->loadComponent('Authentication.Authentication');
$this->Authentication->allowUnauthenticated([
'formExample',
'queryExample',
'dtoQueryExample',
'dtoBodyExample',
'headerExample',
'optionsOrHead',
'index',
'modellessForm',
]);
}
/**
* Index
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
public function index()
{
$this->request->allowMethod('get');
return $this->getResponse()->withType('text/plain')->withStringBody('hello world');
}
/**
* Form Example
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
#[OpenApiForm(name: "my_input", description: "a custom input", isRequired: true)]
public function formExample()
{
$this->request->allowMethod('post');
$data = $this->getRequest()->getData('my_input');
$this->set(compact('data'));
$this->viewBuilder()->setOption('serialize', 'data');
}
/**
* Query Example
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
#[OpenApiQueryParam(name: "my_enum", description: "a description")]
#[OpenApiQueryParam(name: "format_datetime", format: "date-time")]
#[OpenApiQueryParam(name: "explode", explode: true)]
#[OpenApiQueryParam(name: "deprecated", isDeprecated: true, allowEmptyValue: true)]
public function queryExample()
{
$this->request->allowMethod('get');
$data = 'just an example for OpenApiQueryParam';
$this->set(compact('data'));
$this->viewBuilder()->setOption('serialize', 'data');
}
/**
* DTO Body Example
*
* This is an example of OpenApiDto and OpenApiResponse. This takes RequestBodyDto and returns it.
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
#[OpenApiDto(class: RequestBodyDto::class)]
#[OpenApiResponse(schema: Response::class)]
public function dtoBodyExample()
{
$this->request->allowMethod('post');
$dto = RequestBodyDto::createFromRequest($this->request);
$response = new Response($dto->getLastName(), $dto->getFirstName());
$this->set(compact('response'));
$this->viewBuilder()->setOption('serialize', 'response');
}
/**
* DTO Query Example
*
* This is an example of OpenApiDto and OpenApiResponse. This takes QueryDto and returns it as a list.
*
* @see https://github.com/spatie/data-transfer-object
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
#[OpenApiDto(class: QueryDto::class)]
#[OpenApiResponse(schemaType: 'array', schema: Response::class)]
public function dtoQueryExample()
{
$this->request->allowMethod('get');
$dto = QueryDto::createFromRequest($this->request);
$response = [new Response($dto->getLastName(), $dto->getFirstName())];
$this->set(compact('response'));
$this->viewBuilder()->setOption('serialize', 'response');
}
/**
* Header Example
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException
*/
#[OpenApiHeader(name: "my_header", description: "a custom header", isRequired: true)]
#[OpenApiHeader(ref: "#/x-demo/components/parameters/anotherHeader")]
public function headerExample()
{
$this->request->allowMethod('get');
$data = $this->getRequest()->getHeader('my_header');
$this->set(compact('data'));
$this->viewBuilder()->setOption('serialize', 'data');
}
/**
* View (Security Example)
*/
#[OpenApiSecurity(name: 'ApiKey')]
public function apiKeyExample()
{
$this->request->allowMethod('get');
$data = $this->getRequest()->getHeader('ApiKey');
$this->set(compact('data'));
$this->viewBuilder()->setOption('serialize', 'data');
}
public function optionsOrHead()
{
$this->request->allowMethod(['options','head']);
if ($this->request->is('options')) {
return $this->response->withHeader('Allow', 'OPTIONS,, HEAD')->withStatus(200);
}
return $this->response->withStatus(200);
}
#[OpenApiDto(class: ModellessForm::class)]
public function modellessForm()
{
$form = new ModellessForm();
if ($form->execute($this->request->getData())) {
return $this->response->withStatus(200);
}
return $this->response->withStatus(400);
}
}