-
Notifications
You must be signed in to change notification settings - Fork 91
/
JsonViewHelper.php
95 lines (86 loc) · 2.64 KB
/
JsonViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\ViewHelpers\Format;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Wrapper for PHPs :php:`json_encode` function.
* See https://www.php.net/manual/function.json-encode.php.
*
* Examples
* ========
*
* Encoding a view variable
* ------------------------
*
* ::
*
* {someArray -> f:format.json()}
*
* ``["array","values"]``
* Depending on the value of ``{someArray}``.
*
* Associative array
* -----------------
*
* ::
*
* {f:format.json(value: {foo: 'bar', bar: 'baz'})}
*
* ``{"foo":"bar","bar":"baz"}``
*
* Non associative array with forced object
* ----------------------------------------
*
* ::
*
* {f:format.json(value: {0: 'bar', 1: 'baz'}, forceObject: true)}
*
* ``{"0":"bar","1":"baz"}``
*/
final class JsonViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* @var bool
*/
protected $escapeChildren = false;
public function initializeArguments(): void
{
$this->registerArgument('value', 'mixed', 'The incoming data to convert, or null if VH children should be used');
$this->registerArgument('forceObject', 'bool', 'Outputs an JSON object rather than an array', false, false);
}
/**
* Applies json_encode() on the specified value.
*
* Outputs content with its JSON representation. To prevent issues in HTML context, occurrences
* of greater-than or less-than characters are converted to their hexadecimal representations.
*
* If $forceObject is TRUE a JSON object is outputted even if the value is a non-associative array
* Example: array('foo', 'bar') as input will not be ["foo","bar"] but {"0":"foo","1":"bar"}
*
* @see https://www.php.net/manual/function.json-encode.php
* @return string|false
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$value = $renderChildrenClosure();
$options = JSON_HEX_TAG;
if ($arguments['forceObject'] !== false) {
$options = $options | JSON_FORCE_OBJECT;
}
return json_encode($value, $options);
}
/**
* Explicitly set argument name to be used as content.
*/
public function resolveContentArgumentName(): string
{
return 'value';
}
}