Passed
Push — master ( 4c49c9...13c815 )
by Jon
03:07
created

AbstractComponent::setTextStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
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 $customStyle       = null;
11
    protected $textStyle         = 'textStyle';
12
    protected $customTextStyle   = null;
13
    protected $inlineTextStyle   = null;
14
    protected $behaviour         = null;
15
    protected $behaviours        = ['motion', 'background_motion', 'parallax', 'background_parallax', 'springy'];
16
17
    abstract public function getComponent();
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
        $this->updateStyles($role);
26
    }
27
28
    public function getRole()
29
    {
30
        return $this->role;
31
    }
32
33
    public function setIdentifier($identifier)
34
    {
35
        $this->identifier        = $identifier;
36
        $this->currentIdentifier = $identifier;
37
    }
38
39
    public function getIdentifier()
40
    {
41
        return $this->identifier;
42
    }
43
44
    public function setBehaviour($type, $factor = null)
45
    {
46
        if (!in_array($type, $this->behaviours)) {
47
            throw new \ErrorException('Invalid behaviour supplied.');
48
        }
49
        $behaviour = new \stdClass();
50
        $behaviour->type = $type;
51
        if (!is_null($factor)  && $type === 'parallax') {
52
            $behaviour->factor = (float) $factor;
53
        }
54
        $this->behaviour = $behaviour;
55
    }
56
57
    public function getBehaviour()
58
    {
59
        return $this->behaviour;
60
    }
61
62
    public function getLayout()
63
    {
64
        return $this->layout;
65
    }
66
67
    public function setStyle($style)
68
    {
69
        if (is_object($style)) {
70
            $this->customStyle = $style;
71
        }
72
    }
73
74
    public function getStyle()
75
    {
76
        return !is_null($this->customStyle) ? $this->customStyle : $this->style;
77
    }
78
79
    public function setTextStyle($textStyle)
80
    {
81
        if (is_object($textStyle)) {
82
            $this->customTextStyle = $textStyle;
83
        };
84
    }
85
86
    public function getTextStyle()
87
    {
88
        return !is_null($this->customTextStyle) ? $this->customTextStyle : $this->textStyle;
89
    }
90
91
    public function updateStyles($role)
92
    {
93
        if ($this->currentIdentifier !== null && strpos($this->layout, $this->currentIdentifier)) {
94
            $this->layout    = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
95
            $this->style     = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
96
            $this->textStyle = substr($this->layout, 0, -(strlen($this->currentIdentifier) + 1));
97
        }
98
99
        $this->layout    = $role . ucfirst($this->layout);
100
        $this->style     = $role . ucfirst($this->style);
101
        $this->textStyle = $role . ucfirst($this->textStyle);
102
103
        if ($this->currentIdentifier !== null) {
104
            $this->layout    .= '-' . $this->currentIdentifier;
105
            $this->style     .= '-' . $this->currentIdentifier;
106
            $this->textStyle .= '-' . $this->currentIdentifier;
107
        }
108
109
        if (isset($this->components)) {
110
            foreach ($this->components as $component) {
111
                if ($this->currentIdentifier !== null && $component->identifier === null) {
112
                    $component->currentIdentifier = $this->currentIdentifier;
113
                }
114
                $component->updateStyles($role);
115
            }
116
        }
117
    }
118
119
    public function __toString()
120
    {
121
        return json_encode($this->getComponent(), JSON_UNESCAPED_UNICODE);
122
    }
123
}
124