Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Termwind\Html;
6
 
7
use Termwind\Components\Element;
8
use Termwind\Termwind;
9
use Termwind\ValueObjects\Node;
10
 
11
/**
12
 * @internal
13
 */
14
final class PreRenderer
15
{
16
    /**
17
     * Gets HTML content from a given node and converts to the content element.
18
     */
19
    public function toElement(Node $node): Element
20
    {
21
        $lines = explode("\n", $node->getHtml());
22
        if (reset($lines) === '') {
23
            array_shift($lines);
24
        }
25
 
26
        if (end($lines) === '') {
27
            array_pop($lines);
28
        }
29
 
30
        $maxStrLen = array_reduce(
31
            $lines,
32
            static fn (int $max, string $line) => ($max < strlen($line)) ? strlen($line) : $max,
33
 
34
        );
35
 
36
        $styles = $node->getClassAttribute();
37
        $html = array_map(
38
            static fn (string $line) => (string) Termwind::div(str_pad($line, $maxStrLen + 3), $styles),
39
            $lines
40
        );
41
 
42
        return Termwind::raw(
43
            implode('', $html)
44
        );
45
    }
46
}