Completed
Push — master ( 23d5f0...daf9a4 )
by Kamil
05:58
created

TestHtmlAttributeExtension::getFunctions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\Twig;
15
16
use Twig\Extension\AbstractExtension;
17
use Twig\TwigFunction;
18
19
final class TestHtmlAttributeExtension extends AbstractExtension
20
{
21
    /** @var string */
22
    private $env;
23
24
    public function __construct(string $env)
25
    {
26
        $this->env = $env;
27
    }
28
29
    /**
30
     * @return TwigFunction[]
31
     */
32
    public function getFunctions(): array
33
    {
34
        return [
35
            new TwigFunction(
36
                'sylius_test_html_attribute',
37
                function (string $name, ?string $value = null): string {
38
                    if (strpos($this->env, 'test') === 0) {
39
                        return sprintf('data-test-%s="%s"', $name, (string) $value);
40
                    }
41
42
                    return '';
43
                },
44
                ['is_safe' => ['html']]
45
            ),
46
        ];
47
    }
48
}
49