Completed
Push — master ( af3067...26e39e )
by ignace nyamagana
04:48 queued 02:45
created

JsonConverter::convert()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
cc 4
eloc 10
nc 6
nop 1
ccs 10
cts 10
cp 1
crap 4
rs 9.2
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use League\Csv\Exception\RuntimeException;
18
use Traversable;
19
20
/**
21
 * A class to convert CSV records into a DOMDOcument object
22
 *
23
 * @package League.csv
24
 * @since   9.0.0
25
 * @author  Ignace Nyamagana Butera <[email protected]>
26
 */
27
class JsonConverter implements ConverterInterface
28
{
29
    use ConverterTrait;
30
31
    /**
32
     * Tell whether to preserve offset for json
33
     *
34
     * @var bool
35
     */
36
    protected $preserve_record_offset = false;
37
38
    /**
39
     * json_encode options
40
     *
41
     * @var int
42
     */
43
    protected $options = 0;
44
45
    /**
46
     * json_encode depth
47
     *
48
     * @var int
49
     */
50
    protected $depth = 512;
51
52
    /**
53
     * Whether we should preserve the CSV document record offset.
54
     *
55
     * If set to true CSV document record offset will added to
56
     * method output where it makes sense.
57
     *
58
     * @param bool $status
59
     *
60
     * @return static
61
     */
62 2
    public function preserveRecordOffset(bool $status)
63
    {
64 2
        $clone = clone $this;
65 2
        $clone->preserve_record_offset = $status;
66
67 2
        return $clone;
68
    }
69
70
    /**
71
     * Json encode Options
72
     *
73
     * @param int $options
74
     * @param int $depth
75
     *
76
     * @return self
77
     */
78 2
    public function options(int $options = 0, int $depth = 512): self
79
    {
80 2
        $clone = clone $this;
81 2
        $clone->options = $options;
82 2
        $clone->depth = $depth;
83
84 2
        return $clone;
85
    }
86
87
    /**
88
     * Convert an Record collection into a Json string
89
     *
90
     * @param array|Traversable $records the CSV records collection
91
     *
92
     * @return string
93
     */
94 4
    public function convert($records)
95
    {
96 4
        $records = $this->convertToUtf8($this->filterIterable($records, __METHOD__));
97 4
        if (!is_array($records)) {
98 4
            $records = iterator_to_array($records, $this->preserve_record_offset);
99 2
        } elseif (!$this->preserve_record_offset) {
100 2
            $records = array_values($records);
101
        }
102
103 4
        $json = json_encode($records, $this->options, $this->depth);
104 4
        if (JSON_ERROR_NONE === json_last_error()) {
105 2
            return $json;
106
        }
107
108 2
        throw new RuntimeException(json_last_error_msg());
109
    }
110
}
111