Passed
Pull Request — master (#38)
by
unknown
02:06
created

Text::setText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\BlockElement;
6
use Maknz\Slack\Field;
7
8
class Text extends BlockElement implements Field
9
{
10
    const TYPE_PLAIN = 'plain_text';
11
    const TYPE_MARKDOWN = 'mrkdwn';
12
13
    /**
14
     * Text format type.
15
     *
16
     * @var string
17
     */
18
    protected $type;
19
20
    /**
21
     * Text content.
22
     *
23
     * @var string
24
     */
25
    protected $text;
26
27
    /**
28
     * Whether emojis should be escaped.
29
     *
30
     * @var string
31
     */
32
    protected $escape_emojis = false;
33
34
    /**
35
     * Whether text content should be treated as-is.
36
     *
37
     * @var string
38
     */
39
    protected $verbatim = false;
40
41
    /**
42
     * Internal attribute to property map.
43
     *
44
     * @var array
45
     */
46
    protected static $availableAttributes = [
47
        'type'     => 'type',
48
        'text'     => 'text',
49
        'emoji'    => 'escape_emojis',
50
        'verbatim' => 'verbatim',
51
    ];
52
53
    /**
54
     * Get the text format type.
55
     *
56
     * @return string
57
     */
58 50
    public function getType()
59
    {
60 50
        return $this->type;
61
    }
62
63
    /**
64
     * Set the text format type.
65
     *
66
     * @param string $type
67
     *
68
     * @return Text
69
     */
70 55
    public function setType($type)
71
    {
72 55
        $this->type = $type;
73
74 55
        return $this;
75
    }
76
77
    /**
78
     * Get the text content.
79
     *
80
     * @return string
81
     */
82 40
    public function getText()
83
    {
84 40
        return $this->text;
85
    }
86
87
    /**
88
     * Set the text content.
89
     *
90
     * @param string $text
91
     *
92
     * @return Text
93
     */
94 55
    public function setText($text)
95
    {
96 55
        $this->text = $text;
97
98 55
        return $this;
99
    }
100
101
    /**
102
     * Get whether to escape emojis.
103
     *
104
     * @return bool
105
     */
106 18
    public function getEscapeEmojis()
107
    {
108 18
        return $this->escape_emojis;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->escape_emojis returns the type string which is incompatible with the documented return type boolean.
Loading history...
109
    }
110
111
    /**
112
     * Set whether to escape emojis.
113
     *
114
     * @param bool $escape
115
     *
116
     * @return Text
117
     */
118
    public function setEscapeEmojis($escape)
119
    {
120
        $this->escape_emojis = (bool)$escape;
0 ignored issues
show
Documentation Bug introduced by
The property $escape_emojis was declared of type string, but (bool)$escape is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
121
122
        return $this;
123
    }
124
125
    /**
126
     * Get whether to treat text as-is.
127
     *
128
     * @return bool
129
     */
130 2
    public function getVerbatim()
131
    {
132 2
        return $this->verbatim;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->verbatim returns the type string which is incompatible with the documented return type boolean.
Loading history...
133
    }
134
135
    /**
136
     * Set whether to treat text as-is.
137
     *
138
     * @param bool $verbatim
139
     *
140
     * @return Text
141
     */
142
    public function setVerbatim($verbatim)
143
    {
144
        $this->verbatim = (bool)$verbatim;
0 ignored issues
show
Documentation Bug introduced by
The property $verbatim was declared of type string, but (bool)$verbatim is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
145
146
        return $this;
147
    }
148
149
    /**
150
     * Convert the block to its array representation.
151
     *
152
     * @return array
153
     */
154 19
    public function toArray()
155
    {
156
        $data = [
157 19
            'type' => $this->getType(),
158 19
            'text' => $this->getText(),
159
        ];
160
161 19
        if ($data['type'] == static::TYPE_PLAIN) {
162 18
            $data['emoji'] = $this->getEscapeEmojis();
163
        } else {
164 2
            $data['verbatim'] = $this->getVerbatim();
165
        }
166
167 19
        return $data;
168
    }
169
170
    /**
171
     * Create a Text block from various formats.
172
     *
173
     * @param mixed  $text
174
     * @param string $type If passed, the Text object will be of this type.
175
     *
176
     * @return Text
177
     *
178
     * @throws \InvalidArgumentException
179
     */
180 50
    public static function create($text, $type = null)
181
    {
182 50
        if (is_string($text)) {
183
            $text = [
184 46
                'type' => $type ?? static::TYPE_PLAIN,
185 46
                'text' => $text,
186
            ];
187
        }
188
189 50
        if (is_array($text)) {
190 48
            $text = new static($text);
191
        }
192
193 50
        if ($text instanceof static) {
194 49
            if ($type && $text->getType() != $type) {
195
                throw new InvalidArgumentException('Text type must be '.$type);
196
            }
197
198 49
            return $text;
199
        }
200
201 1
        throw new InvalidArgumentException('Text must be a string, keyed array or '.static::class.' object');
202
    }
203
}
204