Passed
Push — master ( 43a084...7e18cf )
by Jon
01:56
created

AbstractComponent::setBehaviour()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 11
rs 9.9
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
    protected $behaviour         = null;
12
    protected $behaviours        = ['motion', 'background_motion', 'parallax', 'background_parallax', 'springy'];
13
14
    abstract public function getComponent();
15
16
    public function setRole($role)
17
    {
18
        if (!in_array($role, $this->roles)) {
19
            throw new \ErrorException('Invalid role supplied.');
20
        }
21
        $this->role = $role;
22
        $this->updateStyles($role);
23
    }
24
25
    public function getRole()
26
    {
27
        return $this->role;
28
    }
29
30
    public function setIdentifier($identifier)
31
    {
32
        $this->identifier        = $identifier;
33
        $this->currentIdentifier = $identifier;
34
    }
35
36
    public function getIdentifier()
37
    {
38
        return $this->identifier;
39
    }
40
41
    public function setBehaviour($type, $factor = null)
42
    {
43
        if (!in_array($type, $this->behaviours)) {
44
            throw new \ErrorException('Invalid behaviour supplied.');
45
        }
46
        $behaviour = new \stdClass();
47
        $behaviour->type = $type;
48
        if (!is_null($factor)  && $type === 'parallax') {
49
            $behaviour->factor = (float) $factor;
50
        }
51
        $this->behaviour = $behaviour;
52
    }
53
54
    public function getBehaviour()
55
    {
56
        return $this->behaviour;
57
    }
58
59
    public function updateStyles($role)
60
    {
61
        if ($this->currentIdentifier !== null && strpos($this->layout, $this->currentIdentifier)) {
62
            $this->layout    = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
63
            $this->style     = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
64
            $this->textStyle = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
65
        }
66
67
        $this->layout    = $role . ucfirst($this->layout);
68
        $this->style     = $role . ucfirst($this->style);
69
        $this->textStyle = $role . ucfirst($this->textStyle);
70
71
        if ($this->currentIdentifier !== null) {
72
            $this->layout    .= '-' . $this->currentIdentifier;
73
            $this->style     .= '-' . $this->currentIdentifier;
74
            $this->textStyle .= '-' . $this->currentIdentifier;
75
        }
76
77
        if (isset($this->components)) {
78
            foreach ($this->components as $component) {
79
                if ($this->currentIdentifier !== null && $component->identifier === null) {
80
                    $component->currentIdentifier = $this->currentIdentifier;
81
                }
82
                $component->updateStyles($role);
83
            }
84
        }
85
    }
86
87
    public function __toString()
88
    {
89
        return json_encode($this->getComponent());
90
    }
91
}
92