1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HTTP Request & Response Body mapper |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\Transformation; |
7
|
|
|
|
8
|
|
|
use Graviton\ProxyBundle\Service\MappingTransformer; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class transforms the body of HTTP requests and responses. |
14
|
|
|
* |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class BodyMapping implements ResponseTransformationInterface, RequestTransformationInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $mapping = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MappingTransformer |
29
|
|
|
*/ |
30
|
|
|
private $transformer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param MappingTransformer $transformer The MappingTransformer to be used for the transformations |
36
|
|
|
*/ |
37
|
|
|
public function __construct(MappingTransformer $transformer) |
38
|
|
|
{ |
39
|
|
|
$this->transformer = $transformer; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Transforms a response |
44
|
|
|
* |
45
|
|
|
* @param Response $responseIn The original response object |
46
|
|
|
* @param Response $responseOut The response object to transform |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function transformResponse(Response $responseIn, Response $responseOut) |
51
|
|
|
{ |
52
|
|
|
$responseOut->setContent( |
53
|
|
|
json_encode($this->transformer->transform($responseIn->getContent(), $this->mapping)) |
|
|
|
|
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Transforms a request |
59
|
|
|
* |
60
|
|
|
* @param Request $requestIn The original request object |
61
|
|
|
* @param Request $requestOut The request object to transform |
62
|
|
|
* @return Request The transformed request |
63
|
|
|
*/ |
64
|
|
|
public function transformRequest(Request $requestIn, Request $requestOut) |
65
|
|
|
{ |
66
|
|
|
$requestOut = Request::create( |
67
|
|
|
$requestOut->getUri(), |
68
|
|
|
$requestOut->getMethod(), |
69
|
|
|
[], |
70
|
|
|
[], |
71
|
|
|
[], |
72
|
|
|
[], |
73
|
|
|
json_encode( |
74
|
|
|
$this->transformer->transform(json_decode($requestIn->getContent()), $this->mapping) |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
return $requestOut; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sets the mapping |
82
|
|
|
* |
83
|
|
|
* Structure: $transformedPropertyPath => $originalPropertyPath |
84
|
|
|
* |
85
|
|
|
* @see http://symfony.com/doc/current/components/property_access/introduction.html |
86
|
|
|
* |
87
|
|
|
* @param array $mapping The mapping |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function setMapping(array $mapping) |
92
|
|
|
{ |
93
|
|
|
$this->mapping = $mapping; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: