Passed
Push — master ( 3bf788...763f00 )
by Jon
02:39 queued 10s
created

Structure::__construct()   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\Components;
2
3
class Structure extends AbstractComponent {
4
5
    private $role;
6
    private $components;
7
8
    protected $roles = ['aside', 'chapter', 'container', 'divider', 'header'];
9
10
    /**
11
     * @param string $role
12
     * @return void
13
     */
14
    public function __construct($role)
15
    {
16
        $this->setRole($role);
17
    }
18
19
    public function setRole($role)
20
    {
21
        if (!in_array($role, $this->roles)) {
22
            throw new \ErrorException('Invalid role supplied.');
23
        }
24
        $this->role = $role;
25
    }
26
27
    public function getRole()
28
    {
29
        return $this->role;
30
    }
31
32
    public function setComponents($components)
33
    {
34
        if (is_array($components)) {
35
            foreach ($components as $component) {
36
                if ($component instanceof AbstractComponent) {
37
                    array_push($this->components, $component);
38
                }
39
            }
40
        } else if ($components instanceof AbstractComponent) {
41
            array_push($this->components, $components);
42
        }
43
    }
44
45
    public function getComponents()
46
    {
47
        return $this->components;
48
    }
49
50
    public function getComponent()
51
    {
52
        $component = new \stdClass();
53
        $component->role       = $this->getRole();
54
        $component->components = $this->getComponents();
55
        return $component;
56
    }
57
58
    public function __toString()
59
    {
60
        return json_encode($this->getComponent());
61
    }
62
}
63