aboutsummaryrefslogtreecommitdiff
path: root/Twig/RutilsExtension.php
blob: d4cf8c2bf2d02a8359579be6f2ec0845f8ca73cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
 * @author Alexander NeonXP Kiryukhin <frei@neonxp.info>
 */

namespace nxp\RutilsBundle\Twig;

use php_rutils\RUtils;
use php_rutils\struct\TimeParams;

class RutilsExtension extends \Twig_Extension
{
    public function getFilters()
    {
        $numeral    = RUtils::numeral();
        $date       = RUtils::dt();
        $translit   = RUtils::translit();
        $typography = RUtils::typo();
        return [
            new \Twig_SimpleFilter('getInWords', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWords($amount, $gender);
            }),
            new \Twig_SimpleFilter('getInWordsInt', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWordsInt($amount, $gender);
            }),
            new \Twig_SimpleFilter('getInWordsFloat', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWordsFloat($amount, $gender);
            }),
            new \Twig_SimpleFilter('getPlural', function ($amount, array $variants, $absence = null) use ($numeral) {
                return $numeral->getPlural($amount, $variants, $absence);
            }),
            new \Twig_SimpleFilter('choosePlural', function ($amount, array $variants) use ($numeral) {
                return $numeral->choosePlural($amount, $variants);
            }),
            new \Twig_SimpleFilter('sumString', function ($amount, $gender, array $variants = null) use ($numeral) {
                return $numeral->sumString($amount, $gender, $variants);
            }),
            new \Twig_SimpleFilter('getRubles', function ($amount, $zeroForKopeck = false) use ($numeral) {
                return $numeral->getRubles($amount, $zeroForKopeck);
            }),
            new \Twig_SimpleFilter('ruStrFTime', function (
                \DateTime $dateTime,
                $format = 'd F Y',
                $monthInflected = false,
                $dayInflected = false,
                $preposition = false,
                $timezone = null
            ) use ($date) {
                $params = TimeParams::create(
                    [
                        'date'           => $dateTime,
                        'format'         => $format,
                        'monthInflected' => $monthInflected,
                        'dayInflected'   => $dayInflected,
                        'preposition'    => $preposition,
                        'timezone'       => $timezone
                    ]
                );
                return $date->ruStrFTime($params);
            }),
            new \Twig_SimpleFilter('distanceOfTimeInWords', function (
                $toTime,
                $fromTime = null,
                $accuracy = RUtils::ACCURACY_YEAR
            ) use ($date) {
                return $date->distanceOfTimeInWords($toTime, $fromTime, $accuracy);
            }),
            new \Twig_SimpleFilter('getAge', function ($birthDate) use ($date) {
                return $date->getAge($birthDate);
            }),
            new \Twig_SimpleFilter('translify', function ($inString) use ($translit) {
                return $translit->translify($inString);
            }),
            new \Twig_SimpleFilter('detranslify', function ($inString) use ($translit) {
                return $translit->detranslify($inString);
            }),
            new \Twig_SimpleFilter('slugify', function ($inString) use ($translit) {
                return $translit->slugify($inString);
            }),
            new \Twig_SimpleFilter('typography', function ($text, array $rules = null) use ($typography) {
                return $typography->typography($text, $rules);
            })
        ];
    }


    public function getFunctions()
    {
        $numeral    = RUtils::numeral();
        $date       = RUtils::dt();
        $translit   = RUtils::translit();
        $typography = RUtils::typo();
        return [
            new \Twig_SimpleFunction('getInWords', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWords($amount, $gender);
            }),
            new \Twig_SimpleFunction('getInWordsInt', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWordsInt($amount, $gender);
            }),
            new \Twig_SimpleFunction('getInWordsFloat', function ($amount, $gender = RUtils::MALE) use ($numeral) {
                return $numeral->getInWordsFloat($amount, $gender);
            }),
            new \Twig_SimpleFunction('getPlural', function ($amount, array $variants, $absence = null) use ($numeral) {
                return $numeral->getPlural($amount, $variants, $absence);
            }),
            new \Twig_SimpleFunction('choosePlural', function ($amount, array $variants) use ($numeral) {
                return $numeral->choosePlural($amount, $variants);
            }),
            new \Twig_SimpleFunction('sumString', function ($amount, $gender, array $variants = null) use ($numeral) {
                return $numeral->sumString($amount, $gender, $variants);
            }),
            new \Twig_SimpleFunction('getRubles', function ($amount, $zeroForKopeck = false) use ($numeral) {
                return $numeral->getRubles($amount, $zeroForKopeck);
            }),
            new \Twig_SimpleFunction('ruStrFTime', function (
                \DateTime $dateTime,
                $format = 'd F Y',
                $monthInflected = false,
                $dayInflected = false,
                $preposition = false,
                $timezone = null
            ) use ($date) {
                $params = TimeParams::create(
                    [
                        'date'           => $dateTime,
                        'format'         => $format,
                        'monthInflected' => $monthInflected,
                        'dayInflected'   => $dayInflected,
                        'preposition'    => $preposition,
                        'timezone'       => $timezone
                    ]
                );
                return $date->ruStrFTime($params);
            }),
            new \Twig_SimpleFunction('distanceOfTimeInWords', function (
                $toTime,
                $fromTime = null,
                $accuracy = RUtils::ACCURACY_YEAR
            ) use ($date) {
                return $date->distanceOfTimeInWords($toTime, $fromTime, $accuracy);
            }),
            new \Twig_SimpleFunction('getAge', function ($birthDate) use ($date) {
                return $date->getAge($birthDate);
            }),
            new \Twig_SimpleFunction('translify', function ($inString) use ($translit) {
                return $translit->translify($inString);
            }),
            new \Twig_SimpleFunction('detranslify', function ($inString) use ($translit) {
                return $translit->detranslify($inString);
            }),
            new \Twig_SimpleFunction('slugify', function ($inString) use ($translit) {
                return $translit->slugify($inString);
            }),
            new \Twig_SimpleFunction('typography', function ($text, array $rules = null) use ($typography) {
                return $typography->typography($text, $rules);
            })
        ];
    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'rutils_extension';
    }

}