|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Astrotomic\Stancy\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
|
8
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
9
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
10
|
|
|
use Illuminate\Contracts\Support\Responsable; |
|
11
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
|
12
|
|
|
use Illuminate\Contracts\View\View; |
|
13
|
|
|
use Illuminate\Support\Arr; |
|
14
|
|
|
use JsonSerializable; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use Spatie\Feed\Feedable; |
|
17
|
|
|
use Spatie\Feed\FeedItem; |
|
18
|
|
|
use Spatie\Sheets\Facades\Sheets; |
|
19
|
|
|
use Spatie\Sheets\Sheet; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
|
|
22
|
|
|
class Page implements Htmlable, Renderable, Responsable, Arrayable, Jsonable, JsonSerializable, Feedable |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string|null */ |
|
25
|
|
|
protected $view; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string|null */ |
|
28
|
|
|
protected $page; |
|
29
|
|
|
|
|
30
|
|
|
/** @var PageData|array */ |
|
|
|
|
|
|
31
|
|
|
protected $data = []; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ViewFactory */ |
|
34
|
|
|
protected $viewFactory; |
|
35
|
|
|
|
|
36
|
18 |
|
public function __construct(ViewFactory $viewFactory, array $data = [], ?string $page = null) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
18 |
|
$this->viewFactory = $viewFactory; |
|
39
|
|
|
|
|
40
|
18 |
|
$this->view($data['_view'] ?? null); |
|
41
|
|
|
|
|
42
|
18 |
|
$page = $page ?? $data['_pageData'] ?? null; |
|
43
|
|
|
|
|
44
|
18 |
|
$data = array_merge($data, $this->getSheetData($data['_sheets'] ?? [])); |
|
45
|
|
|
|
|
46
|
17 |
|
$this->data($data); |
|
47
|
17 |
|
$this->page($page); |
|
48
|
14 |
|
} |
|
49
|
|
|
|
|
50
|
17 |
|
public function page(?string $page): self |
|
51
|
|
|
{ |
|
52
|
17 |
|
if (is_string($page)) { |
|
53
|
11 |
|
if (! is_subclass_of($page, PageData::class, true)) { |
|
54
|
2 |
|
throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class)); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
15 |
|
$this->page = $page; |
|
59
|
|
|
|
|
60
|
15 |
|
$this->parse(); |
|
61
|
|
|
|
|
62
|
14 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
17 |
|
public function data(array $data): self |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
17 |
|
$this->data = $this->prepareData($data); |
|
68
|
|
|
|
|
69
|
17 |
|
$this->parse(); |
|
70
|
|
|
|
|
71
|
17 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
18 |
|
public function view(?string $view): self |
|
75
|
|
|
{ |
|
76
|
18 |
|
$this->view = $view; |
|
77
|
|
|
|
|
78
|
18 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
public function render(): View |
|
82
|
|
|
{ |
|
83
|
4 |
|
if (empty($this->view)) { |
|
84
|
1 |
|
throw new Exception('You have to define a view before the page can render.'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
return $this->viewFactory->make($this->view, $this->data); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
public function toHtml(): string |
|
91
|
|
|
{ |
|
92
|
3 |
|
return $this->render()->render(); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
9 |
|
public function toArray(): array |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
9 |
|
return is_array($this->data) ? $this->data : $this->data->toArray(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function jsonSerialize(): array |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
2 |
|
return $this->toArray(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** {@inheritdoc} */ |
|
106
|
1 |
|
public function toJson($options = 0): string |
|
107
|
|
|
{ |
|
108
|
1 |
|
return json_encode($this->jsonSerialize(), $options); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** {@inheritdoc} */ |
|
112
|
4 |
|
public function toResponse($request): Response |
|
113
|
|
|
{ |
|
114
|
4 |
|
if ($request->wantsJson()) { |
|
115
|
1 |
|
return response()->json($this->jsonSerialize()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
return response($this->render()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
public function toFeedItem(): FeedItem |
|
122
|
|
|
{ |
|
123
|
2 |
|
if (! ($this->data instanceof PageData)) { |
|
124
|
1 |
|
throw new Exception(sprintf('The page data has to extend %s to allow transformation to %s.', PageData::class, FeedItem::class)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return $this->data->toFeedItem(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
17 |
|
protected function parse(): void |
|
131
|
|
|
{ |
|
132
|
17 |
|
if ($this->page === null) { |
|
133
|
17 |
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
9 |
|
$this->data = forward_static_call( |
|
137
|
9 |
|
[$this->page, 'make'], |
|
138
|
9 |
|
$this->toArray() |
|
139
|
|
|
); |
|
140
|
7 |
|
} |
|
141
|
|
|
|
|
142
|
18 |
|
protected function getSheetData(array $sheets): array |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
18 |
|
if (empty($sheets)) { |
|
145
|
16 |
|
return []; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
if (! Arr::isAssoc($sheets)) { |
|
149
|
1 |
|
throw new RuntimeException('The [_sheets] data has to be an associative array.'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
$data = []; |
|
153
|
|
|
|
|
154
|
1 |
|
foreach ($sheets as $key => $sheet) { |
|
155
|
1 |
|
[$collection, $path] = explode(':', $sheet); |
|
156
|
|
|
|
|
157
|
1 |
|
if ($path === '*') { |
|
158
|
|
|
$data[$key] = Sheets::collection($collection)->all()->map(function (Sheet $sheet) { |
|
159
|
1 |
|
return $this->prepareData($sheet->toArray()); |
|
160
|
1 |
|
})->all(); |
|
161
|
|
|
|
|
162
|
1 |
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
$data[$key] = $this->prepareData(Sheets::collection($collection)->get($path)->toArray()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
return $data; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
17 |
|
protected function prepareData(array $data): array |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
17 |
|
unset($data['_view'], $data['_pageData'], $data['_sheets']); |
|
174
|
|
|
|
|
175
|
17 |
|
return $data; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|