• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / form-renderer / 4584589568

pending completion
4584589568

push

github

Petr Morávek
Fix CS

247 of 265 relevant lines covered (93.21%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/FormRenderer/Macros/FormRendererMacros.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\FormRenderer\Macros;
5

6
use Latte;
7
use Nepada\FormRenderer\Helpers;
8

9
final class FormRendererMacros extends Latte\Macros\MacroSet
10
{
11

12
    public static function install(Latte\Compiler $compiler): void
13
    {
14
        $me = new static($compiler);
15
        $me->addMacro('class', null, null, [$me, 'macroClass']);
16
    }
17

18
    /**
19
     * Improved version of `n:class="..."` that supports array arguments, e.g. `n:class="firstClass, $condition ? [foo => true, bar => true], anotherClass"`.
20
     * This is especially useful in combination with instances of Nette\Utils\Html, so you can do stuff like `n:class="foo, $el->class"`.
21
     *
22
     * @param Latte\MacroNode $node
23
     * @param Latte\PhpWriter $writer
24
     * @return string
25
     * @throws Latte\CompileException
26
     */
27
    public function macroClass(Latte\MacroNode $node, Latte\PhpWriter $writer): string
28
    {
29
        if (isset($node->htmlNode->attrs['class'])) {
30
            throw new Latte\CompileException('It is not possible to combine class with n:class.');
31
        }
32

33
        $classListCode = '$_tmp = array_filter(array_merge(...array_map([\'' . Helpers::class . '\', \'parseClassList\'], %node.array)))';
34
        $code = 'if (' . $classListCode . ') echo \' class="\', %escape(implode(\' \', array_unique($_tmp))), \'"\';';
35

36
        return $writer->write($code);
37
    }
38

39
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc