Json::encode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
crap 20
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Output;
14
15
class Json extends AbstractOutput
16
{
17
18
    /**
19
     * {@inheritdoc}
20
     * @see http://www.ietf.org/rfc/rfc4627.txt
21
     */
22
    protected $content_type = 'application/json';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function encode(array $data, $rootNode=null)
28
    {
29
        // Encode <, >, ', &, and " for RFC4627-compliant JSON.
30
        $options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
31
32
        // @codeCoverageIgnoreStart
33
        if (!defined('UNIT_TEST') && version_compare(PHP_VERSION, '5.4.0') >= 0) {
34
            $options = $options | JSON_PRETTY_PRINT;
35
        }
36
        // @codeCoverageIgnoreEnd
37
38
        if (null !== $rootNode) {
39
            $data = array($rootNode => $data);
40
        }
41
42
        return json_encode($data, $options);
43
    }
44
45
}
46