Passed
Push — master ( 641df3...c95e72 )
by Caen
03:59 queued 15s
created

RenderData::getRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
use JetBrains\PhpStorm\Deprecated;
12
13
/**
14
 * Contains data for the current page being rendered/compiled.
15
 *
16
 * All public data here will be available in the Blade views through {@see ManagesViewData::shareViewData()}.
17
 *
18
 * @see \Hyde\Support\Facades\Render
19
 * @see \Hyde\Framework\Testing\Feature\RenderHelperTest
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
    /**
42
     * @deprecated v1.0.0-RC.2 - Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.
43
     * @codeCoverageIgnore
44
     */
45
    #[Deprecated(reason: 'Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.', replacement: '%class%->getRoute()')]
46
    public function getCurrentRoute(): ?Route
47
    {
48
        return $this->getRoute();
49
    }
50
51
    public function getRoute(): ?Route
52
    {
53
        return $this->route ?? null;
54
    }
55
56
    /**
57
     * @deprecated v1.0.0-RC.2 - Renamed to getRouteKey() to match renamed property. This method will be removed before version 1.0.
58
     * @codeCoverageIgnore
59
     */
60
    #[Deprecated(reason: 'Renamed to getRoute() to match renamed property. This method will be removed before version 1.0.', replacement: '%class%->getRouteKey()')]
61
    public function getCurrentPage(): ?string
62
    {
63
        return $this->getRouteKey();
64
    }
65
66
    public function getRouteKey(): ?string
67
    {
68
        return $this->routeKey ?? null;
69
    }
70
71
    public function shareToView(): void
72
    {
73
        View::share($this->toArray());
74
    }
75
76
    public function share(string $key, mixed $value): void
77
    {
78
        if (property_exists($this, $key)) {
79
            $this->{$key} = $value;
80
            $this->shareToView();
81
        } else {
82
            throw new InvalidArgumentException("Property '$key' does not exist on ".self::class);
83
        }
84
    }
85
86
    public function clearData(): void
87
    {
88
        unset($this->page, $this->route, $this->routeKey);
89
        View::share(['page' => null, 'route' => null, 'routeKey' => null]);
90
    }
91
92
    /**
93
     * @return array{render: $this, page: \Hyde\Pages\Concerns\HydePage|null, currentRoute: \Hyde\Support\Models\Route|null, currentPage: string|null}
94
     */
95
    public function toArray(): array
96
    {
97
        return [
98
            'render' => $this,
99
            'page' => $this->getPage(),
100
            'route' => $this->getRoute(),
101
            'routeKey' => $this->getRouteKey(),
102
        ];
103
    }
104
}
105