|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/relay-transform/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/relay-transform |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Flipbox\Relay\Middleware\Transform; |
|
10
|
|
|
|
|
11
|
|
|
use Flipbox\Http\Stream\Factory as StreamFactory; |
|
12
|
|
|
use Flipbox\Relay\Middleware\AbstractMiddleware; |
|
13
|
|
|
use Flipbox\Skeleton\Helpers\JsonHelper; |
|
14
|
|
|
use Flipbox\Transform\Factory as TransformFactory; |
|
15
|
|
|
use Flipbox\Transform\Helpers\Transformer as TransformerHelper; |
|
16
|
|
|
use Flipbox\Transform\transformers\TransformerInterface; |
|
17
|
|
|
use Psr\Http\Message\RequestInterface; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class AbstractResource extends AbstractMiddleware |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
public $data; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|callable|TransformerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
public $transformer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
public $config = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __invoke( |
|
45
|
|
|
RequestInterface $request, |
|
46
|
|
|
ResponseInterface $response, |
|
47
|
|
|
callable $next = null |
|
48
|
|
|
) { |
|
49
|
|
|
// Set transformed body contents |
|
50
|
|
|
$request = $request->withBody( |
|
51
|
|
|
StreamFactory::create( |
|
52
|
|
|
JsonHelper::encode($this->transform()) |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$response = $next($request, $response); |
|
57
|
|
|
|
|
58
|
|
|
return $this->handleResponse($response); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array|null |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function transform() |
|
65
|
|
|
{ |
|
66
|
|
|
return TransformFactory::item($this->config) |
|
67
|
|
|
->transform( |
|
68
|
|
|
$this->resolveTransformer(), |
|
69
|
|
|
$this->data |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return callable|TransformerInterface |
|
75
|
|
|
* @throws \Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function resolveTransformer() |
|
78
|
|
|
{ |
|
79
|
|
|
if (TransformerHelper::isTransformer($this->transformer)) { |
|
80
|
|
|
return $this->transformer; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (TransformerHelper::isTransformerClass($this->transformer)) { |
|
84
|
|
|
return new $this->transformer(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new \Exception("Invalid transformer"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param ResponseInterface $response |
|
92
|
|
|
* @return ResponseInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function handleResponse(ResponseInterface $response) |
|
95
|
|
|
{ |
|
96
|
|
|
return $response; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|