Completed
Pull Request — master (#97)
by Alex
05:49 queued 03:13
created

JsonParser::checkBodyStructure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3.0261
1
<?php
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
14
use FeedIo\Feed\Item;
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)
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
35 1
        if ( array_key_exists('items', $data) ) {
36 1
            $this->parseItems($data['items'], $feed);
37 1
        }
38
39 1
        return $feed;
40
    }
41
42
    /**
43
     * @param Document $document
44
     * @param array $mandatoryFields
45
     * @return $this
46
     * @throws MissingFieldsException
47
     */
48 1
    public function checkBodyStructure(Document $document, array $mandatoryFields)
49
    {
50 1
        $data = $document->getJsonAsArray();
51
52 1
        foreach($mandatoryFields as $mandatoryField) {
53 1
            if ( ! array_key_exists($mandatoryField, $data) ) {
54
                throw new MissingFieldsException("Missing {$mandatoryField} in the JSON Feed");
55
            }
56 1
        }
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @param array $items
63
     * @param FeedInterface $feed
64
     * @return $this
65
     */
66 1
    public function parseItems(array $items, FeedInterface $feed)
67
    {
68 1
        foreach( $items as $dataItem ) {
69 1
            $item = new Item();
70 1
            $item->setPublicId($this->readOffset($dataItem, 'id'));
71 1
            $item->setTitle($this->readOffset($dataItem, 'title'));
72 1
            $item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published')));
73 1
            $contentHtml = $this->readOffset($dataItem, 'content_html');
74 1
            $item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
75 1
            $feed->add($item);
76 1
        }
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @param array $data
83
     * @param string $offsetName
84
     * @param mixed $default
85
     * @return mixed
86
     */
87 1
    public function readOffset(array $data, $offsetName, $default = null)
88
    {
89 1
        if (array_key_exists($offsetName, $data) ){
90 1
            return $data[$offsetName];
91
        }
92
93 1
        return $default;
94
    }
95
}
96