Json   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 54
ccs 15
cts 16
cp 0.9375
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A makePrintable() 0 17 4
1
<?php
2
/**
3
 * Json.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained on one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
18
namespace Pvra\Result\ResultFormatter;
19
20
use Pvra\Result\Collection as ResultCollection;
21
use Pvra\Result\Exceptions\ResultFileWriterException;
22
23
/**
24
 * Class Json
25
 *
26
 * @package Pvra\Result\ResultFormatter
27
 */
28
class Json implements ResultFormatter
29
{
30
    /**
31
     * @var int Bitmask of options to be passed to `json_encode`
32
     */
33
    private $options = 0;
34
    /**
35
     * @var int Depth to be passed to the third parameter of `json_encode`  Only used on php >= 5.5
36
     */
37
    private $depth = 512;
38
39
    /**
40
     * Supported option keys:
41
     *
42
     * * 'options': Json encoding options
43
     * * 'depth': Maximum depths
44
     *
45
     * @param array $options
46
     */
47 16
    public function __construct(array $options = [])
48
    {
49 16
        $this->options = isset($options['options']) ? $options['options'] : 0;
50 16
        $this->depth = isset($options['depth']) ? $options['depth'] : 512;
51 16
    }
52
53
    /**
54
     * Generate a json representation of a `Result\Collection`
55
     *
56
     * The output is the json representation based on the array returned by
57
     * `Pvra\Result\Collection::jsonSerialize()`.
58
     *
59
     * @param \Pvra\Result\Collection $collection The source collection
60
     * @return string The returned json string
61
     * @throws \Pvra\Result\Exceptions\ResultFileWriterException Thrown if json generation failed
62
     * @see \Pvra\Result\Collection::jsonSerialize() Data source
63
     */
64 12
    public function makePrintable(ResultCollection $collection)
65
    {
66 12
        if (PHP_VERSION_ID >= 50500) {
67 12
            $json = json_encode($collection, $this->options, $this->depth);
68 6
        } else {
69
            $json = json_encode($collection, $this->options);
70
        }
71 12
        if (json_last_error() !== 0) {
72 4
            $msg = 'Json Encoding failed with error: ' . json_last_error();
73 4
            if (PHP_VERSION_ID >= 50500) {
74 4
                $msg .= ': ' . json_last_error_msg();
75 2
            }
76 4
            throw new ResultFileWriterException($msg);
77
        }
78
79 8
        return $json;
80
    }
81
}
82