Completed
Push — master ( 90104f...724d92 )
by Mehmet
21:23
created

TwigExtensions::extendForTranslator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
cc 2
eloc 12
nc 1
nop 0
crap 6
1
<?php
2
declare(strict_types = 1);
3
4
namespace Selami\View\Twig;
5
6
use Selami\Stdlib\CaseConverter;
7
use Selami\View\ExtensionsAbstract;
8
use Twig\Environment;
9
use InvalidArgumentException;
10
use BadMethodCallException;
11
use Twig_SimpleFunction;
12
13
/**
14
 * Class TwigExtensions extends ViewExtensionsAbstracts
15
 *
16
 * @package Selami\View\Twig
17
 */
18
class TwigExtensions extends ExtensionsAbstract
19
{
20
    private $twig;
21
    private $config;
22
23
    public function __construct(Environment $twig, array $config)
24
    {
25
        $this->twig = $twig;
26
        $this->config = $config;
27
        $this->loadFunctions();
28
    }
29
30
    protected function extendForTranslator() : void
31
    {
32
        $dictionary = $this->config['dictionary'];
33
        $filter = new \Twig_SimpleFunction(
34
            'translate',
35
            function (
36
                $string
37
            ) use ($dictionary) {
38
                if (array_key_exists($string, $dictionary)) {
39
                    return $dictionary[$string];
40
                }
41
                return $string;
42
            },
43
            array('is_safe' => array('html'))
44
        );
45
        $this->twig->addFunction($filter);
46
    }
47
48 View Code Duplication
    protected function extendForGetUrl() : void
49
    {
50
        $filter = new Twig_SimpleFunction(
51
            'getUrl',
52
            function (
53
                $alias,
54
                $params = []
55
            ) {
56
                $function = new Functions\GetUrl($this->config['base_url'], $this->config['aliases'], $alias, $params);
57
                return $function->run();
58
            },
59
            array('is_safe' => array('html'))
60
        );
61
        $this->twig->addFunction($filter);
62
    }
63
64 View Code Duplication
    protected function extendForWidget() : void
65
    {
66
        $filter = new \Twig_SimpleFunction(
67
            'Widget_*_*',
68
            function ($widgetNameStr, $widgetActionStr, $args = []) {
69
                $function = new Functions\Widget($this->twig, $this->config, $widgetNameStr, $widgetActionStr, $args);
70
                return $function->run();
71
            },
72
            array('is_safe' => array('html'))
73
        );
74
        $this->twig->addFunction($filter);
75
    }
76
77 View Code Duplication
    protected function extendForQueryParams() : void
78
    {
79
        $filter = new \Twig_SimpleFunction(
80
            'queryParams',
81
            function ($queryParams, $prefix = '?') {
82
                return $prefix . http_build_query($queryParams);
83
            },
84
            array('is_safe' => array('html'))
85
        );
86
        $this->twig->addFunction($filter);
87
    }
88
89 View Code Duplication
    protected function extendForSiteUrl() : void
90
    {
91
        $filter = new \Twig_SimpleFunction(
92
            'siteUrl',
93
            function ($path = '') {
94
                return $this->config['base_url'] . $path;
95
            },
96
            array('is_safe' => array('html'))
97
        );
98
        $this->twig->addFunction($filter);
99
    }
100
101
    protected function extendForVarDump() : void
102
    {
103
        $filter = new \Twig_SimpleFunction(
104
            'varDump',
105
            function ($args) {
106
                /**
107
            * @noinspection ForgottenDebugOutputInspection
108
            */
109
                var_dump($args);
110
            },
111
            array('is_safe' => array('html'))
112
        );
113
        $this->twig->addFunction($filter);
114
    }
115
116
    protected function extendForPagination() : void
117
    {
118
        /**
119
 * @noinspection MoreThanThreeArgumentsInspection
120
*/
121
        $filter = new \Twig_SimpleFunction(
122
            'Pagination',
123
            function (
124
                int $total,
125
                int $current,
126
                string $linkTemplate,
127
                string $parentTemplate = '<ul class="pagination">(items)</ul>',
128
                string $itemTemplate = '<li class="(item_class)">(link)</li>',
129
                string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>',
130
                string $ellipsesTemplate = '<li><a>...</a></li>'
131
            ) {
132
133
                $function = new Functions\Pagination(
134
                    $total,
135
                    $current,
136
                    $linkTemplate,
137
                    $parentTemplate,
138
                    $itemTemplate,
139
                    $linkItemTemplate,
140
                    $ellipsesTemplate
141
                );
142
                return $function->run();
143
            },
144
            array('is_safe' => array('html'))
145
        );
146
        $this->twig->addFunction($filter);
147
    }
148
}
149