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

ElementsBlock   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 94
rs 10
c 1
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addElement() 0 5 1
A setElements() 0 9 2
A toArray() 0 7 1
A getElements() 0 3 1
A clearElements() 0 5 1
A getElementsAsArrays() 0 9 2
A __construct() 0 3 1
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 ElementsBlock extends Block
21
{
22
    /**
23
     * @var array
24
     */
25
    private $elements;
26
27
    public function __construct($type = 'actions')
28
    {
29
        parent::__construct($type);
30
    }
31
32
    /**
33
     * Convert this block to its array representation.
34
     *
35
     * @return array
36
     */
37
    public function toArray(): array
38
    {
39
        $data = [
40
            'elements' => $this->getElementsAsArrays()
41
        ];
42
43
        return array_merge(parent::toArray(), $data);
44
    }
45
46
    /**
47
     * Add an element to the message.
48
     *
49
     * @param Block $element
50
     *
51
     * @return $this
52
     */
53
    public function addElement(Block $element): self
54
    {
55
        $this->elements[] = $element;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return Block[]
62
     */
63
    public function getElements(): array
64
    {
65
        return $this->elements;
66
    }
67
68
    /**
69
     * Set the elements for the message.
70
     *
71
     * @param array $elements
72
     *
73
     * @return $this
74
     */
75
    public function setElements(array $elements): self
76
    {
77
        $this->clearElements();
78
79
        foreach ($elements as $element) {
80
            $this->addElement($element);
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Remove all elements for the message.
88
     *
89
     * @return $this
90
     */
91
    public function clearElements(): self
92
    {
93
        $this->elements = [];
94
95
        return $this;
96
    }
97
98
99
    /**
100
     * Iterates over all fields in this attachment and returns
101
     * them in their array form.
102
     *
103
     * @return array
104
     */
105
    private function getElementsAsArrays(): array
106
    {
107
        $elements = [];
108
109
        foreach ($this->elements as $element) {
110
            $elements[] = $element->toArray();
111
        }
112
113
        return $elements;
114
    }
115
}
116