Test Setup Failed
Push — master ( 6d6991...a81a2e )
by Gabriel
02:14
created

HasAttributesTrait::delAttrib()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Form\Elements\Traits;
4
5
/**
6
 * Trait HasAttributesTrait
7
 * @package Nip\Form\Elements\Traits
8
 */
9
trait HasAttributesTrait
10
{
11
    protected $_attribs;
12
13
    /**
14
     * @return mixed
15
     */
16
    public function getId()
17
    {
18
        return $this->getAttrib('id');
19
    }
20
21
    /**
22
     * @param $id
23
     * @return $this
24
     */
25
    public function setId($id)
26
    {
27
        $this->setAttrib('id', $id);
28
29
        return $this;
30
    }
31
32
    /**
33
     * @return null
34
     */
35 1
    public function getName()
36
    {
37 1
        return $this->getAttrib('name');
38
    }
39
40
41
    /**
42
     * @param $name
43
     * @return $this
44
     */
45 1
    public function setName($name)
46
    {
47 1
        $this->setAttrib('name', $name);
48
49 1
        return $this;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getLabel()
56
    {
57 1
        return $this->getAttrib('label');
58
    }
59
60
    /**
61
     * @param $label
62
     * @return $this
63
     */
64 1
    public function setLabel($label)
65
    {
66 1
        $this->setAttrib('label', $label);
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param string $requester
73
     * @return null
74
     */
75 1
    public function getValue($requester = 'abstract')
0 ignored issues
show
Unused Code introduced by
The parameter $requester is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77 1
        return $this->getAttrib('value');
78
    }
79
80
    /**
81
     * @param $value
82
     * @return $this
83
     */
84 1
    public function setValue($value)
85
    {
86 1
        $this->setAttrib('value', $value);
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94 View Code Duplication
    public function addClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $classes = func_get_args();
97
        if (is_array($classes)) {
98
            $oldClasses = explode(' ', $this->getAttrib('class'));
99
            $classes = array_merge($classes, $oldClasses);
100
            $this->setAttrib('class', implode(' ', $classes));
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return $this
108
     */
109 View Code Duplication
    public function removeClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $removeClasses = func_get_args();
112
        if (is_array($removeClasses)) {
113
            $classes = explode(' ', $this->getAttrib('class'));
114
            foreach ($removeClasses as $class) {
115
                $key = array_search($class, $classes);
116
                if ($key !== false) {
117
                    unset($classes[$key]);
118
                }
119
            }
120
            $this->setAttrib('class', implode(' ', $classes));
121
        }
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param $key
128
     * @param $value
129
     * @return static
130
     */
131 2
    public function setAttrib($key, $value)
132
    {
133 2
        $key = (string)$key;
134 2
        $this->_attribs[$key] = $value;
135
136 2
        return $this;
137
    }
138
139
    /**
140
     * @param string $key
141
     * @return null
142
     */
143 2 View Code Duplication
    public function getAttrib($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 2
        $key = (string)$key;
146 2
        if (!isset($this->_attribs[$key])) {
147
            return null;
148
        }
149
150 2
        return $this->_attribs[$key];
151
    }
152
153
    /**
154
     * @param string $key
155
     * @return bool
156
     */
157
    public function delAttrib($key)
158
    {
159
        $key = (string)$key;
160
        unset($this->_attribs[$key]);
161
162
        return true;
163
    }
164
165
    /**
166
     * @return mixed
167
     */
168
    public function getAttribs()
169
    {
170
        return $this->_attribs;
171
    }
172
173
    /**
174
     * @param  array $attribs
175
     * @return static
176
     */
177
    public function setAttribs(array $attribs)
178
    {
179
        $this->clearAttribs();
180
181
        return $this->addAttribs($attribs);
182
    }
183
184
    /**
185
     * @return static
186
     */
187
    public function clearAttribs()
188
    {
189
        $this->_attribs = [];
190
191
        return $this;
192
    }
193
194
    /**
195
     * @param  array $attribs
196
     * @return static
197
     */
198
    public function addAttribs(array $attribs)
199
    {
200
        foreach ($attribs as $key => $value) {
201
            $this->setAttrib($key, $value);
202
        }
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return bool
209
     */
210 View Code Duplication
    public function removeAttrib($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        if (isset($this->_attribs[$key])) {
213
            unset($this->_attribs[$key]);
214
215
            return true;
216
        }
217
218
        return false;
219
    }
220
}
221