Json::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Formater\Details;
11
12
use Hal\Application\Formater\FormaterInterface;
13
use Hal\Component\Result\ResultCollection;
14
15
/**
16
 * json formatting class for metrics result
17
 *
18
 * @package Hal\Application\Formater\Summary
19
 * @author marc aschmann <[email protected]>
20
 */
21
class Json implements FormaterInterface {
22
23
    /**
24
     * @var boolean
25
     */
26
    private $prettyPrint;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param boolean $prettyPrint optional pretty printing for result json
32
     */
33
    public function __construct($prettyPrint = false)
34
    {
35
        $this->prettyPrint = $prettyPrint;
36
    }
37
38
    /**
39
     * Terminate process
40
     *
41
     * @param ResultCollection $collection
42
     * @param ResultCollection $groupedResults
43
     * @return string
44
     */
45
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults)
46
    {
47
        // use pretty print for readability if according php version given
48
        if ($this->prettyPrint && version_compare(PHP_VERSION, '5.4.0') >= 0) {
49
            return json_encode($collection->asArray(), JSON_PRETTY_PRINT);
50
        } else {
51
            return json_encode($collection->asArray());
52
        }
53
    }
54
55
    /**
56
     * Get name of formatter
57
     *
58
     * @return string
59
     */
60
    public function getName()
61
    {
62
        return 'JSON';
63
    }
64
}
65