Passed
Push — master ( b1c492...cf95d8 )
by Malte
02:26
created

Attribute::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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