This repository was archived by the owner on Apr 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRequestSerializationHandler.php
More file actions
71 lines (59 loc) · 1.8 KB
/
RequestSerializationHandler.php
File metadata and controls
71 lines (59 loc) · 1.8 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
<?php
/**
* This file is part of the Elastic OpenAPI PHP code generator.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Elastic\OpenApi\Codegen\Connection\Handler;
use Elastic\OpenApi\Codegen\Serializer\SerializerInterface;
use GuzzleHttp\Ring\Core as GuzzleCore;
/**
* Automatatic unserialization of the response.
*
* @package Elastic\OpenApi\Codegen\Connection\Handler
* @author Aurélien FOUCRET <aurelien.foucret@elastic.co>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
*/
class RequestSerializationHandler
{
/**
* @var callable
*/
private $handler;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var GuzzleCore
*/
private $ringUtils;
/**
* Constructor.
*
* @param callable $handler original handler
* @param SerializerInterface $serializer serialize
*/
public function __construct(callable $handler, SerializerInterface $serializer)
{
$this->handler = $handler;
$this->serializer = $serializer;
$this->ringUtils = new GuzzleCore();
}
public function __invoke($request)
{
$handler = $this->handler;
$request = $this->ringUtils->setHeader($request, 'Content-Type', ['application/json']);
$body = isset($request['body']) ? $request['body'] : [];
if (isset($request['query_params'])) {
$body = array_merge($body, $request['query_params']);
unset($request['query_params']);
}
if (!empty($body) && 'GET' !== $request['http_method']) {
ksort($body);
$request['body'] = $this->serializer->serialize($body);
}
return $handler($request);
}
}