Completed
Push — master ( c80402...25bb76 )
by Philip
02:57
created

TwigExtensions::arrayColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
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 CRUDlex;
13
14
use Pimple\Container;
15
use Symfony\Component\Intl\Intl;
16
17
/**
18
 * Provides and setups the Twig extensions like filters.
19
 */
20
class TwigExtensions {
21
22
    /**
23
     * Registers all extensions.
24
     *
25
     * @param Container $app
26
     * the current application
27
     */
28
    public function registerTwigExtensions(Container $app) {
29
        $self = $this;
30
        $app->extend('twig', function(\Twig_Environment $twig) use ($self) {
31
            $twig->addFilter(new \Twig_SimpleFilter('arrayColumn', [$self, 'arrayColumn']));
32
            $twig->addFilter(new \Twig_SimpleFilter('languageName', [$self, 'getLanguageName']));
33
            $twig->addFilter(new \Twig_SimpleFilter('float', [$self, 'formatFloat']));
34
            return $twig;
35
        });
36
    }
37
38
    /**
39
     * To have array_column available as Twig filter.
40
     *
41
     * @param $array
42
     * the array
43
     * @param $key
44
     * the key
45
     *
46
     * @return array
47
     * the resulting array
48
     */
49
    public function arrayColumn($array, $key) {
50
        return array_column($array, $key);
51
    }
52
53
    /**
54
     * Gets a language name in the given language.
55
     *
56
     * @param string $language
57
     * the language code of the desired language name
58
     *
59
     * @return string
60
     * the language name in the given language or null if not available
61
     */
62
    public function getLanguageName($language) {
63
        return Intl::getLanguageBundle()->getLanguageName($language, $language, $language);
64
    }
65
66
    /**
67
     * Formats a float to not display in scientific notation.
68
     *
69
     * @param float $float
70
     * the float to format
71
     *
72
     * @return double|string
73
     * the formated float
74
     */
75
    public function formatFloat($float) {
76
77
        if (!$float) {
78
            return $float;
79
        }
80
81
        $zeroFraction = $float - floor($float) == 0 ? '0' : '';
82
83
        // We don't want values like 0.004 converted to  0.00400000000000000008
84
        if ($float > 0.0001) {
85
            return $float.($zeroFraction === '0' ? '.'.$zeroFraction : '');
86
        }
87
88
        // We don't want values like 0.00004 converted to its scientific notation 4.0E-5
89
        return rtrim(sprintf('%.20F', $float), '0').$zeroFraction;
90
    }
91
}
92