|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Usox\JsonSchemaApi\Contract\ApiMethodInterface; |
|
7
|
|
|
use Usox\JsonSchemaApi\Contract\MethodProviderInterface; |
|
8
|
|
|
use Usox\JsonSchemaApi\Endpoint; |
|
9
|
|
|
|
|
10
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* API Handler which actually contains the business logic |
|
14
|
|
|
* |
|
15
|
|
|
* @implements ApiMethodInterface< |
|
16
|
|
|
* object&stdClass, |
|
17
|
|
|
* array{beer_style_list: list<string>} |
|
18
|
|
|
* > |
|
19
|
|
|
*/ |
|
20
|
|
|
final class BeerlistMethod implements ApiMethodInterface |
|
21
|
|
|
{ |
|
22
|
|
|
public function handle( |
|
23
|
|
|
ServerRequestInterface $request, |
|
24
|
|
|
stdClass $parameter |
|
25
|
|
|
): array { |
|
26
|
|
|
return [ |
|
27
|
|
|
'beer_style_list' => [ |
|
28
|
|
|
'ipa', |
|
29
|
|
|
'lager', |
|
30
|
|
|
'porter', |
|
31
|
|
|
'stout', |
|
32
|
|
|
'quadruple', |
|
33
|
|
|
] |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getSchemaFile(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return __DIR__ . '/schema/beerlist.json'; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// MethodProvider which performs the lookup for a certain method name |
|
44
|
|
|
$methodContainer = new class () implements MethodProviderInterface { |
|
45
|
|
|
/** @var array<string, ApiMethodInterface> */ |
|
46
|
|
|
private array $methods; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->methods = [ |
|
51
|
|
|
'beerlist' => new BeerlistMethod(), |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function lookup(string $methodName): ?ApiMethodInterface |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->methods[$methodName] ?? null; |
|
58
|
|
|
} |
|
59
|
|
|
}; |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
// Build a request instance |
|
63
|
|
|
$request = Laminas\Diactoros\ServerRequestFactory::fromGlobals( |
|
64
|
|
|
$_SERVER, |
|
65
|
|
|
$_GET, |
|
66
|
|
|
$_POST, |
|
67
|
|
|
$_COOKIE, |
|
68
|
|
|
$_FILES |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
// Create the endpoint and retrieve the response |
|
73
|
|
|
$response = Endpoint::factory($methodContainer)->serve($request); |
|
74
|
|
|
|
|
75
|
|
|
// Just boilerplate code. Any framework (like slim) will to that for you |
|
76
|
|
|
$statusLine = sprintf( |
|
77
|
|
|
'HTTP/%s %s %s', |
|
78
|
|
|
$response->getProtocolVersion(), |
|
79
|
|
|
$response->getStatusCode(), |
|
80
|
|
|
$response->getReasonPhrase() |
|
81
|
|
|
); |
|
82
|
|
|
header($statusLine); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($response->getHeaders() as $name => $values) { |
|
85
|
|
|
$responseHeader = sprintf( |
|
86
|
|
|
'%s: %s', |
|
87
|
|
|
$name, |
|
88
|
|
|
$response->getHeaderLine($name) |
|
89
|
|
|
); |
|
90
|
|
|
header($responseHeader); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
echo $response->getBody(); |
|
94
|
|
|
|