-
-
Notifications
You must be signed in to change notification settings - Fork 962
Expand file tree
/
Copy pathWithResponseHeaderParameter.php
More file actions
78 lines (68 loc) · 2.67 KB
/
WithResponseHeaderParameter.php
File metadata and controls
78 lines (68 loc) · 2.67 KB
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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\ResponseHeaderParameter;
#[Get(
uriTemplate: 'with_response_headers/{id}',
parameters: [
'RateLimit-Limit' => new ResponseHeaderParameter(schema: ['type' => 'integer'], description: 'Maximum number of requests per window', provider: [self::class, 'provideRateLimitHeaders']),
'RateLimit-Remaining' => new ResponseHeaderParameter(schema: ['type' => 'integer'], description: 'Remaining requests in current window', provider: [self::class, 'provideRateLimitHeaders']),
],
provider: [self::class, 'provide'],
)]
#[Post(
uriTemplate: 'with_response_headers',
parameters: [
'RateLimit-Limit' => new ResponseHeaderParameter(schema: ['type' => 'integer'], description: 'Maximum number of requests per window'),
'RateLimit-Remaining' => new ResponseHeaderParameter(schema: ['type' => 'integer'], description: 'Remaining requests in current window'),
],
provider: [self::class, 'provide'],
processor: [self::class, 'process'],
)]
class WithResponseHeaderParameter
{
public function __construct(public readonly string $id = '1', public readonly string $name = 'hello')
{
}
public static function provide(Operation $operation, array $uriVariables = []): self
{
return new self($uriVariables['id'] ?? '1');
}
public static function provideRateLimitHeaders(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
{
if ('RateLimit-Limit' === $parameter->getKey()) {
$parameter->setValue(100);
}
if ('RateLimit-Remaining' === $parameter->getKey()) {
$parameter->setValue(99);
}
return $context['operation'] ?? null;
}
/**
* @param array<string, mixed> $context
*/
public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
foreach ($operation->getParameters() ?? [] as $parameter) {
if ('RateLimit-Limit' === $parameter->getKey()) {
$parameter->setValue(50);
}
if ('RateLimit-Remaining' === $parameter->getKey()) {
$parameter->setValue(49);
}
}
return $data;
}
}