Passed
Pull Request — master (#53)
by
unknown
01:19
created

SectionBlock::getFieldsAsArrays()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Nexylan packages.
7
 *
8
 * (c) Nexylan SAS <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nexy\Slack;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 * @author Mikey McLellan <[email protected]>
19
 */
20
class SectionBlock extends Block
21
{
22
    /**
23
     * @var TextBlock
24
     */
25
    private $text;
26
27
    /**
28
     * @var Block
29
     */
30
    private $accessory;
31
32
    /**
33
     * @var TextBlock[]
34
     */
35
    private $fields = [];
36
37
    public function __construct($type = 'section')
38
    {
39
        parent::__construct($type);
40
    }
41
42
    /**
43
     * @param string $text
44
     */
45
    public function setText(TextBlock $text): self
46
    {
47
        $this->text = $text;
48
49
        return $this;
50
    }
51
52
    public function getText(): TextBlock
53
    {
54
        return $this->text;
55
    }
56
57
    public function setPlainText(string $string): self
58
    {
59
        return $this->setText((new TextBlock())
60
            ->setType(TextBlock::PLAIN_TEXT)
61
            ->setText($string));
62
    }
63
64
    public function setMarkdown(string $string): self
65
    {
66
        return $this->setText((new TextBlock())
67
            ->setType(TextBlock::MARKDOWN)
68
            ->setText($string));
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getAccessory(): Block
75
    {
76
        return $this->accessory;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->accessory returns the type Nexy\Slack\Block which is incompatible with the documented return type array.
Loading history...
77
    }
78
79
    /**
80
     * @param array $accessory
81
     */
82
    public function setAccessory(Block $accessory): self
83
    {
84
        $this->accessory = $accessory;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Convert this block to its array representation.
91
     *
92
     * @return array
93
     */
94
    public function toArray(): array
95
    {
96
        $data = [
97
            'text' => $this->text->toArray(),
98
            'fields' => $this->getFieldsAsArrays()
99
        ];
100
101
        if ($this->accessory) {
102
            $data['accessory'] = $this->accessory->toArray();
103
        }
104
105
        return array_merge(parent::toArray(), $this->removeNulls($data));
106
    }
107
108
    /**
109
     * Add an field to the message.
110
     *
111
     * @param TextBlock $field
112
     *
113
     * @return $this
114
     */
115
    public function addField(TextBlock $field): self
116
    {
117
        $this->fields[] = $field;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return TextBlock[]
124
     */
125
    public function getFields(): array
126
    {
127
        return $this->fields;
128
    }
129
130
    /**
131
     * Set the fields for the message.
132
     *
133
     * @param TextBlock[] $fields
134
     *
135
     * @return $this
136
     */
137
    public function setFields(array $fields): self
138
    {
139
        $this->clearFields();
140
141
        foreach ($fields as $field) {
142
            $this->addField($field);
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * Remove all fields for the message.
150
     *
151
     * @return $this
152
     */
153
    public function clearFields(): self
154
    {
155
        $this->fields = [];
156
157
        return $this;
158
    }
159
160
    /**
161
     * Iterates over all fields in this attachment and returns
162
     * them in their array form.
163
     *
164
     * @return array
165
     */
166
    private function getFieldsAsArrays(): array
167
    {
168
        $fields = [];
169
170
        foreach ($this->fields as $field) {
171
            $fields[] = $field->toArray();
172
        }
173
174
        return $fields;
175
    }
176
}
177