TwigExtensions::extendForQueryParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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, ...$findAndReplace
37
            ) use ($dictionary) {
38
                if (array_key_exists($string, $dictionary)) {
39
                    $string = $dictionary[$string];
40
                }
41
                if (! empty($findAndReplace)) {
42
                    foreach ($findAndReplace[0] as $find => $replace) {
43
                        $string = str_replace('{{'.$find.'}}', $replace, $string);
44
                    }
45
                }
46
                return $string;
47
            },
48
            array('is_safe' => array('html'))
49
        );
50
        $this->twig->addFunction($filter);
51
    }
52
53
    protected function extendForGetUrl() : void
54
    {
55
        $filter = new Twig_SimpleFunction(
56
            'getUrl',
57
            function (
58
                $alias,
59
                $params = []
60
            ) {
61
                $function = new Functions\GetUrl(
62
                    $this->config['runtime']['base_url'],
63
                    $this->config['runtime']['aliases'],
64
                    $alias,
65
                    $params
66
                );
67
                return $function->run();
68
            },
69
            array('is_safe' => array('html'))
70
        );
71
        $this->twig->addFunction($filter);
72
    }
73
74
    protected function extendForWidget() : void
75
    {
76
        $filter = new \Twig_SimpleFunction(
77
            'Widget_*_*',
78
            function ($widgetNameStr, $widgetActionStr, $args = []) {
79
                $function = new Functions\Widget($this->twig, $this->config, $widgetNameStr, $widgetActionStr, $args);
80
                return $function->run();
81
            },
82
            array('is_safe' => array('html'))
83
        );
84
        $this->twig->addFunction($filter);
85
    }
86
87
    protected function extendForQueryParams() : void
88
    {
89
        $filter = new \Twig_SimpleFunction(
90
            'queryParams',
91
            function ($queryParams, $prefix = '?') {
92
                return $prefix . http_build_query($queryParams);
93
            },
94
            array('is_safe' => array('html'))
95
        );
96
        $this->twig->addFunction($filter);
97
    }
98
99
    protected function extendForSiteUrl() : void
100
    {
101
        $filter = new \Twig_SimpleFunction(
102
            'siteUrl',
103
            function ($path = '') {
104
                return $this->config['runtime']['base_url'] . $path;
105
            },
106
            array('is_safe' => array('html'))
107
        );
108
        $this->twig->addFunction($filter);
109
    }
110
111
    protected function extendForVarDump() : void
112
    {
113
        $filter = new \Twig_SimpleFunction(
114
            'varDump',
115
            function ($args) {
116
                /**
117
            * @noinspection ForgottenDebugOutputInspection
118
            */
119
                var_dump($args);
120
            },
121
            array('is_safe' => array('html'))
122
        );
123
        $this->twig->addFunction($filter);
124
    }
125
126
    protected function extendForPagination() : void
127
    {
128
        /**
129
 * @noinspection MoreThanThreeArgumentsInspection
130
*/
131
        $filter = new \Twig_SimpleFunction(
132
            'Pagination',
133
            function (
134
                int $total,
135
                int $current,
136
                string $linkTemplate,
137
                string $parentTemplate = '<ul class="pagination">(items)</ul>',
138
                string $itemTemplate = '<li class="(item_class)">(link)</li>',
139
                string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>',
140
                string $ellipsesTemplate = '<li><a>...</a></li>'
141
            ) {
142
143
                $function = new Functions\Pagination(
144
                    $total,
145
                    $current,
146
                    $linkTemplate,
147
                    $parentTemplate,
148
                    $itemTemplate,
149
                    $linkItemTemplate,
150
                    $ellipsesTemplate
151
                );
152
                return $function->run();
153
            },
154
            array('is_safe' => array('html'))
155
        );
156
        $this->twig->addFunction($filter);
157
    }
158
}
159