Passed
Push — master ( 3e8cff...b55e8c )
by Jon
02:15
created

Article::setCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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