JsonParser::parseItems()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Parser;
12
13
use FeedIo\Feed\Item;
14
use FeedIo\Feed\Item\Author;
15
use FeedIo\FeedInterface;
16
use FeedIo\ParserAbstract;
17
use FeedIo\Reader\Document;
18
19
class JsonParser extends ParserAbstract
20
{
21
22
    /**
23
     * @param Document $document
24
     * @param FeedInterface $feed
25
     * @return FeedInterface
26
     */
27 1
    public function parseContent(Document $document, FeedInterface $feed) : FeedInterface
28
    {
29 1
        $data = $document->getJsonAsArray();
30 1
        $feed->setTitle($this->readOffset($data, 'title'));
31 1
        $feed->setDescription($this->readOffset($data, 'description'));
32 1
        $feed->setLink($this->readOffset($data, 'feed_url'));
33 1
        $feed->setUrl($this->readOffset($data, 'home_page_url'));
34 1
        $feed->setLogo($this->readOffset($data, 'icon'));
35
36 1
        if (array_key_exists('items', $data)) {
37 1
            $this->parseItems($data['items'], $feed);
38
        }
39
40 1
        return $feed;
41
    }
42
43
    /**
44
     * @param Document $document
45
     * @param iterable $mandatoryFields
46
     * @throws MissingFieldsException
47
     * @return bool
48
     */
49 1
    public function checkBodyStructure(Document $document, iterable $mandatoryFields) : bool
50
    {
51 1
        $data = $document->getJsonAsArray();
52
53 1
        foreach ($mandatoryFields as $mandatoryField) {
54 1
            if (! array_key_exists($mandatoryField, $data)) {
55
                throw new MissingFieldsException("Missing {$mandatoryField} in the JSON Feed");
56
            }
57
        }
58
59 1
        return true;
60
    }
61
62
    /**
63
     * @param iterable $items
64
     * @param FeedInterface $feed
65
     * @return JsonParser
66
     */
67 1
    public function parseItems(iterable $items, FeedInterface $feed) : JsonParser
68
    {
69 1
        foreach ($items as $dataItem) {
70 1
            $item = new Item();
71 1
            $item->setPublicId($this->readOffset($dataItem, 'id'));
72 1
            $item->setTitle($this->readOffset($dataItem, 'title'));
73 1
            $item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published')));
74 1
            $contentHtml = $this->readOffset($dataItem, 'content_html');
75 1
            $item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
76 1
            $item->setLink($this->readOffset($dataItem, 'url'));
77
78 1
            if (array_key_exists('author', $dataItem)) {
79 1
                $authorItem = $dataItem['author'];
80 1
                $author = new Author();
81 1
                $author->setName($this->readOffset($authorItem, 'name'));
82 1
                $author->setUri($this->readOffset($authorItem, 'url'));
83 1
                $author->setEmail($this->readOffset($authorItem, 'email'));
84 1
                $item->setAuthor($author);
85
            }
86 1
            $feed->add($item);
87
        }
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * @param array $data
94
     * @param string $offsetName
95
     * @param string|null $default
96
     * @return null|string
97
     */
98 1
    public function readOffset(array $data, string $offsetName, string $default = null) : ? string
99
    {
100 1
        if (array_key_exists($offsetName, $data)) {
101 1
            return $data[$offsetName];
102
        }
103
104 1
        return $default;
105
    }
106
}
107