Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

CoreExtension::pageAddAsset()   C

Complexity

Conditions 9
Paths 7

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 7
nop 3
dl 0
loc 24
rs 5.3563
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreBundle\Twig\Extension;
13
14
use Symfony\Component\Intl\Intl;
15
use Zikula\Bundle\CoreBundle\Twig;
16
use Zikula\Common\Translator\TranslatorInterface;
17
18
class CoreExtension extends \Twig_Extension
19
{
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * CoreExtension constructor.
27
     * @param TranslatorInterface $translator
28
     */
29
    public function __construct(TranslatorInterface $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getTokenParsers()
38
    {
39
        return [
40
            new Twig\TokenParser\SwitchTokenParser()
41
        ];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getFunctions()
48
    {
49
        $functions = [
50
            new \Twig_SimpleFunction('array_unset', [$this, 'arrayUnset']),
51
            new \Twig_SimpleFunction('callFunc', [$this, 'callFunc']),
52
        ];
53
54
        return $functions;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getFilters()
61
    {
62
        return [
63
            new \Twig_SimpleFilter('languageName', [$this, 'languageName']),
64
            new \Twig_SimpleFilter('yesNo', [$this, 'yesNo']),
65
            new \Twig_SimpleFilter('php', [$this, 'applyPhp']),
66
            new \Twig_SimpleFilter('protectMail', [$this, 'protectMailAddress'], ['is_safe' => ['html']]),
67
        ];
68
    }
69
70
    /**
71
     * Delete a key of an array
72
     *
73
     * @param array  $array Source array
74
     * @param string $key   The key to remove
75
     *
76
     * @return array
77
     */
78
    public function arrayUnset($array, $key)
79
    {
80
        unset($array[$key]);
81
82
        return $array;
83
    }
84
85
    /**
86
     * @param string $code
87
     * @return string
88
     */
89
    public function languageName($code)
90
    {
91
        return Intl::getLanguageBundle()->getLanguageName($code);
92
    }
93
94
    /**
95
     * @param $string
96
     * @return string
97
     */
98
    public function yesNo($string)
99
    {
100
        if ($string != '0' && $string != '1') {
101
            return $string;
102
        }
103
104
        return (bool)$string ? $this->translator->__('Yes') : $this->translator->__('No');
105
    }
106
107
    /**
108
     * Apply an existing function (e.g. php's `md5`) to a string.
109
     *
110
     * @param $string
111
     * @param $func
112
     * @return mixed
113
     */
114
    public function applyPhp($string, $func)
115
    {
116
        if (function_exists($func)) {
117
            return $func($string);
118
        }
119
120
        return $string;
121
    }
122
123
    /**
124
     * Protect a given mail address by finding the text 'x@y' and replacing
125
     * it with HTML entities. This provides protection against email harvesters.
126
     *
127
     * @param string
128
     * @return string
129
     */
130
    public function protectMailAddress($string)
131
    {
132
        $string = preg_replace_callback(
133
            '/(.)@(.)/s',
134
            function ($m) {
135
                return "&#" . sprintf("%03d", ord($m[1])) . ";&#064;&#" .sprintf("%03d", ord($m[2])) . ";";
136
            },
137
            $string
138
        );
139
140
        return $string;
141
    }
142
143
    /**
144
     * Call a php callable with parameters.
145
     * @param callable $callable
146
     * @param array $params
147
     * @return mixed
148
     */
149
    public function callFunc(callable $callable, array $params = [])
150
    {
151
        if (function_exists($callable)) {
152
            return call_user_func_array($callable, $params);
153
        }
154
        throw new \InvalidArgumentException($this->translator->__('Function does not exist or is not callable.'));
155
    }
156
}
157