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

Text::setRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace FlatPlan\Components;
2
3
class Text extends AbstractComponent {
4
5
    private $text;
6
    private $role;
7
    private $format;
8
    private $textStyle;
9
10
    protected $formats = ['html', 'markdown', 'none'];
11
    protected $roles   = ['body', 'title', 'heading', 'intro', 'caption', 'author', 'byline', 'illustrator', 'photographer', 'quote', 'pullquote'];
12
13
    /**
14
     * @param string $text
15
     * @param string $role
16
     * @param string $format
17
     * @param string $textStyle
18
     * @return void
19
     */
20
    public function __construct($text, $role, $format = 'none', $textStyle = null)
21
    {
22
        $this->setText($text);
23
        $this->setRole($role);
24
        $this->setFormat($format);
25
        $this->setTextStyle($textStyle);
26
    }
27
28
    public function setText($text)
29
    {
30
        $this->text = $text;
31
    }
32
33
    public function getText()
34
    {
35
        return $this->text;
36
    }
37
38
    public function setRole($role)
39
    {
40
        if (!in_array($role, $this->roles)) {
41
            throw new \ErrorException('Invalid role supplied.');
42
        }
43
        $this->role = $role;
44
    }
45
46
    public function getRole()
47
    {
48
        return $this->role;
49
    }
50
51
    public function setFormat($format = null)
52
    {
53
        if (!in_array($format, $this->formats)) {
54
            throw new \ErrorException('Invalid format supplied.');
55
        }
56
        $this->format = $format;
57
    }
58
59
    public function getFormat()
60
    {
61
        return $this->format;
62
    }
63
64
    public function setTextStyle($textStyle)
65
    {
66
        $this->textStyle = $textStyle;
67
    }
68
69
    public function getTextStyle()
70
    {
71
        return $this->textStyle;
72
    }
73
74
    public function getComponent()
75
    {
76
        $component = new \stdClass();
77
        $component->text      = $this->getText();
78
        $component->role      = $this->getRole();
79
        $component->format    = $this->getFormat();
80
        $component->textStyle = $this->getTextStyle();
81
        return $component;
82
    }
83
84
    public function __toString()
85
    {
86
        return json_encode($this->getComponent());
87
    }
88
}
89