BeditaTwigExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 52
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getFilters() 0 25 1
A getFunctions() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\WebTools\View\Twig;
16
17
use Cake\Core\Configure;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20
use Twig\TwigFunction;
21
22
/**
23
 * BEdita Twig extension class.
24
 *
25
 * Provide BEdita utils to Twig view.
26
 */
27
class BeditaTwigExtension extends AbstractExtension
28
{
29
    /**
30
     * @inheritDoc
31
     */
32
    public function getName(): string
33
    {
34
        return 'bedita';
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function getFunctions(): array
41
    {
42
        return [
43
            new TwigFunction('config', [Configure::class, 'read']),
44
            new TwigFunction('write_config', function ($key, $val): void {
45
                // avoid unwanted return value display in templates
46
                Configure::write($key, $val);
47
            }),
48
        ];
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function getFilters(): array
55
    {
56
        return [
57
            new TwigFilter(
58
                'shuffle',
59
                function (array $array) {
60
                    shuffle($array);
61
62
                    return $array;
63
                }
64
            ),
65
            new TwigFilter(
66
                'ksort',
67
                function (array $array) {
68
                    ksort($array);
69
70
                    return $array;
71
                }
72
            ),
73
            new TwigFilter(
74
                'krsort',
75
                function (array $array) {
76
                    krsort($array);
77
78
                    return $array;
79
                }
80
            ),
81
        ];
82
    }
83
}
84