Passed
Push — master ( 21e189...784e92 )
by Jon
01:55
created

Article::addComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
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 $status;
9
    private $articleId;
10
    private $title;
11
    private $subTitle;
12
    private $language = 'en';
13
    private $category;
14
    private $metaData;
15
    private $allowedMetaKeys = array(
16
        'authors',
17
        'canonicalURL',
18
        'coverArt',
19
        'dateCreated',
20
        'dateModified',
21
        'datePublished',
22
        'excerpt',
23
        'keywords',
24
        'thumbnailURL',
25
        'transparentToolbar',
26
        'videoURL'
27
    );
28
29
    public function __construct($articleId, $status = 'draft')
30
    {
31
        $this->articleId = $articleId;
32
        $this->status    = $status;
33
    }
34
35
    public function setTitle($title) {
36
        $this->title = $title;
37
    }
38
39
    public function getTitle()
40
    {
41
        return $this->title;
42
    }
43
44
    public function setSubTitle($subTitle) {
45
        $this->subTitle = $subTitle;
46
    }
47
48
    public function getSubTitle()
49
    {
50
        return $this->subTitle;
51
    }
52
53
    public function setLanguage($language)
54
    {
55
        $this->language = $language;
56
    }
57
58
    public function getLanguage()
59
    {
60
        return $this->language;
61
    }
62
63
    public function setCategory($category)
64
    {
65
        $this->category = $category;
66
    }
67
68
    public function getCategory()
69
    {
70
        return $this->category;
71
    }
72
73
    public function setMetaData($metaData)
74
    {
75
        $metaObj = new \stdClass();
76
        if (is_array($metaData)) {
77
            foreach ($metaData as $key => $value) {
78
                if (in_array($key, $this->allowedMetaKeys)) {
79
                    $metaObj->{$key} = $value;
80
                }
81
            }
82
        }
83
84
        $this->metaData = $metaObj;
85
    }
86
87
    public function getMetaData()
88
    {
89
        return $this->metaData;
90
    }
91
92
    public function getJson()
93
    {
94
        $article             = new \stdClass();
95
        $article->articleId  = $this->articleId;
96
        $article->language   = $this->getLanguage();
97
        $article->status     = $this->status;
98
        $article->title      = $this->getTitle();
99
        $article->subTitle   = $this->getSubTitle();
100
        $article->category   = $this->getCategory();
101
        $article->metaData   = $this->getMetaData();
102
        $article->components = $this->getComponents();
103
104
        return json_encode($article);
105
    }
106
107
    public function getComponents($format = null)
108
    {
109
        $output = array();
110
        foreach ($this->components as $component) {
111
            array_push($output, $component->getComponent());
112
        }
113
114
        if ($format === 'json') {
115
            return json_encode($output);
116
        }
117
118
        return $output;
119
    }
120
121
    /**
122
     * @param AbstractComponent $component
123
     * @throws \ErrorException
124
     * @return void
125
     */
126
    public function setComponents($components)
127
    {
128
        if (is_array($components)) {
129
            foreach ($components as $component) {
130
                if ($component instanceof AbstractComponent) {
131
                    $component->updateStyles('root');
132
                    array_push($this->components, $component);
133
                }
134
            }
135
        } else if ($components instanceof AbstractComponent) {
136
            $components->updateStyles('root');
137
            array_push($this->components, $components);
138
        } else {
139
            throw new \ErrorException('Invalid component supplied.');
140
        }
141
    }
142
}
143