StripAttributesExtension::getFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Twig\Extension;
17
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20
use Veslo\AppBundle\Html\Tag\AttributeRemover;
21
22
/**
23
 * Removes any attributes from tags in html string
24
 */
25
class StripAttributesExtension extends AbstractExtension
26
{
27
    /**
28
     * Removes attributes from tags in html string
29
     *
30
     * @var AttributeRemover
31
     */
32
    private $attributeRemover;
33
34
    /**
35
     * StripAttributesExtension constructor.
36
     *
37
     * @param AttributeRemover $attributeRemover Removes attributes from tags in html string
38
     */
39
    public function __construct(AttributeRemover $attributeRemover)
40
    {
41
        $this->attributeRemover = $attributeRemover;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getFilters()
48
    {
49
        return [
50
            new TwigFilter('stripattributes', [$this, 'removeAttributesFromTags']),
51
        ];
52
    }
53
54
    /**
55
     * Removes any attributes from tags in html string
56
     *
57
     * @param string $html Html string
58
     *
59
     * @return string|null
60
     */
61
    public function removeAttributesFromTags(?string $html): ?string
62
    {
63
        if (empty($html)) {
64
            return $html;
65
        }
66
67
        return $this->attributeRemover->removeAttributes($html);
68
    }
69
}
70