Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

AdminBundle/Twig/DateByLocaleExtension.php (2 issues)

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
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFilter;
8
9
/**
10
 * DateByLocaleExtension
11
 *
12
 * @final since 5.4
13
 */
14
class DateByLocaleExtension extends AbstractExtension
15
{
16
    /**
17
     * Get Twig filters defined in this extension.
18
     *
19
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use TwigFilter[].

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