JsonFormatter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 0
loc 94
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 7 1
A getInitialBlock() 0 8 2
A getClosingBlock() 0 8 2
A getRowSeparator() 0 4 1
A __construct() 0 12 3
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Formatter;
15
16
use Graze\DataFile\Format\JsonFormatInterface;
17
use Graze\DataFile\Format\Processor\DateTimeProcessor;
18
use Graze\DataFile\Format\Processor\RowProcessor;
19
use Traversable;
20
21
class JsonFormatter implements FormatterInterface
22
{
23
    use RowProcessor;
24
    use InvokeFormatter;
25
    use TraversableTrait;
26
27
    const JSON_ARRAY_START     = '[';
28
    const JSON_ARRAY_END       = ']' . PHP_EOL;
29
    const JSON_ARRAY_SEPARATOR = ',' . PHP_EOL;
30
31
    /**
32
     * @var JsonFormatInterface
33
     */
34
    private $format;
35
36
    /**
37
     * @var string
38
     */
39
    private $linePostfix;
40
41
    /**
42
     * @var int
43
     */
44
    private $encodeOptions;
45
46
    /**
47
     * JsonFormatter constructor.
48
     *
49
     * @param JsonFormatInterface $format
50
     */
51 7
    public function __construct(JsonFormatInterface $format)
52
    {
53 7
        $this->addProcessor(new DateTimeProcessor());
54 7
        $this->format = $format;
55 7
        $this->linePostfix = $format->isSingleBlock() ? static::JSON_ARRAY_SEPARATOR : PHP_EOL;
56 7
        $this->encodeOptions = $format->getJsonEncodeOptions();
57
58
        // If the json is a blob on each line, turn off pretty print to ensure it is all on a single line
59 7
        if ($format->isEachLine()) {
60 4
            $this->encodeOptions &= ~JSON_PRETTY_PRINT;
61
        }
62 7
    }
63
64
    /**
65
     * @param array|Traversable $row
66
     *
67
     * @return string
68
     */
69 6
    public function format($row)
70
    {
71 6
        $data = $this->getArray($row);
72 5
        $data = $this->process($data);
73
74 5
        return json_encode($data, $this->encodeOptions);
75
    }
76
77
    /**
78
     * Return an initial block if required
79
     *
80
     * @return string
81
     */
82 5
    public function getInitialBlock()
83
    {
84 5
        if ($this->format->isSingleBlock()) {
85 2
            return static::JSON_ARRAY_START;
86
        } else {
87 3
            return '';
88
        }
89
    }
90
91
    /**
92
     * Return a closing block if required
93
     *
94
     * @return string
95
     */
96 5
    public function getClosingBlock()
97
    {
98 5
        if ($this->format->isSingleBlock()) {
99 2
            return static::JSON_ARRAY_END;
100
        } else {
101 3
            return '';
102
        }
103
    }
104
105
    /**
106
     * Get a separator between each row
107
     *
108
     * @return string
109
     */
110 5
    public function getRowSeparator()
111
    {
112 5
        return $this->linePostfix;
113
    }
114
}
115