Completed
Push — master ( cf8ab0...30f4d3 )
by Jérémy
02:16
created

Reader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JDecool\JsonFeed\Reader;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
class Reader
9
{
10
    /** @var ReaderInterface[] */
11
    private $readers;
12
13
    /**
14
     * Constructor
15
     *
16
     * @param ReaderInterface[] $readers
17
     */
18
    public function __construct(array $readers)
19
    {
20
        $this->readers = $readers;
21
    }
22
23
    /**
24
     * Read feed from JSON
25
     *
26
     * @param string $json
27
     * @return \JDecool\JsonFeed\Feed
28
     */
29
    public function createFromJson($json)
30
    {
31
        $content = json_decode($json, true);
32
        if (!is_array($content)) {
33
            throw new InvalidArgumentException('Invalid JSONFeed string');
34
        }
35
36
        if (!isset($content['version'])) {
37
            throw new RuntimeException('JSONFeed version is not defined');
38
        }
39
40
        if (!isset($this->readers[$content['version']])) {
41
            throw new RuntimeException(sprintf('No reader for version "%s"', $content['version']));
42
        }
43
44
        return $this->readers[$content['version']]->readFromJson($json);
45
    }
46
}
47