[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[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 /**
15 * This ViewHelper counts elements of the specified array or countable object.
16 *
17 * = Examples =
18 *
19 * <code title="Count array elements">
20 * <f:count subject="{0:1, 1:2, 2:3, 3:4}" />
21 * </code>
22 * <output>
23 * 4
24 * </output>
25 *
26 * <code title="inline notation">
27 * {objects -> f:count()}
28 * </code>
29 * <output>
30 * 10 (depending on the number of items in {objects})
31 * </output>
32 *
33 * @api
34 */
35 class CountViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
36
37 /**
38 * @var boolean
39 */
40 protected $escapingInterceptorEnabled = FALSE;
41
42 /**
43 * Counts the items of a given property.
44 *
45 * @param array $subject The array or \Countable to be counted
46 * @return integer The number of elements
47 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
48 * @api
49 */
50 public function render($subject = NULL) {
51 if ($subject === NULL) {
52 $subject = $this->renderChildren();
53 }
54 if (is_object($subject) && !$subject instanceof \Countable) {
55 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('CountViewHelper only supports arrays and objects implementing \Countable interface. Given: "' . get_class($subject) . '"', 1279808078);
56 }
57 return count($subject);
58 }
59 }
60
61 ?>