Completed
Pull Request — master (#3)
by Harry
03:17
created

JsonFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 92
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A format() 0 6 1
A getInitialBlock() 0 8 2
A getClosingBlock() 0 8 2
A getRowSeparator() 0 4 1
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
20
class JsonFormatter implements FormatterInterface
21
{
22
    const JSON_ARRAY_START     = '[';
23
    const JSON_ARRAY_END       = ']' . PHP_EOL;
24
    const JSON_ARRAY_SEPARATOR = ',' . PHP_EOL;
25
26
    use RowProcessor;
27
    use InvokeFormatter;
28
29
    /**
30
     * @var JsonFormatInterface
31
     */
32
    private $format;
33
34
    /**
35
     * @var string
36
     */
37
    private $linePostfix;
38
39
    /**
40
     * @var int
41
     */
42
    private $encodeOptions;
43
44
    /**
45
     * JsonFormatter constructor.
46
     *
47
     * @param JsonFormatInterface $format
48
     */
49 5
    public function __construct(JsonFormatInterface $format)
50
    {
51 5
        $this->addProcessor(new DateTimeProcessor());
52 5
        $this->format = $format;
53 5
        $this->linePostfix = $format->isSingleBlock() ? static::JSON_ARRAY_SEPARATOR : PHP_EOL;
54 5
        $this->encodeOptions = $format->getJsonEncodeOptions();
55
56
        // If the json is a blob on each line, turn off pretty print to ensure it is all on a single line
57 5
        if ($format->isEachLine()) {
58 3
            $this->encodeOptions &= ~JSON_PRETTY_PRINT;
59 3
        }
60 5
    }
61
62
    /**
63
     * @param array $row
64
     *
65
     * @return string
66
     */
67 4
    public function format(array $row)
68
    {
69 4
        $row = $this->process($row);
70
71 4
        return json_encode($row, $this->encodeOptions);
72
    }
73
74
    /**
75
     * Return an initial block if required
76
     *
77
     * @return string
78
     */
79 4
    public function getInitialBlock()
80
    {
81 4
        if ($this->format->isSingleBlock()) {
82 2
            return static::JSON_ARRAY_START;
83
        } else {
84 2
            return '';
85
        }
86
    }
87
88
    /**
89
     * Return a closing block if required
90
     *
91
     * @return string
92
     */
93 4
    public function getClosingBlock()
94
    {
95 4
        if ($this->format->isSingleBlock()) {
96 2
            return static::JSON_ARRAY_END;
97
        } else {
98 2
            return '';
99
        }
100
    }
101
102
    /**
103
     * Get a separator between each row
104
     *
105
     * @return string
106
     */
107 4
    public function getRowSeparator()
108
    {
109 4
        return $this->linePostfix;
110
    }
111
}
112