JsonFormat::isSingleBlock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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;
15
16
use Graze\DataFile\Helper\GetOptionTrait;
17
18
class JsonFormat implements FormatInterface, JsonFormatInterface
19
{
20
    use GetOptionTrait;
21
22
    const OPTION_FILE_TYPE          = 'fileType';
23
    const OPTION_ENCODE_OPTIONS     = 'encodeOptions';
24
    const OPTION_DECODE_OPTIONS     = 'decodeOptions';
25
    const OPTION_DECODE_ASSOC       = 'decodeAssoc';
26
    const OPTION_IGNORE_BLANK_LINES = 'ignoreBlankLines';
27
28
    const DEFAULT_TYPE               = JsonFormatInterface::JSON_FILE_TYPE_SINGLE_BLOCK;
29
    const DEFAULT_ENCODE_OPTIONS     = 0;
30
    const DEFAULT_DECODE_OPTIONS     = 0;
31
    const DEFAULT_DECODE_ASSOC       = false;
32
    const DEFAULT_IGNORE_BLANK_LINES = true;
33
34
    /**
35
     * The type of the file
36
     *
37
     * @var int
38
     */
39
    private $fileType = JsonFormatInterface::JSON_FILE_TYPE_SINGLE_BLOCK;
40
    /** @var int */
41
    private $encodeOptions = 0;
42
    /** @var int */
43
    private $decodeOptions = 0;
44
    /** @var bool */
45
    private $decodeToAssoc = false;
46
    /** @var bool */
47
    private $ignoreBlankLines;
48
49
    /**
50
     * @param array $options -fileType <int> (Default: JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK) a
51
     *                       JsonFormat::JSON_FILE_TYPE_*
52
     *                       -encodeOptions <int> Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP,
53
     *                       JSON_HEX_APOS, JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK, JSON_UNESCAPED_SLASHES,
54
     *                       JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE,
55
     *                       JSON_PARTIAL_OUTPUT_ON_ERROR. The behaviour of these constants is described on the JSON
56
     *                       constants page.
57
     *                       -decodeOptions <int> Bitmask of JSON decode options. Currently only
58
     *                       JSON_BIGINT_AS_STRING
59
     *                       is supported (default is to cast large integers as floats)
60
     */
61 7
    public function __construct(array $options = [])
62
    {
63 7
        $this->options = $options;
64 7
        $this->fileType = $this->getOption(static::OPTION_FILE_TYPE, static::DEFAULT_TYPE);
65 7
        $this->encodeOptions = $this->getOption(static::OPTION_ENCODE_OPTIONS, static::DEFAULT_ENCODE_OPTIONS);
66 7
        $this->decodeOptions = $this->getOption(static::OPTION_DECODE_OPTIONS, static::DEFAULT_DECODE_OPTIONS);
67 7
        $this->decodeToAssoc = $this->getOption(static::OPTION_DECODE_ASSOC, static::DEFAULT_DECODE_ASSOC);
68 7
        $this->ignoreBlankLines = $this->getOption(
69 7
            static::OPTION_IGNORE_BLANK_LINES,
70 7
            static::DEFAULT_IGNORE_BLANK_LINES
71
        );
72 7
    }
73
74
    /**
75
     * Type type of file format
76
     *
77
     * @return string
78
     */
79 3
    public function getType()
80
    {
81 3
        return 'json';
82
    }
83
84
    /**
85
     * Each line is an individual json blob
86
     *
87
     * @return bool
88
     */
89 13
    public function isEachLine()
90
    {
91 13
        return $this->fileType === JsonFormatInterface::JSON_FILE_TYPE_EACH_LINE;
92
    }
93
94
    /**
95
     * The whole file is a single json blob
96
     *
97
     * @return bool
98
     */
99 6
    public function isSingleBlock()
100
    {
101 6
        return $this->fileType === JsonFormatInterface::JSON_FILE_TYPE_SINGLE_BLOCK;
102
    }
103
104
    /**
105
     * @param int $fileType
106
     *
107
     * @return $this
108
     */
109 1
    public function setJsonFileType($fileType)
110
    {
111 1
        $this->fileType = $fileType;
112 1
        return $this;
113
    }
114
115
    /**
116
     * Get the type of json file ::JSON_FILE_TYPE_*
117
     *
118
     * @return int static::JSON_FILE_TYPE_*
119
     */
120 3
    public function getJsonFileType()
121
    {
122 3
        return $this->fileType;
123
    }
124
125
    /**
126
     * Set the encoding options.
127
     *
128
     * @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,
129
     *                     JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT,
130
     *                     JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR. The
131
     *                     behaviour of these constants is described on the JSON constants page.
132
     *
133
     * @link http://php.net/manual/en/json.constants.php
134
     *
135
     * @return $this
136
     */
137 1
    public function setJsonEncodeOptions($options)
138
    {
139 1
        $this->encodeOptions = $options;
140 1
        return $this;
141
    }
142
143
    /**
144
     * Get the Json Encode Options
145
     *
146
     * Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
147
     * JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
148
     * JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
149
     *
150
     * The behaviour of these constants is described on the JSON constants page.
151
     *
152
     * @link http://php.net/manual/en/json.constants.php
153
     *
154
     * @return int
155
     */
156 9
    public function getJsonEncodeOptions()
157
    {
158 9
        return $this->encodeOptions;
159
    }
160
161
    /**
162
     * @param int $options Bitmask of JSON decode options. Currently only
163
     *                     JSON_BIGINT_AS_STRING
164
     *                     is supported (default is to cast large integers as floats)
165
     *
166
     * @return $this
167
     */
168 1
    public function setJsonDecodeOptions($options)
169
    {
170 1
        $this->decodeOptions = $options;
171 1
        return $this;
172
    }
173
174
    /**
175
     * Bitmask of JSON decode options. Currently only
176
     * JSON_BIGINT_AS_STRING
177
     * is supported (default is to cast large integers as floats)
178
     *
179
     * @return mixed
180
     */
181 9
    public function getJsonDecodeOptions()
182
    {
183 9
        return $this->decodeOptions;
184
    }
185
186
    /**
187
     * @param bool $assoc
188
     *
189
     * @return $this
190
     */
191 1
    public function setJsonDecodeAssoc($assoc)
192
    {
193 1
        $this->decodeToAssoc = $assoc;
194 1
        return $this;
195
    }
196
197
    /**
198
     * @return bool
199
     */
200 9
    public function isJsonDecodeAssoc()
201
    {
202 9
        return $this->decodeToAssoc;
203
    }
204
205
    /**
206
     * @param bool $ignore
207
     *
208
     * @return $this
209
     */
210 1
    public function setIgnoreBlankLines($ignore)
211
    {
212 1
        $this->ignoreBlankLines = $ignore;
213 1
        return $this;
214
    }
215
216
    /**
217
     * @return bool
218
     */
219 7
    public function isIgnoreBlankLines()
220
    {
221 7
        return $this->ignoreBlankLines;
222
    }
223
}
224