[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / JsonViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It originated from the Neos.Form package (www.neos.io)
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
22
23 /**
24 * Wrapper for PHPs json_encode function.
25 *
26 * = Examples =
27 *
28 * <code title="encoding a view variable">
29 * {someArray -> f:format.json()}
30 * </code>
31 * <output>
32 * ["array","values"]
33 * // depending on the value of {someArray}
34 * </output>
35 *
36 * <code title="associative array">
37 * {f:format.json(value: {foo: 'bar', bar: 'baz'})}
38 * </code>
39 * <output>
40 * {"foo":"bar","bar":"baz"}
41 * </output>
42 *
43 * <code title="non-associative array with forced object">
44 * {f:format.json(value: {0: 'bar', 1: 'baz'}, forceObject: true)}
45 * </code>
46 * <output>
47 * {"0":"bar","1":"baz"}
48 * </output>
49 */
50 class JsonViewHelper extends AbstractViewHelper
51 {
52 use CompileWithContentArgumentAndRenderStatic;
53
54 /**
55 * @var bool
56 */
57 protected $escapeChildren = false;
58
59 /**
60 * Initialize arguments
61 */
62 public function initializeArguments()
63 {
64 $this->registerArgument('value', 'mixed', 'The incoming data to convert, or null if VH children should be used');
65 $this->registerArgument('forceObject', 'bool', 'Outputs an JSON object rather than an array', false, false);
66 }
67
68 /**
69 * Applies json_encode() on the specified value.
70 *
71 * Outputs content with its JSON representation. To prevent issues in HTML context, occurrences
72 * of greater-than or less-than characters are converted to their hexadecimal representations.
73 *
74 * If $forceObject is TRUE a JSON object is outputted even if the value is a non-associative array
75 * Example: array('foo', 'bar') as input will not be ["foo","bar"] but {"0":"foo","1":"bar"}
76 *
77 * @param array $arguments
78 * @param \Closure $renderChildrenClosure
79 * @param RenderingContextInterface $renderingContext
80 * @see http://www.php.net/manual/en/function.json-encode.php
81 * @return string
82 */
83 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
84 {
85 $value = $renderChildrenClosure();
86 $options = JSON_HEX_TAG;
87 if ($arguments['forceObject'] !== false) {
88 $options = $options | JSON_FORCE_OBJECT;
89 }
90 return json_encode($value, $options);
91 }
92 }