Passed
Push — master ( b163d5...3e4260 )
by Jon
01:43
created

AbstractComponent::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace FlatPlan\Components;
2
3
abstract class AbstractComponent {
4
5
    protected $role;
6
    protected $identifier = null;
7
    protected $currentIdentifier = null;
8
    protected $layout     = 'layout';
9
    protected $style      = 'style';
10
    protected $textStyle  = 'textStyle';
11
12
    abstract public function getComponent();
13
14
    public function setRole($role)
15
    {
16
        if (!in_array($role, $this->roles)) {
17
            throw new \ErrorException('Invalid role supplied.');
18
        }
19
        $this->role = $role;
20
        $this->updateStyles($role);
21
    }
22
23
    public function getRole()
24
    {
25
        return $this->role;
26
    }
27
28
    public function setIdentifier($identifier)
29
    {
30
        $this->identifier        = $identifier;
31
        $this->currentIdentifier = $identifier;
32
    }
33
34
    public function getIdentifier()
35
    {
36
        return $this->identifier;
37
    }
38
39
    public function updateStyles($role)
40
    {
41
        if ($this->currentIdentifier !== null && strpos($this->layout, $this->currentIdentifier)) {
42
            $this->layout    = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
43
            $this->style     = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
44
            $this->textStyle = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
45
        }
46
47
        $this->layout    = $role . ucfirst($this->layout);
48
        $this->style     = $role . ucfirst($this->style);
49
        $this->textStyle = $role . ucfirst($this->textStyle);
50
51
        if ($this->currentIdentifier !== null) {
52
            $this->layout    .= '-' . $this->currentIdentifier;
53
            $this->style     .= '-' . $this->currentIdentifier;
54
            $this->textStyle .= '-' . $this->currentIdentifier;
55
        }
56
57
        if (isset($this->components)) {
58
            foreach ($this->components as $component) {
59
                if ($this->currentIdentifier !== null && $component->identifier === null) {
60
                    $component->currentIdentifier = $this->currentIdentifier;
61
                }
62
                $component->updateStyles($role);
63
            }
64
        }
65
    }
66
67
    public function __toString()
68
    {
69
        return json_encode($this->getComponent());
70
    }
71
}
72