Passed
Push — master ( 5f0e13...194915 )
by Jon
05:19
created

Article::setSubTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
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 $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 getJson()
66
    {
67
        $article = new \stdClass();
68
        $article->articleId = $this->articleId;
69
        $article->defer = $this->defer;
70
        $article->title = $this->getTitle();
71
        $article->subTitle = $this->getSubTitle();
72
        $article->components = $this->getComponents();
73
74
        return json_encode($article);
75
    }
76
77
    public function getComponents($format = null)
78
    {
79
        $output = array();
80
        foreach ($this->components as $component) {
81
            array_push($output, $component->getComponent());
82
        }
83
84
        if ($format === 'json') {
85
            return json_encode($output);
86
        }
87
88
        return $output;
89
    }
90
91
    /**
92
     * @param AbstractComponent $component
93
     * @throws \ErrorException
94
     * @return void
95
     */
96
    public function addComponent($component)
97
    {
98
        if ($component instanceof AbstractComponent) {
0 ignored issues
show
introduced by
$component is always a sub-type of FlatPlan\Components\AbstractComponent.
Loading history...
99
            $component->updateStyles('root');
100
            array_push($this->components, $component);
101
        } else {
102
            throw new \ErrorException('Invalid component supplied.');
103
        }
104
    }
105
}
106