Completed
Pull Request — master (#3)
by Harry
07:29
created

JsonParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 22 3
A decodeJson() 0 9 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\Parser;
15
16
use ArrayIterator;
17
use Graze\DataFile\Format\JsonFormatInterface;
18
use Graze\DataFile\Helper\LineStreamIterator;
19
use Iterator;
20
use League\Csv\Modifier\MapIterator;
21
use Psr\Http\Message\StreamInterface;
22
use RuntimeException;
23
24
class JsonParser implements ParserInterface
25
{
26
    const JSON_DEFAULT_DEPTH = 512;
27
28
    /**
29
     * @var JsonFormatInterface
30
     */
31
    private $format;
32
33
    /**
34
     * JsonFormatter constructor.
35
     *
36
     * @param JsonFormatInterface $format
37
     */
38
    public function __construct(JsonFormatInterface $format)
39
    {
40
        $this->format = $format;
41
    }
42
43
    /**
44
     * @param StreamInterface $stream
45
     *
46
     * @return Iterator
47
     */
48
    public function parse(StreamInterface $stream)
49
    {
50
        if ($this->format->isEachLine()) {
51
            $iterator = new LineStreamIterator(
52
                $stream,
53
                [
54
                    LineStreamIterator::OPTION_ENDING         => "\n",
55
                    LineStreamIterator::OPTION_IGNORE_BLANK   => $this->format->isIgnoreBlankLines(),
56
                    LineStreamIterator::OPTION_INCLUDE_ENDING => false,
57
                ]
58
            );
59
            $iterator->setIgnoreBlank($this->format->isIgnoreBlankLines());
60
            return new MapIterator($iterator, [$this, 'decodeJson']);
61
        } else {
62
            $json = $this->decodeJson($stream->getContents());
63
            if (is_array($json)) {
64
                return new ArrayIterator($json);
65
            } else {
66
                throw new RuntimeException("Expecting a json array to parse, unknown format detected");
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param string $string
73
     *
74
     * @return mixed
75
     */
76
    public function decodeJson($string)
77
    {
78
        return json_decode(
79
            $string,
80
            $this->format->isJsonDecodeAssoc(),
81
            static::JSON_DEFAULT_DEPTH,
82
            $this->format->getJsonDecodeOptions()
83
        );
84
    }
85
}
86