|
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 JsonSerializable; |
|
14
|
|
|
use Spatie\Sheets\Facades\Sheets; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
class Page implements Htmlable, Renderable, Responsable, Arrayable, Jsonable, JsonSerializable |
|
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 ViewFactory */ |
|
29
|
|
|
protected $viewFactory; |
|
30
|
|
|
|
|
31
|
12 |
|
public function __construct(ViewFactory $viewFactory, array $data = [], ?string $page = null) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
12 |
|
$this->viewFactory = $viewFactory; |
|
34
|
12 |
|
$this->data($data); |
|
35
|
12 |
|
$this->page($page); |
|
36
|
10 |
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
public static function make(array $data = [], ?string $page = null): self |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
11 |
|
return app(static::class, [ |
|
|
|
|
|
|
41
|
11 |
|
'data' => $data, |
|
42
|
11 |
|
'page' => $page, |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
public static function makeFromSheet(string $collection, string $name, ?string $page = null): self |
|
47
|
|
|
{ |
|
48
|
7 |
|
$sheet = Sheets::collection($collection)->get($name); |
|
49
|
|
|
|
|
50
|
7 |
|
if ($sheet === null) { |
|
51
|
1 |
|
throw new Exception(sprintf('No sheet found in collection [%s] with name [%s].', $collection, $name)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
return static::make($sheet->toArray(), $page); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
12 |
|
public function page(?string $page): self |
|
58
|
|
|
{ |
|
59
|
12 |
|
if (is_string($page)) { |
|
60
|
7 |
|
if (! is_subclass_of($page, PageData::class, true)) { |
|
61
|
2 |
|
throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class)); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
10 |
|
$this->page = $page; |
|
66
|
|
|
|
|
67
|
10 |
|
$this->parse(); |
|
68
|
|
|
|
|
69
|
10 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
12 |
|
public function data(array $data): self |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
12 |
|
$this->data = $data; |
|
75
|
|
|
|
|
76
|
12 |
|
$this->parse(); |
|
77
|
|
|
|
|
78
|
12 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function view(string $view): self |
|
82
|
|
|
{ |
|
83
|
2 |
|
$this->view = $view; |
|
84
|
|
|
|
|
85
|
2 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
public function render(): View |
|
89
|
|
|
{ |
|
90
|
3 |
|
if (empty($this->view)) { |
|
91
|
1 |
|
throw new Exception('You have to define a view before the page can render.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $this->viewFactory->make($this->view, $this->data); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public function toHtml(): string |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->render()->render(); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
5 |
|
public function toArray(): array |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
5 |
|
return is_array($this->data) ? $this->data : $this->data->toArray(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
public function jsonSerialize(): array |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->toArray(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function toJson($options = 0): string |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
1 |
|
return json_encode($this->jsonSerialize(), $options); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
3 |
|
public function toResponse($request): Response |
|
121
|
|
|
{ |
|
122
|
3 |
|
if ($request->wantsJson()) { |
|
123
|
1 |
|
return response()->json($this->jsonSerialize()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
return response($this->render()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
12 |
|
protected function parse(): void |
|
130
|
|
|
{ |
|
131
|
12 |
|
if ($this->page === null) { |
|
132
|
12 |
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
5 |
|
$this->data = forward_static_call( |
|
136
|
5 |
|
[$this->page, 'make'], |
|
137
|
5 |
|
$this->toArray() |
|
138
|
|
|
); |
|
139
|
4 |
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|