Completed
Push — master ( a775f9...497487 )
by Alex
07:59
created

JsonParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseContent() 0 14 2
A checkBodyStructure() 0 12 3
A parseItems() 0 14 2
A readOffset() 0 8 2
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
    public function parseContent(Document $document, FeedInterface $feed)
28
    {
29
        $data = $document->getJsonAsArray();
30
        $feed->setTitle($this->readOffset($data, 'title'));
31
        $feed->setDescription($this->readOffset($data, 'description'));
32
        $feed->setLink($this->readOffset($data, 'feed_url'));
33
        $feed->setUrl($this->readOffset($data, 'home_page_url'));
34
35
        if ( array_key_exists('items', $data) ) {
36
            $this->parseItems($data['items'], $feed);
37
        }
38
39
        return $feed;
40
    }
41
42
    /**
43
     * @param Document $document
44
     * @param array $mandatoryFields
45
     * @return $this
46
     * @throws MissingFieldsException
47
     */
48
    public function checkBodyStructure(Document $document, array $mandatoryFields)
49
    {
50
        $data = $document->getJsonAsArray();
51
52
        foreach($mandatoryFields as $mandatoryField) {
53
            if ( ! array_key_exists($mandatoryField, $data) ) {
54
                throw new MissingFieldsException("Missing {$mandatoryField} in the JSON Feed");
55
            }
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param array $items
63
     * @param FeedInterface $feed
64
     * @return $this
65
     */
66
    public function parseItems(array $items, FeedInterface $feed)
67
    {
68
        foreach( $items as $dataItem ) {
69
            $item = new Item();
70
            $item->setPublicId($this->readOffset($dataItem, 'id'));
71
            $item->setTitle($this->readOffset($dataItem, 'title'));
72
            $item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published')));
73
            $contentHtml = $this->readOffset($dataItem, 'content_html');
74
            $item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
75
            $feed->add($item);
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param array $data
83
     * @param string $offsetName
84
     * @param mixed $default
85
     * @return mixed
86
     */
87
    public function readOffset(array $data, $offsetName, $default = null)
88
    {
89
        if (array_key_exists($offsetName, $data) ){
90
            return $data[$offsetName];
91
        }
92
93
        return $default;
94
    }
95
}
96