[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / DateViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Core\Utility\MathUtility;
15 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
19
20 /**
21 * Formats a \DateTime object.
22 *
23 * = Examples =
24 *
25 * <code title="Defaults">
26 * <f:format.date>{dateObject}</f:format.date>
27 * </code>
28 * <output>
29 * 1980-12-13
30 * (depending on the current date)
31 * </output>
32 *
33 * <code title="Custom date format">
34 * <f:format.date format="H:i">{dateObject}</f:format.date>
35 * </code>
36 * <output>
37 * 01:23
38 * (depending on the current time)
39 * </output>
40 *
41 * <code title="strtotime string">
42 * <f:format.date format="d.m.Y - H:i:s">+1 week 2 days 4 hours 2 seconds</f:format.date>
43 * </code>
44 * <output>
45 * 13.12.1980 - 21:03:42
46 * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
47 * </output>
48 *
49 * <code title="Localized dates using strftime date format">
50 * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
51 * </code>
52 * <output>
53 * 13. Dezember 1980
54 * (depending on the current date and defined locale. In the example you see the 1980-12-13 in a german locale)
55 * </output>
56 *
57 * <code title="Inline notation">
58 * {f:format.date(date: dateObject)}
59 * </code>
60 * <output>
61 * 1980-12-13
62 * (depending on the value of {dateObject})
63 * </output>
64 *
65 * <code title="Inline notation (2nd variant)">
66 * {dateObject -> f:format.date()}
67 * </code>
68 * <output>
69 * 1980-12-13
70 * (depending on the value of {dateObject})
71 * </output>
72 *
73 * @api
74 */
75 class DateViewHelper extends AbstractViewHelper implements CompilableInterface {
76
77 /**
78 * @var bool
79 */
80 protected $escapingInterceptorEnabled = FALSE;
81
82 /**
83 * Render the supplied DateTime object as a formatted date.
84 *
85 * @param mixed $date either a DateTime object or a string that is accepted by DateTime constructor
86 * @param string $format Format String which is taken to format the Date/Time
87 *
88 * @return string Formatted date
89 * @throws Exception
90 * @api
91 */
92 public function render($date = NULL, $format = '') {
93 return static::renderStatic(
94 array(
95 'date' => $date,
96 'format' => $format
97 ),
98 $this->buildRenderChildrenClosure(),
99 $this->renderingContext
100 );
101 }
102
103 /**
104 * @param array $arguments
105 * @param callable $renderChildrenClosure
106 * @param RenderingContextInterface $renderingContext
107 *
108 * @return string
109 * @throws Exception
110 */
111 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
112 $date = $arguments['date'];
113 $format = $arguments['format'];
114 if ($format === '') {
115 $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
116 }
117
118 if ($date === NULL) {
119 $date = $renderChildrenClosure();
120 if ($date === NULL) {
121 return '';
122 }
123 }
124 if (!$date instanceof \DateTime) {
125 try {
126 if (MathUtility::canBeInterpretedAsInteger($date)) {
127 $date = new \DateTime('@' . $date);
128 } else {
129 $date = new \DateTime($date);
130 }
131 $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
132 } catch (\Exception $exception) {
133 throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor.', 1241722579);
134 }
135 }
136
137 if (strpos($format, '%') !== FALSE) {
138 return strftime($format, $date->format('U'));
139 } else {
140 return $date->format($format);
141 }
142 }
143
144 }