1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace App\Bundle\Example\Controller; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Import classes |
7
|
|
|
*/ |
8
|
|
|
use App\ContainerAwareTrait; |
9
|
|
|
use Arus\Http\Response\ResponseFactoryAwareTrait; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Route( |
16
|
|
|
* name="api_v1_entry_list", |
17
|
|
|
* path="/api/v1/entry", |
18
|
|
|
* methods={"GET"}, |
19
|
|
|
* summary="Gets a list of entries ", |
20
|
|
|
* tags={"entry"}, |
21
|
|
|
* middlewares={ |
22
|
|
|
* "App\Middleware\RequestQueryValidationMiddleware", |
23
|
|
|
* }, |
24
|
|
|
* ) |
25
|
|
|
* |
26
|
|
|
* @OpenApi\Operation( |
27
|
|
|
* parameters={ |
28
|
|
|
* @OpenApi\Parameter( |
29
|
|
|
* in="query", |
30
|
|
|
* name="limit", |
31
|
|
|
* required=false, |
32
|
|
|
* schema=@OpenApi\Schema( |
33
|
|
|
* type="string", |
34
|
|
|
* pattern="^(?:[1-9][0-9]*)?$", |
35
|
|
|
* ), |
36
|
|
|
* ), |
37
|
|
|
* @OpenApi\Parameter( |
38
|
|
|
* in="query", |
39
|
|
|
* name="offset", |
40
|
|
|
* required=false, |
41
|
|
|
* schema=@OpenApi\Schema( |
42
|
|
|
* type="string", |
43
|
|
|
* pattern="^(?:0|[1-9][0-9]*)?$", |
44
|
|
|
* ), |
45
|
|
|
* ), |
46
|
|
|
* }, |
47
|
|
|
* responses={ |
48
|
|
|
* 200: @OpenApi\Response( |
49
|
|
|
* description="OK", |
50
|
|
|
* content={ |
51
|
|
|
* "application/json": @OpenApi\MediaType( |
52
|
|
|
* schema=@OpenApi\Schema( |
53
|
|
|
* type="object", |
54
|
|
|
* properties={ |
55
|
|
|
* "data": @OpenApi\SchemaReference( |
56
|
|
|
* class="App\Bundle\Example\Service\EntrySerializer", |
57
|
|
|
* method="serializeList", |
58
|
|
|
* ), |
59
|
|
|
* }, |
60
|
|
|
* ), |
61
|
|
|
* ), |
62
|
|
|
* }, |
63
|
|
|
* ), |
64
|
|
|
* }, |
65
|
|
|
* ) |
66
|
|
|
*/ |
67
|
|
|
final class EntryListController implements RequestHandlerInterface |
68
|
|
|
{ |
69
|
|
|
use ContainerAwareTrait; |
70
|
|
|
use ResponseFactoryAwareTrait; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
* |
75
|
|
|
* @param ServerRequestInterface $request |
76
|
|
|
* |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
2 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
80
|
|
|
{ |
81
|
2 |
|
$q = $request->getQueryParams(); |
82
|
|
|
|
83
|
2 |
|
$limit = 10; |
84
|
2 |
|
$offset = 0; |
85
|
|
|
|
86
|
2 |
|
if (isset($q['limit'])) { |
87
|
1 |
|
$limit = (int) $q['limit']; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if (isset($q['offset'])) { |
91
|
1 |
|
$offset = (int) $q['offset']; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$entries = $this->container->get('entryManager')->getList($limit, $offset); |
95
|
2 |
|
$data = $this->container->get('entrySerializer')->serializeList(...$entries); |
96
|
|
|
|
97
|
2 |
|
return $this->ok($data); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|