Passed
Push — master ( 435fee...0527d3 )
by Ralf
04:23
created

JsonViewHelper::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace EWW\Dpf\ViewHelpers\Format;
3
4
// backport of TYPO3 8.7!
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It originated from the Neos.Form package (www.neos.io)
10
 *
11
 * It is free software; you can redistribute it and/or modify it under
12
 * the terms of the GNU General Public License, either version 2
13
 * of the License, or any later version.
14
 *
15
 * For the full copyright and license information, please read the
16
 * LICENSE.txt file that was distributed with this source code.
17
 *
18
 * The TYPO3 project - inspiring people to share!
19
 */
20
21
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
 */
51
class JsonViewHelper extends AbstractViewHelper
52
{
53
    /**
54
     * Applies json_encode() on the specified value.
55
     *
56
     * Outputs content with its JSON representation. To prevent issues in HTML context, occurrences
57
     * of greater-than or less-than characters are converted to their hexadecimal representations.
58
     *
59
     * If $forceObject is TRUE a JSON object is outputted even if the value is a non-associative array
60
     * Example: array('foo', 'bar') as input will not be ["foo","bar"] but {"0":"foo","1":"bar"}
61
     *
62
     * @param array $value
63
     * @param boolean $forceObject
64
     * @see http://www.php.net/manual/en/function.json-encode.php
65
     * @return string
66
     */
67
    public static function render(array $value, $forceObject = false)
68
    {
69
        $options = JSON_HEX_TAG;
70
        if ($forceObject !== false) {
71
            $options = $options | JSON_FORCE_OBJECT;
72
        }
73
        return json_encode($value, $options);
74
    }
75
}
76