Skip to content

Commit 67f4f95

Browse files
committed
GridComponent: no deep, refactor, fluent
1 parent 8ca82f1 commit 67f4f95

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

src/UI/Component/GridComponent.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ final class GridComponent extends Control
2424
*/
2525
private ?Html $column = null;
2626

27+
private ?string $incrementSequencePrefix = null;
28+
2729
private int $columnNumber = 1;
2830

2931
public function __construct(Component $component)
@@ -42,14 +44,25 @@ public function setRow(?Html $row): void
4244
/**
4345
* @phpstan-param Html<int, Html|string>|null $column
4446
*/
45-
public function setColumn(?Html $column): void
47+
public function setColumn(?Html $column): self
4648
{
4749
$this->column = $column;
50+
51+
return $this;
4852
}
4953

50-
public function setColumnNumber(int $columnNumber): void
54+
public function setColumnNumber(int $columnNumber): self
5155
{
5256
$this->columnNumber = max(1, $columnNumber);
57+
58+
return $this;
59+
}
60+
61+
public function setIncrementSequence(string $prefix): self
62+
{
63+
$this->incrementSequencePrefix = $prefix;
64+
65+
return $this;
5366
}
5467

5568
public function render(): void
@@ -60,17 +73,54 @@ public function render(): void
6073
assert($component instanceof Component);
6174
assert($template instanceof Template);
6275

63-
$controls = iterator_to_array($component->getComponents(true, IRenderable::class));
76+
$controls = iterator_to_array($component->getComponents(false, IRenderable::class));
6477

6578
if (!$controls) {
6679
return;
6780
}
6881

82+
// column functions
83+
$template->getLatte()->addFunction('startColumn', function (int $counter): ?string {
84+
if (!$this->column) {
85+
return null;
86+
}
87+
88+
$col = $this->column;
89+
if ($this->incrementSequencePrefix) {
90+
$col = clone $this->column;
91+
$col->appendAttribute('class', $this->incrementSequencePrefix . $counter);
92+
}
93+
94+
return $col->startTag();
95+
});
96+
$template->getLatte()->addFunction('endColumn', function (): ?string {
97+
if (!$this->column) {
98+
return null;
99+
}
100+
101+
return $this->column->endTag();
102+
});
103+
104+
// row functions
105+
$template->getLatte()->addFunction('startRow', function (int $counter): ?string {
106+
if (!$this->row) {
107+
return null;
108+
}
109+
110+
return $this->row->startTag();
111+
});
112+
$template->getLatte()->addFunction('endRow', function (): ?string {
113+
if (!$this->row) {
114+
return null;
115+
}
116+
117+
return $this->row->endTag();
118+
});
119+
120+
69121
$template->render(__DIR__ . '/templates/grid.latte', [
70122
'controls' => $controls,
71123
'calculate' => (bool) $this->row,
72-
'row' => $this->row,
73-
'column' => $this->column,
74124
'number' => $this->columnNumber,
75125
]);
76126
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
{var $startRow = $row ? $row->startTag()}
2-
{var $endRow = $row ? $row->endTag()}
3-
4-
{var $startColumn = $column ? $column->startTag()}
5-
{var $endColumn = $column ? $column->endTag()}
61

72
{foreach $controls as $ctrl}
83
{if $calculate && $iterator->isFirst()}
9-
{$startRow|noescape}
4+
{startRow()|noescape}
105
{/if}
116

12-
{$startColumn|noescape}
7+
{startColumn($iterator->getCounter())|noescape}
138
{control $ctrl}
14-
{$endColumn|noescape}
9+
{endColumn()|noescape}
1510

1611
{if $calculate}
1712
{if $iterator->isLast()}
18-
{$endRow|noescape}
13+
{endRow()|noescape}
1914
{elseif $iterator->getCounter() % $number === 0}
20-
{$endRow|noescape}
21-
{$startRow|noescape}
15+
{endRow()|noescape}
16+
{startRow()|noescape}
2217
{/if}
2318
{/if}
2419
{/foreach}

0 commit comments

Comments
 (0)