Completed
Push — master ( f3c97a...d1068a )
by Vojta
08:02
created

Plugin::getTimeFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php namespace VojtaSvoboda\TwigExtensions;
2
3
use App;
4
use Backend;
5
use Carbon\Carbon;
6
use System\Classes\PluginBase;
7
use Twig_Extension_StringLoader;
8
use Twig_Extensions_Extension_Array;
9
use Twig_Extensions_Extension_Date;
10
use Twig_Extensions_Extension_Intl;
11
use Twig_Extensions_Extension_Text;
12
13
/**
14
 * Twig Extensions Plugin
15
 * - add more Twig filters to your template
16
 *
17
 * @see http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install
18
 */
19
class Plugin extends PluginBase
20
{
21
    /**
22
     * Returns information about this plugin.
23
     *
24
     * @return array
25
     */
26
    public function pluginDetails()
27
    {
28
        return [
29
            'name' => 'Twig Extensions',
30
            'description' => 'Add more Twig filters to your templates.',
31
            'author' => 'Vojta Svoboda',
32
            'icon' => 'icon-plus',
33
        ];
34
    }
35
36
    /**
37
     * Add Twig extensions
38
     *
39
     * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html
40
     * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html
41
     * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html
42
     * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html
43
     *
44
     * @return array
45
     */
46 1
    public function registerMarkupTags()
47
    {
48 1
        $filters = [];
49 1
        $functions = [];
50
51
        // init Twig
52 1
        $twig = App::make('twig.environment');
53
54
        // add String Loader functions
55 1
        $functions += $this->getStringLoaderFunctions($twig);
56
57
        // add Text extensions
58 1
        $filters += $this->getTextFilters($twig);
59
60
        // add Intl extensions if php5-intl installed
61 1
        if (class_exists('IntlDateFormatter')) {
62 1
            $filters += $this->getLocalizedFilters($twig);
63 1
        }
64
65
        // add Array extensions
66 1
        $filters += $this->getArrayFilters($twig);
67
68
        // add Time extensions
69 1
        $filters += $this->getTimeFilters($twig);
70
71
        // add PHP functions
72 1
        $filters += $this->getPhpFunctions();
73
74
        return [
75 1
            'filters' => $filters,
76
            'functions' => $functions
77 1
        ];
78
    }
79
80
    /**
81
     * Returns String Loader functions
82
     *
83
     * @param \Twig_Environment $twig
84
     *
85
     * @return array
86
     */
87 1
    private function getStringLoaderFunctions($twig)
88
    {
89 1
        $stringLoader = new Twig_Extension_StringLoader();
90 1
        $stringLoaderFunc = $stringLoader->getFunctions();
91
92
        return [
93
            'template_from_string' => function($template) use ($twig, $stringLoaderFunc) {
94 1
                $callable = $stringLoaderFunc[0]->getCallable();
95 1
                return $callable($twig, $template);
96
            }
97 1
        ];
98
    }
99
100
    /**
101
     * Returns Text filters
102
     *
103
     * @param \Twig_Environment $twig
104
     *
105
     * @return array
106
     */
107 3
    private function getTextFilters($twig)
108
    {
109 1
        $textExtension = new Twig_Extensions_Extension_Text();
110 1
        $textFilters = $textExtension->getFilters();
111
112
        return [
113 View Code Duplication
            'truncate' => function($value, $length = 30, $preserve = false, $separator = '...') use ($twig, $textFilters) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 3
                $callable = $textFilters[0]->getCallable();
115 3
                return $callable($twig, $value, $length, $preserve, $separator);
116 1
            },
117 View Code Duplication
            'wordwrap' => function($value, $length = 80, $separator = "\n", $preserve = false) use ($twig, $textFilters) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118 1
                $callable = $textFilters[1]->getCallable();
119 1
                return $callable($twig, $value, $length, $separator, $preserve);
120
            }
121 1
        ];
122
    }
123
124
    /**
125
     * Returns Intl filters
126
     *
127
     * @param \Twig_Environment $twig
128
     *
129
     * @return array
130
     */
131 1
    private function getLocalizedFilters($twig)
132
    {
133 1
        $intlExtension = new Twig_Extensions_Extension_Intl();
134 1
        $intlFilters = $intlExtension->getFilters();
135
136
        return [
137
            'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) {
138
                $callable = $intlFilters[0]->getCallable();
139
                return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format);
140 1
            },
141
            'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) {
142
                $callable = $intlFilters[1]->getCallable();
143
                return $callable($twig, $number, $style, $type, $locale);
144 1
            },
145
            'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) {
146
                $callable = $intlFilters[2]->getCallable();
147
                return $callable($twig, $number, $currency, $locale);
148
            }
149 1
        ];
150
    }
151
152
    /**
153
     * Returns Array filters
154
     *
155
     * @param \Twig_Environment $twig
156
     *
157
     * @return array
158
     */
159 1
    private function getArrayFilters($twig)
160
    {
161 1
        $arrayExtension = new Twig_Extensions_Extension_Array();
162 1
        $arrayFilters = $arrayExtension->getFilters();
163
164
        return [
165
            'shuffle' => function($array) use ($twig, $arrayFilters) {
166
                $callable = $arrayFilters[0]->getCallable();
167
                return $callable($twig, $array);
168
            }
169 1
        ];
170
    }
171
172
    /**
173
     * Returns Date filters
174
     *
175
     * @param \Twig_Environment $twig
176
     *
177
     * @return array
178
     */
179 1
    private function getTimeFilters($twig)
180
    {
181 1
        $timeExtension = new Twig_Extensions_Extension_Date();
182 1
        $timeFilters = $timeExtension->getFilters();
183
184
        return [
185
            'time_diff' => function($date, $now = null) use ($twig, $timeFilters) {
186 1
                $callable = $timeFilters[0]->getCallable();
187 1
                return $callable($twig, $date, $now);
188
            }
189 1
        ];
190
    }
191
192
    /**
193
     * Returns plain PHP functions
194
     *
195
     * @return array
196
     */
197 1
    private function getPhpFunctions()
198
    {
199
        return [
200
            'strftime' => function($time, $format = '%d.%m.%Y %H:%M:%S') {
201 1
                $timeObj = new Carbon($time);
202 1
                return strftime($format, $timeObj->getTimestamp());
203 1
            },
204
            'uppercase' => function($string) {
205 1
                return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
206 1
            },
207
            'lowercase' => function($string) {
208 1
                return mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
209 1
            },
210
            'ucfirst' => function($string) {
211 1
                return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
212 1
            },
213
            'lcfirst' => function($string) {
214 1
                return lcfirst($string);
215 1
            },
216
            'ltrim' => function($string, $charlist = " \t\n\r\0\x0B") {
217 1
                return ltrim($string, $charlist);
218 1
            },
219
            'rtrim' => function($string, $charlist = " \t\n\r\0\x0B") {
220 1
                return rtrim($string, $charlist);
221 1
            },
222
            'str_repeat' => function($string, $multiplier = 1) {
223 1
                return str_repeat($string, $multiplier);
224 1
            },
225
            'plural' => function($string, $count = 2) {
226 1
                return str_plural($string, $count);
227 1
            },
228
            'strpad' => function($string, $pad_length, $pad_string = ' ') {
229 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_BOTH);
230 1
            },
231
            'leftpad' => function($string, $pad_length, $pad_string = ' ') {
232 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_LEFT);
233 1
            },
234 1
            'rightpad' => function($string, $pad_length, $pad_string = ' ') {
235 1
                return str_pad($string, $pad_length, $pad_string, $pad_type = STR_PAD_RIGHT);
236 1
            },
237 1
        ];
238
    }
239
}
240