Passed
Push — master ( a22205...112e67 )
by Gabriel
04:04 queued 13s
created

HasButtonsTrait::getButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip\Form\Buttons\AbstractButton as ButtonAbstract;
6
use Nip\Form\Elements\AbstractElement as ElementAbstract;
7
8
/**
9
 * Trait HasButtonsTrait
10
 * @package Nip\Form\Traits
11
 */
12
trait HasButtonsTrait
13
{
14
    protected $_buttons;
15
16
    /**
17
     * @param $name
18
     * @param bool $label
19
     * @param string $type
20
     * @return $this
21
     */
22
    public function addButton($name, $label = false, $type = 'button')
23
    {
24
        $this->_buttons[$name] = $this->newButton($name, $label, $type);
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param $name
31
     * @param bool $label
32
     * @param string $type
33
     * @return ButtonAbstract
34
     */
35
    protected function newButton($name, $label = false, $type = 'button')
36
    {
37
        $class = 'Nip_Form_Button_' . ucfirst($type);
38
        /** @var ButtonAbstract $button */
39
        $button = new $class($this);
40
        $button->setName($name)
41
            ->setLabel($label);
42
43
        return $button;
44
    }
45
46
    /**
47
     * @param $name
48
     * @return ElementAbstract
49
     */
50
    public function getButton($name)
51
    {
52
        if (array_key_exists($name, $this->_buttons)) {
53
            return $this->_buttons[$name];
54
        }
55
56
        return null;
57
    }
58
59
60
    /**
61
     * @return ButtonAbstract[]
62
     */
63
    public function getButtons()
64
    {
65
        return $this->_buttons;
66
    }
67
}
68