|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Astrotomic\Stancy\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
8
|
|
|
use Illuminate\Contracts\Support\Responsable; |
|
9
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
|
10
|
|
|
use Illuminate\Contracts\View\View; |
|
11
|
|
|
use Spatie\Sheets\Facades\Sheets; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
|
|
14
|
|
|
class Page implements Htmlable, Renderable, Responsable |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string|null */ |
|
17
|
|
|
protected $view; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string|null */ |
|
20
|
|
|
protected $page; |
|
21
|
|
|
|
|
22
|
|
|
/** @var PageData|array */ |
|
|
|
|
|
|
23
|
|
|
protected $data = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ViewFactory */ |
|
26
|
|
|
protected $viewFactory; |
|
27
|
|
|
|
|
28
|
10 |
|
public function __construct(ViewFactory $viewFactory, array $data = [], ?string $page = null) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
10 |
|
$this->viewFactory = $viewFactory; |
|
31
|
10 |
|
$this->data($data); |
|
32
|
10 |
|
$this->page($page); |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
9 |
|
public static function make(array $data = [], ?string $page = null): self |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
9 |
|
return app(static::class, [ |
|
|
|
|
|
|
38
|
9 |
|
'data' => $data, |
|
39
|
9 |
|
'page' => $page, |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
public static function makeFromSheet(string $collection, string $name, ?string $page = null): self |
|
44
|
|
|
{ |
|
45
|
5 |
|
$sheet = Sheets::collection($collection)->get($name); |
|
46
|
|
|
|
|
47
|
5 |
|
if ($sheet === null) { |
|
48
|
1 |
|
throw new Exception(sprintf('No sheet found in collection [%s] with name [%s].', $collection, $name)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
return static::make($sheet->toArray(), $page); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
10 |
|
public function page(?string $page): self |
|
55
|
|
|
{ |
|
56
|
10 |
|
if (is_string($page)) { |
|
57
|
5 |
|
if (! is_subclass_of($page, PageData::class, true)) { |
|
58
|
2 |
|
throw new Exception(sprintf('The page data class [%s] has to extend %s.', $page, PageData::class)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
8 |
|
$this->page = $page; |
|
63
|
|
|
|
|
64
|
8 |
|
$this->parse(); |
|
65
|
|
|
|
|
66
|
8 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
10 |
|
public function data(array $data): self |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
10 |
|
$this->data = $data; |
|
72
|
|
|
|
|
73
|
10 |
|
$this->parse(); |
|
74
|
|
|
|
|
75
|
10 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public function view(string $view): self |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->view = $view; |
|
81
|
|
|
|
|
82
|
2 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function render(): View |
|
86
|
|
|
{ |
|
87
|
3 |
|
if (empty($this->view)) { |
|
88
|
1 |
|
throw new Exception('You have to define a view before the page can render.'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
return $this->viewFactory->make($this->view, $this->data); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
public function toHtml(): string |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->render()->render(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritDoc |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
3 |
|
public function toResponse($request): Response |
|
103
|
|
|
{ |
|
104
|
3 |
|
if ($request->wantsJson()) { |
|
105
|
1 |
|
return response()->json($this->data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
return response($this->render()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
10 |
|
protected function parse(): void |
|
112
|
|
|
{ |
|
113
|
10 |
|
if ($this->page === null) { |
|
114
|
10 |
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
$this->data = forward_static_call( |
|
118
|
3 |
|
[$this->page, 'make'], |
|
119
|
3 |
|
is_array($this->data) ? $this->data : $this->data->toArray() |
|
120
|
|
|
); |
|
121
|
2 |
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|