Passed
Push — master ( 1676bf...daa768 )
by Malte
02:31
created

Attribute   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 30
eloc 46
c 5
b 0
f 4
dl 0
loc 230
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 2 1
A __construct() 0 3 1
A __serialize() 0 2 1
A __toString() 0 2 1
A toString() 0 2 1
A add() 0 8 3
A offsetExists() 0 2 1
A setName() 0 4 1
A attach() 0 7 3
A all() 0 2 1
A contains() 0 7 3
A merge() 0 8 3
A last() 0 6 2
A get() 0 2 1
A offsetUnset() 0 2 1
A offsetSet() 0 5 2
A first() 0 5 2
A offsetGet() 0 2 1
A getName() 0 2 1
1
<?php
2
/*
3
* File:     Attribute.php
4
* Category: -
5
* Author:   M. Goldenbaum
6
* Created:  01.01.21 20:17
7
* Updated:  -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP;
14
15
/**
16
 * Class Attribute
17
 *
18
 * @package Webklex\PHPIMAP
19
 */
20
class Attribute implements \ArrayAccess {
21
22
    /** @var string $name */
23
    protected $name;
24
25
    /**
26
     * Value holder
27
     *
28
     * @var array $values
29
     */
30
    protected $values = [];
31
32
    /**
33
     * Attribute constructor.
34
     * @param string   $name
35
     * @param array|mixed      $value
36
     */
37
    public function __construct($name, $value = null) {
38
        $this->setName($name);
39
        $this->add($value);
40
    }
41
42
43
    /**
44
     * Return the stringified attribute
45
     *
46
     * @return string
47
     */
48
    public function __toString() {
49
        return implode(", ", $this->values);
50
    }
51
52
    /**
53
     * Return the stringified attribute
54
     *
55
     * @return string
56
     */
57
    public function toString(){
58
        return $this->__toString();
59
    }
60
61
    /**
62
     * Return the serialized attribute
63
     *
64
     * @return array
65
     */
66
    public function __serialize(){
67
        return $this->values;
68
    }
69
70
    /**
71
     * Convert instance to array
72
     *
73
     * @return array
74
     */
75
    public function toArray(){
76
        return $this->__serialize();
77
    }
78
79
    /**
80
     * Determine if a value exists at an offset.
81
     *
82
     * @param  mixed  $key
83
     * @return bool
84
     */
85
    public function offsetExists($key) {
86
        return array_key_exists($key, $this->values);
87
    }
88
89
    /**
90
     * Get a value at a given offset.
91
     *
92
     * @param  mixed  $key
93
     * @return mixed
94
     */
95
    public function offsetGet($key) {
96
        return $this->values[$key];
97
    }
98
99
    /**
100
     * Set the value at a given offset.
101
     *
102
     * @param  mixed  $key
103
     * @param  mixed  $value
104
     * @return void
105
     */
106
    public function offsetSet($key, $value) {
107
        if (is_null($key)) {
108
            $this->values[] = $value;
109
        } else {
110
            $this->values[$key] = $value;
111
        }
112
    }
113
114
    /**
115
     * Unset the value at a given offset.
116
     *
117
     * @param  string  $key
118
     * @return void
119
     */
120
    public function offsetUnset($key) {
121
        unset($this->values[$key]);
122
    }
123
124
    /**
125
     * Add one or more values to the attribute
126
     * @param array|mixed $value
127
     * @param boolean $strict
128
     *
129
     * @return Attribute
130
     */
131
    public function add($value, $strict = false) {
132
        if (is_array($value)) {
133
            return $this->merge($value, $strict);
134
        }elseif ($value !== null) {
135
            $this->attach($value, $strict);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Merge a given array of values with the current values array
143
     * @param array $values
144
     * @param boolean $strict
145
     *
146
     * @return Attribute
147
     */
148
    public function merge($values, $strict = false) {
149
        if (is_array($values)) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
150
            foreach ($values as $value) {
151
                $this->attach($value, $strict);
152
            }
153
        }
154
155
        return $this;
156
    }
157
158
    /**
159
     * Check if the attribute contains the given value
160
     * @param mixed $value
161
     *
162
     * @return bool
163
     */
164
    public function contains($value) {
165
        foreach ($this->values as $v) {
166
            if ($v === $value) {
167
                return true;
168
            }
169
        }
170
        return false;
171
    }
172
173
    /**
174
     * Attach a given value to the current value array
175
     * @param $value
176
     * @param bool $strict
177
     */
178
    public function attach($value, $strict = false) {
179
        if ($strict === true) {
180
            if ($this->contains($value) === false) {
181
                $this->values[] = $value;
182
            }
183
        }else{
184
            $this->values[] = $value;
185
        }
186
    }
187
188
    /**
189
     * Set the attribute name
190
     * @param $name
191
     *
192
     * @return Attribute
193
     */
194
    public function setName($name){
195
        $this->name = $name;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Get the attribute name
202
     *
203
     * @return string
204
     */
205
    public function getName(){
206
        return $this->name;
207
    }
208
209
    /**
210
     * Get all values
211
     *
212
     * @return array
213
     */
214
    public function get(){
215
        return $this->values;
216
    }
217
218
    /**
219
     * Alias method for self::get()
220
     *
221
     * @return array
222
     */
223
    public function all(){
224
        return $this->get();
225
    }
226
227
    /**
228
     * Get the first value if possible
229
     *
230
     * @return mixed|null
231
     */
232
    public function first(){
233
        if (count($this->values) > 0) {
234
            return $this->values[0];
235
        }
236
        return null;
237
    }
238
239
    /**
240
     * Get the last value if possible
241
     *
242
     * @return mixed|null
243
     */
244
    public function last(){
245
        $cnt = count($this->values);
246
        if ($cnt > 0) {
247
            return $this->values[$cnt - 1];
248
        }
249
        return null;
250
    }
251
}