-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderer.php
More file actions
146 lines (120 loc) · 4.66 KB
/
Renderer.php
File metadata and controls
146 lines (120 loc) · 4.66 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Pandawa\Component\Resource;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Enumerable;
use InvalidArgumentException;
use Pandawa\Component\Resource\Exception\FormatNotAllowed;
use Pandawa\Component\Transformer\CollectionTransformer;
use Pandawa\Component\Transformer\PaginatedTransformer;
use Pandawa\Contracts\Resource\Formatter\FormatterInterface;
use Pandawa\Contracts\Resource\Formatter\FormatterResolverInterface;
use Pandawa\Contracts\Resource\RendererInterface;
use Pandawa\Contracts\Resource\Rendering;
use Pandawa\Contracts\Transformer\Context;
use Pandawa\Contracts\Transformer\TransformerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class Renderer implements RendererInterface
{
protected Pipeline $pipeline;
public function __construct(
protected readonly Container $container,
protected readonly FormatterResolverInterface $formatterResolver,
protected readonly SerializerInterface $serializer,
protected string $defaultContentType,
protected ?string $defaultWrapper = null,
protected array $middlewares = [],
protected ?array $allowedFormats = [],
) {
$this->pipeline = new Pipeline($this->container);
}
public function setDefaultWrapper(?string $defaultWrapper): void
{
$this->defaultWrapper = $defaultWrapper;
}
public function render(Context $context, mixed $result, TransformerInterface $resourceTransformer): Response {
$transformer = $this->wrapTransformer($result, $resourceTransformer);
if (null === $transformer->getWrapper()) {
$transformer->setWrapper($this->defaultWrapper);
}
return $this->format($context, $this->toArray(
$context,
$transformer->wrap($transformer->process($context, $result))
));
}
public function format(Context $context, array $data): Response
{
$formatter = $this->getFormatter($context);
return $formatter->toResponse(
$context,
$this->serialize(
$formatter->getFormat(),
$context,
$data
)
);
}
public function toArray(Context $context, mixed $data): array
{
return $this->pipeline
->send(new Rendering($data, $context))
->through($this->middlewares)
->then(fn(Rendering $rendering) => $rendering->data);
}
protected function serialize(string $format, Context $context, array $data): string
{
if (!empty($this->allowedFormats) && !in_array($format, $this->allowedFormats)) {
throw new FormatNotAllowed($format);
}
return $this->serializer->serialize($data, $format, array_get($context->options, 'serialize.context', []));
}
protected function getFormatter(Context $context): FormatterInterface
{
$request = clone $context->request;
if ($request->accepts(['text/html']) || !count($request->getAcceptableContentTypes())) {
$request->headers->set(
'Accept',
$context->options['default_content_type'] ?? $this->defaultContentType
);
}
if (null !== $formatter = $this->formatterResolver->resolve($request)) {
return $formatter;
}
throw new InvalidArgumentException(
sprintf('Unsupported format "%s".', implode(', ', $request->getAcceptableContentTypes()))
);
}
protected function wrapTransformer(mixed $result, TransformerInterface $resourceTransformer): TransformerInterface
{
if ($this->isList($result)) {
return $this->getCollectionTransformer($result, $resourceTransformer);
}
return $resourceTransformer;
}
protected function getCollectionTransformer(mixed $result, ?TransformerInterface $resourceTransformer): TransformerInterface
{
if ($result instanceof LengthAwarePaginator) {
return new PaginatedTransformer($resourceTransformer);
}
return new CollectionTransformer($resourceTransformer);
}
protected function isList(mixed $result): bool
{
if ($result instanceof LengthAwarePaginator) {
return true;
}
if ($result instanceof Enumerable) {
return true;
}
if (is_array($result) && isset($result[0]) && is_array($result[0])) {
return true;
}
return false;
}
}