HasAttributesTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 87
ccs 15
cts 26
cp 0.5769
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A removeAttrib() 0 9 2
A getAttrib() 0 8 2
A setAttrib() 0 6 1
A setAttribs() 0 5 1
A clearAttribs() 0 5 1
A addAttribs() 0 7 2
A getAttribs() 0 3 1
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
/**
6
 * Trait HasAttributesTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait HasAttributesTrait
10
{
11
    protected $_attribs = [];
12
13
14
    /**
15
     * @param $key
16
     * @param $value
17
     * @return $this
18
     */
19 7
    public function setAttrib($key, $value)
20
    {
21 7
        $key = (string)$key;
22 7
        $this->_attribs[$key] = $value;
23
24 7
        return $this;
25
    }
26
27
    /**
28
     * @param string $key
29
     * @return string
30
     */
31 9
    public function getAttrib($key)
32
    {
33 9
        $key = (string)$key;
34 9
        if (!isset($this->_attribs[$key])) {
35 9
            return null;
36
        }
37
38 6
        return $this->_attribs[$key];
39
    }
40
41
    /**
42
     * @return array
43
     */
44 1
    public function getAttribs()
45
    {
46 1
        return $this->_attribs;
47
    }
48
49
    /**
50
     * @param  array $attribs
51
     * @return $this
52
     */
53
    public function setAttribs(array $attribs)
54
    {
55
        $this->clearAttribs();
56
57
        return $this->addAttribs($attribs);
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    public function clearAttribs()
64
    {
65
        $this->_attribs = [];
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param  array $attribs
72
     * @return $this
73
     */
74 1
    public function addAttribs(array $attribs)
75
    {
76 1
        foreach ($attribs as $key => $value) {
77 1
            $this->setAttrib($key, $value);
78
        }
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * @param $key
85
     * @return bool
86
     */
87
    public function removeAttrib($key)
88
    {
89
        if (isset($this->_attribs[$key])) {
90
            unset($this->_attribs[$key]);
91
92
            return true;
93
        }
94
95
        return false;
96
    }
97
}
98