Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

AdminBundle/Twig/DateByLocaleExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Twig;
4
5
use IntlDateFormatter as DateFormatter;
6
7
/**
8
 * DateByLocaleExtension
9
 */
10
class DateByLocaleExtension extends \Twig_Extension
11
{
12
    /**
13
     * Get Twig filters defined in this extension.
14
     *
15
     * @return array
16
     */
17
    public function getFilters()
18
    {
19
        return array(
20
            new \Twig_SimpleFilter('localeDate', '\Kunstmaan\AdminBundle\Twig\DateByLocaleExtension::localeDateFilter'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: since Twig 2.7, use "Twig\TwigFilter" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
        );
22
    }
23
24
    /**
25
     * A date formatting filter for Twig, renders the date using the specified parameters.
26
     *
27
     * @param mixed  $date     Unix timestamp to format
28
     * @param string $locale   The locale
29
     * @param string $dateType The date type
30
     * @param string $timeType The time type
31
     * @param string $pattern  The pattern to use
32
     *
33
     * @return string
34
     */
35
    public static function localeDateFilter($date, $locale = 'nl', $dateType = 'medium', $timeType = 'none', $pattern = null)
36
    {
37
        $values = array(
38
            'none' => DateFormatter::NONE,
39
            'short' => DateFormatter::SHORT,
40
            'medium' => DateFormatter::MEDIUM,
41
            'long' => DateFormatter::LONG,
42
            'full' => DateFormatter::FULL,
43
        );
44
45
        if (is_null($pattern)) {
46
            $dateFormatter = DateFormatter::create(
47
                $locale,
48
                $values[$dateType],
49
                $values[$timeType], 'Europe/Brussels'
50
            );
51
        } else {
52
            $dateFormatter = DateFormatter::create(
53
                $locale,
54
                $values[$dateType],
55
                $values[$timeType], 'Europe/Brussels',
56
                DateFormatter::GREGORIAN,
57
                $pattern
58
            );
59
        }
60
61
        return $dateFormatter->format($date);
62
    }
63
}
64