1<?php
2
3/**
4 * This file is part of Twig.
5 *
6 * (c) 2014 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11use Symfony\Component\Translation\TranslatorInterface;
12use Symfony\Component\Translation\IdentityTranslator;
13
14/**
15 * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
16 */
17class Twig_Extensions_Extension_Date extends Twig_Extension
18{
19    public static $units = array(
20        'y' => 'year',
21        'm' => 'month',
22        'd' => 'day',
23        'h' => 'hour',
24        'i' => 'minute',
25        's' => 'second',
26    );
27
28    /**
29     * @var TranslatorInterface
30     */
31    private $translator;
32
33    public function __construct(TranslatorInterface $translator = null)
34    {
35        // Ignore the IdentityTranslator, otherwise the parameters won't be replaced properly
36        if ($translator instanceof IdentityTranslator) {
37            $translator = null;
38        }
39
40        $this->translator = $translator;
41    }
42
43    /**
44     * {@inheritdoc}
45     */
46    public function getFilters()
47    {
48        return array(
49            new Twig_SimpleFilter('time_diff', array($this, 'diff'), array('needs_environment' => true)),
50        );
51    }
52
53    /**
54     * Filter for converting dates to a time ago string like Facebook and Twitter has.
55     *
56     * @param Twig_Environment $env  a Twig_Environment instance
57     * @param string|DateTime  $date a string or DateTime object to convert
58     * @param string|DateTime  $now  A string or DateTime object to compare with. If none given, the current time will be used.
59     *
60     * @return string the converted time
61     */
62    public function diff(Twig_Environment $env, $date, $now = null)
63    {
64        // Convert both dates to DateTime instances.
65        $date = twig_date_converter($env, $date);
66        $now = twig_date_converter($env, $now);
67
68        // Get the difference between the two DateTime objects.
69        $diff = $date->diff($now);
70
71        // Check for each interval if it appears in the $diff object.
72        foreach (self::$units as $attribute => $unit) {
73            $count = $diff->$attribute;
74
75            if (0 !== $count) {
76                return $this->getPluralizedInterval($count, $diff->invert, $unit);
77            }
78        }
79
80        return '';
81    }
82
83    protected function getPluralizedInterval($count, $invert, $unit)
84    {
85        if ($this->translator) {
86            $id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit);
87
88            return $this->translator->transChoice($id, $count, array('%count%' => $count), 'date');
89        }
90
91        if (1 !== $count) {
92            $unit .= 's';
93        }
94
95        return $invert ? "in $count $unit" : "$count $unit ago";
96    }
97
98    /**
99     * {@inheritdoc}
100     */
101    public function getName()
102    {
103        return 'date';
104    }
105}
106
107class_alias('Twig_Extensions_Extension_Date', 'Twig\Extensions\DateExtension', false);
108