Passed
Push — master ( 8ede22...b3e0ce )
by Alexander
02:27
created

OptionsTrait::getOptionsAsArrays()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Object\Option;
6
7
trait OptionsTrait
8
{
9
    /**
10
     * Options available within the block.
11
     *
12
     * @var \Maknz\Slack\Object\Option[]
13
     */
14
    protected $options = [];
15
16
    /**
17
     * Get options available within the block.
18
     *
19
     * @return \Maknz\Slack\Object\Option[]
20
     */
21 30
    public function getOptions()
22
    {
23 30
        return $this->options;
24
    }
25
26
    /**
27
     * Get options available within the block in array format.
28
     *
29
     * @return array
30
     */
31 11
    public function getOptionsAsArrays()
32
    {
33 11
        $options = [];
34
35 11
        foreach ($this->getOptions() as $option) {
36 11
            $options[] = $option->toArray();
37
        }
38
39 11
        return $options;
40
    }
41
42
    /**
43
     * Set options available within the block.
44
     *
45
     * @param array $options
46
     *
47
     * @return $this
48
     *
49
     * @throws \InvalidArgumentException
50
     */
51 29
    public function setOptions(array $options)
52
    {
53 29
        $this->clearOptions();
54
55 29
        foreach ($options as $option) {
56 29
            $this->addOption($option);
57
        }
58
59 27
        return $this;
60
    }
61
62
    /**
63
     * Clear options available within the block.
64
     *
65
     * @return $this
66
     */
67 29
    public function clearOptions()
68
    {
69 29
        $this->options = [];
70
71 29
        return $this;
72
    }
73
74
    /**
75
     * Add an option to the block.
76
     *
77
     * @param mixed $option
78
     *
79
     * @return $this
80
     *
81
     * @throws \InvalidArgumentException
82
     */
83 33
    public function addOption($option)
84
    {
85 33
        if (is_array($option)) {
86 29
            $option = new Option($option);
87
        }
88
89 33
        if ($option instanceof Option) {
90 32
            $this->options[] = $option;
91
92 32
            return $this;
93
        }
94
95 1
        throw new InvalidArgumentException('The option must be an instance of '.Option::class.' or a keyed array');
96
    }
97
}
98