Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
RenderData | |
100.00% |
20 / 20 |
|
100.00% |
8 / 8 |
9 | |
100.00% |
1 / 1 |
setPage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRoute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRouteKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
shareToView | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
share | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
clearData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Support\Models; |
6 | |
7 | use Hyde\Pages\Concerns\HydePage; |
8 | use Illuminate\Contracts\Support\Arrayable; |
9 | use Illuminate\Support\Facades\View; |
10 | use InvalidArgumentException; |
11 | |
12 | use function property_exists; |
13 | |
14 | /** |
15 | * Contains data for the current page being rendered/compiled. |
16 | * |
17 | * All public data here will be available in the Blade views through {@see ManagesViewData::shareViewData()}. |
18 | * |
19 | * @see \Hyde\Support\Facades\Render |
20 | */ |
21 | class RenderData implements Arrayable |
22 | { |
23 | protected HydePage $page; |
24 | protected Route $route; |
25 | protected string $routeKey; |
26 | |
27 | public function setPage(HydePage $page): void |
28 | { |
29 | $this->page = $page; |
30 | $this->route = $page->getRoute(); |
31 | $this->routeKey = $page->getRouteKey(); |
32 | |
33 | $this->shareToView(); |
34 | } |
35 | |
36 | public function getPage(): ?HydePage |
37 | { |
38 | return $this->page ?? null; |
39 | } |
40 | |
41 | public function getRoute(): ?Route |
42 | { |
43 | return $this->route ?? null; |
44 | } |
45 | |
46 | public function getRouteKey(): ?string |
47 | { |
48 | return $this->routeKey ?? null; |
49 | } |
50 | |
51 | public function shareToView(): void |
52 | { |
53 | View::share($this->toArray()); |
54 | } |
55 | |
56 | public function share(string $key, HydePage|Route|string $value): void |
57 | { |
58 | if (property_exists($this, $key)) { |
59 | $this->{$key} = $value; |
60 | $this->shareToView(); |
61 | } else { |
62 | throw new InvalidArgumentException("Property '$key' does not exist on ".self::class); |
63 | } |
64 | } |
65 | |
66 | public function clearData(): void |
67 | { |
68 | unset($this->page, $this->route, $this->routeKey); |
69 | View::share(['page' => null, 'route' => null, 'routeKey' => null]); |
70 | } |
71 | |
72 | /** |
73 | * @return array{render: \Hyde\Support\Models\RenderData, page: \Hyde\Pages\Concerns\HydePage|null, route: \Hyde\Support\Models\Route|null, routeKey: string|null} |
74 | */ |
75 | public function toArray(): array |
76 | { |
77 | return [ |
78 | 'render' => $this, |
79 | 'page' => $this->getPage(), |
80 | 'route' => $this->getRoute(), |
81 | 'routeKey' => $this->getRouteKey(), |
82 | ]; |
83 | } |
84 | } |