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