-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathSlot.php
More file actions
81 lines (65 loc) · 1.95 KB
/
Slot.php
File metadata and controls
81 lines (65 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace Tempest\View;
use Tempest\Reflection\ClassReflector;
use Tempest\Support\Arr\ImmutableArray;
use Tempest\View\Export\ExportableViewObject;
use Tempest\View\Parser\Token;
final class Slot implements ExportableViewObject
{
public const string DEFAULT = 'default';
public function __construct(
public string $name,
public array $attributes,
string $content,
) {
$this->content = base64_encode($content);
}
public string $content {
get => ($d = base64_decode($this->content, strict: true)) === false ? '' : $d;
}
public ImmutableArray $exportData {
get => new ImmutableArray([
'name' => $this->name,
'attributes' => $this->attributes,
'content' => base64_encode($this->content),
]);
}
public static function restore(mixed ...$data): ExportableViewObject
{
$self = new ClassReflector(self::class)->newInstanceWithoutConstructor();
$self->name = $data['name'];
$self->attributes = $data['attributes'];
$self->content = $data['content'];
return $self;
}
public function __get(string $name): mixed
{
return $this->attributes[$name] ?? null;
}
public static function named(Token $token): self
{
$name = $token->getAttribute('name');
$attributes = $token->htmlAttributes;
$content = $token->compileChildren();
return new self(
name: $name,
attributes: $attributes,
content: $content,
);
}
public static function default(Token ...$tokens): self
{
$name = Slot::DEFAULT;
$attributes = [];
$content = '';
foreach ($tokens as $token) {
$content .= $token->compile();
}
return new self(
name: $name,
attributes: $attributes,
content: $content,
);
}
}