-
Notifications
You must be signed in to change notification settings - Fork 554
/
RenderData.php
65 lines (58 loc) · 1.78 KB
/
RenderData.php
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
<?php
namespace A17\Twill\Services\Blocks;
use A17\Twill\Models\Block;
use A17\Twill\Models\Contracts\TwillModelContract;
/**
* DTO for rendering a Block.
*
* $model is undefined when in a block editor context.
* @todo: I guess we can add this by passing the model via the editor preview request? Not blocking.
*/
class RenderData
{
public function __construct(
public Block $block,
public string $editorName,
public array $children,
public ?TwillModelContract $model = null,
public ?string $parentEditorName = null,
public bool $inEditor = false,
) {
}
public function renderChildren(
string $editorName,
array $viewMapping = [],
array $data = [],
): string {
$output = [];
/** @var \A17\Twill\Services\Blocks\Block $child */
foreach ($this->children as $child) {
if ($child->renderData->parentEditorName === $editorName) {
$output[] = $child->renderView(
$viewMapping,
$data + ['inEditor' => $this->inEditor],
$this->inEditor
);
}
}
return implode('', $output);
}
public function getChildrenFor(
string $editorName,
array $viewMapping = [],
array $data = [],
): array {
$output = [];
/** @var \A17\Twill\Services\Blocks\Block $child */
foreach ($this->children as $child) {
if ($child->renderData->parentEditorName === $editorName) {
$output[] = $child->renderView(
$viewMapping,
$data + ['inEditor' => $this->inEditor],
$this->inEditor
);
}
}
return $output;
}
}