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

DateByLocaleExtension::localeDateFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 2
nc 2
nop 5
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
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" 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...
11
{
12
    /**
13
     * Get Twig filters defined in this extension.
14
     *
15
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Twig_SimpleFilter[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $pattern not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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