Test Failed
Push — dev ( 55bb00...c2c26e )
by Janko
17:14
created

TwigHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMethodsAndFilters() 0 24 2
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Twig;
6
7
use JBBCode\Parser;
8
use Stu\Module\Tal\TalHelper;
9
use Twig\Environment;
10
use Twig\TwigFilter;
11
12
class TwigHelper
13
{
14
    private Environment $environment;
15
16
    private Parser $parser;
17
18
    public function __construct(
19
        Environment $environment,
20
        Parser $parser
21
    ) {
22
        $this->environment = $environment;
23
        $this->parser = $parser;
24
    }
25
26
    /**
27
     * Registers global available twig methods and filters
28
     */
29
    public function registerMethodsAndFilters(): void
30
    {
31
        $bbcode2txtFilter = new TwigFilter('bbcode2txt', function ($string) {
32
            return $this->parser->parse($string)->getAsText();
33
        });
34
        $this->environment->addFilter($bbcode2txtFilter);
35
36
        $bbcodeFilter = new TwigFilter('bbcode', function ($string) {
37
            return $this->parser->parse($string)->getAsHTML();
38
        });
39
        $this->environment->addFilter($bbcodeFilter);
40
41
        $jsquoteFilter = new TwigFilter('jsquote', function ($string) {
42
            return TalHelper::jsquote($string);
43
        });
44
        $this->environment->addFilter($jsquoteFilter);
45
46
        $addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value) {
47
            if (is_integer($value)) {
48
                return TalHelper::addPlusCharacter(strval($value));
49
            }
50
            return TalHelper::addPlusCharacter($value);
51
        });
52
        $this->environment->addFilter($addPlusCharacterFilter);
53
    }
54
}
55