Passed
Push — master ( 38d516...3306f1 )
by Alexander
02:07
created

Section::getFieldClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Maknz\Slack\Block;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Block;
6
use Maknz\Slack\BlockElement;
7
use Maknz\Slack\BlockElement\Text;
8
use Maknz\Slack\FieldsTrait;
9
use UnexpectedValueException;
10
11
class Section extends Block
12
{
13
    use FieldsTrait;
14
15
    /**
16
     * Block type.
17
     *
18
     * @var string
19
     */
20
    protected $type = 'section';
21
22
    /**
23
     * The text for the section.
24
     *
25
     * @var \Maknz\Slack\BlockElement\Text
26
     */
27
    protected $text;
28
29
    /**
30
     * Fields to appear in the section.
31
     *
32
     * @var \Maknz\Slack\BlockElement\Text[]
33
     */
34
    protected $fields = [];
35
36
    /**
37
     * Block element to be included in the section.
38
     *
39
     * @var \Maknz\Slack\BlockElement
40
     */
41
    protected $accessory;
42
43
    /**
44
     * Internal attribute to property map.
45
     *
46
     * @var array
47
     */
48
    protected static $availableAttributes = [
49
        'text'      => 'text',
50
        'block_id'  => 'block_id',
51
        'fields'    => 'fields',
52
        'accessory' => 'accessory',
53
    ];
54
55
    /**
56
     * Get the class name of valid fields.
57
     *
58
     * @return string
59
     */
60 3
    protected function getFieldClass()
61
    {
62 3
        return Text::class;
63
    }
64
65
    /**
66
     * Get the section text.
67
     *
68
     * @return \Maknz\Slack\BlockElement\Text
69
     */
70 6
    public function getText()
71
    {
72 6
        return $this->text;
73
    }
74
75
    /**
76
     * Set the section text.
77
     *
78
     * @param mixed $text
79
     *
80
     * @return $this
81
     *
82
     * @throws \InvalidArgumentException
83
     */
84 6
    public function setText($text)
85
    {
86 6
        $this->text = Text::create($text);
87
88 6
        return $this;
89
    }
90
91
    /**
92
     * Add a field to the block.
93
     *
94
     * @param mixed $field
95
     *
96
     * @return $this
97
     *
98
     * @throws \InvalidArgumentException
99
     */
100 3
    public function addField($field)
101
    {
102 3
        $field = $this->getFieldClass()::create($field);
103
104 3
        $this->fields[] = $field;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Get the section accessory.
111
     *
112
     * @return \Maknz\Slack\BlockElement
113
     */
114 2
    public function getAccessory()
115
    {
116 2
        return $this->accessory;
117
    }
118
119
    /**
120
     * Set the section accessory.
121
     *
122
     * @param mixed $accessory
123
     *
124
     * @return $this
125
     *
126
     * @throws \InvalidArgumentException
127
     */
128 2
    public function setAccessory($accessory)
129
    {
130 2
        $accessory = BlockElement::factory($accessory);
131
132 2
        if ( ! $accessory->isValidFor($this)) {
133 1
            throw new InvalidArgumentException('Block element '.get_class($accessory).' is not valid for '.static::class);
134
        }
135
136 1
        $this->accessory = $accessory;
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Convert the block to its array representation.
143
     *
144
     * @return array
145
     */
146 3
    public function toArray()
147
    {
148 3
        $data = [
149 3
            'type' => $this->getType(),
150
        ];
151
152 3
        if ($this->getText()) {
153 1
            $data['text'] = $this->getText()->toArray();
154
        }
155
156 3
        if (count($this->getFields())) {
157 2
            $data['fields'] = $this->getFieldsAsArrays();
158 1
        } elseif ( ! $this->getText()) {
159 1
            throw new UnexpectedValueException('Section requires text attribute if no fields attribute is provided');
160
        }
161
162 2
        if ($this->getBlockId()) {
163 1
            $data['block_id'] = $this->getBlockId();
164
        }
165
166 2
        if ($this->getAccessory()) {
167 1
            $data['accessory'] = $this->getAccessory()->toArray();
168
        }
169
170 2
        return $data;
171
    }
172
}
173