[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / CountViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
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\Fluid\Core\Rendering\RenderingContextInterface;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
18
19 /**
20 * This ViewHelper counts elements of the specified array or countable object.
21 *
22 * = Examples =
23 *
24 * <code title="Count array elements">
25 * <f:count subject="{0:1, 1:2, 2:3, 3:4}" />
26 * </code>
27 * <output>
28 * 4
29 * </output>
30 *
31 * <code title="inline notation">
32 * {objects -> f:count()}
33 * </code>
34 * <output>
35 * 10 (depending on the number of items in {objects})
36 * </output>
37 *
38 * @api
39 */
40 class CountViewHelper extends AbstractViewHelper implements CompilableInterface {
41
42 /**
43 * @var bool
44 */
45 protected $escapingInterceptorEnabled = FALSE;
46
47 /**
48 * Counts the items of a given property.
49 *
50 * @param array $subject The array or \Countable to be counted
51 * @return int The number of elements
52 * @throws Exception
53 * @api
54 */
55 public function render($subject = NULL) {
56 return static::renderStatic(
57 array('subject' => $subject),
58 $this->buildRenderChildrenClosure(),
59 $this->renderingContext
60 );
61 }
62
63 /**
64 * @param array $arguments
65 * @param callable $renderChildrenClosure
66 * @param RenderingContextInterface $renderingContext
67 *
68 * @return int
69 * @throws Exception
70 */
71 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
72 $subject = $arguments['subject'];
73 if ($subject === NULL) {
74 $subject = $renderChildrenClosure();
75 }
76 if (is_object($subject) && !$subject instanceof \Countable) {
77 throw new Exception('CountViewHelper only supports arrays and objects implementing \Countable interface. Given: "' . get_class($subject) . '"', 1279808078);
78 }
79 return count($subject);
80 }
81
82 }