Passed
Push — master ( 194915...1c01ff )
by Jon
03:52 queued 01:31
created

Article::getJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php namespace FlatPlan;
2
3
use FlatPlan\Components\AbstractComponent;
4
5
class Article {
6
7
    private $components = array();
8
    private $defer;
9
    private $articleId;
10
    private $title;
11
    private $subTitle;
12
    private $metaData;
13
    private $allowedMetaKeys = array(
14
        'authors',
15
        'canonicalUrl',
16
        'coverArt',
17
        'dateCreated',
18
        'dateModified',
19
        'datePublished',
20
        'excerpt',
21
        'keywords',
22
        'thumbnailUrl',
23
        'transparentToolbar',
24
        'videoURL'
25
    );
26
27
    public function __construct($articleId, $defer = false)
28
    {
29
        $this->articleId = $articleId;
30
        $this->defer     = $defer;
31
    }
32
33
    public function setTitle($title) {
34
        $this->title = $title;
35
    }
36
37
    public function getTitle()
38
    {
39
        return $this->title;
40
    }
41
42
    public function setSubTitle($subTitle) {
43
        $this->subTitle = $subTitle;
44
    }
45
46
    public function getSubTitle()
47
    {
48
        return $this->subTitle;
49
    }
50
51
    public function setMetaData($metaData)
52
    {
53
        $metaObj = new \stdClass();
54
        if (is_array($metaData)) {
55
            foreach ($metaData as $key => $value) {
56
                if (in_array($key, $this->allowedMetaKeys)) {
57
                    $metaObj->{$key} = $value;
58
                }
59
            }
60
        }
61
62
        $this->metaData = $metaObj;
63
    }
64
65
    public function getMetaData()
66
    {
67
        return $this->metaData;
68
    }
69
70
    public function getJson()
71
    {
72
        $article             = new \stdClass();
73
        $article->articleId  = $this->articleId;
74
        $article->defer      = $this->defer;
75
        $article->title      = $this->getTitle();
76
        $article->subTitle   = $this->getSubTitle();
77
        $article->metaData   = $this->getMetaData();
78
        $article->components = $this->getComponents();
79
80
        return json_encode($article);
81
    }
82
83
    public function getComponents($format = null)
84
    {
85
        $output = array();
86
        foreach ($this->components as $component) {
87
            array_push($output, $component->getComponent());
88
        }
89
90
        if ($format === 'json') {
91
            return json_encode($output);
92
        }
93
94
        return $output;
95
    }
96
97
    /**
98
     * @param AbstractComponent $component
99
     * @throws \ErrorException
100
     * @return void
101
     */
102
    public function addComponent($component)
103
    {
104
        if ($component instanceof AbstractComponent) {
0 ignored issues
show
introduced by
$component is always a sub-type of FlatPlan\Components\AbstractComponent.
Loading history...
105
            $component->updateStyles('root');
106
            array_push($this->components, $component);
107
        } else {
108
            throw new \ErrorException('Invalid component supplied.');
109
        }
110
    }
111
}
112