Attribute   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 5
Metric Value
wmc 29
eloc 47
c 9
b 0
f 5
dl 0
loc 237
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 3
A offsetExists() 0 2 1
A toArray() 0 2 1
A merge() 0 6 2
A __construct() 0 3 1
A __toString() 0 2 1
A offsetUnset() 0 3 1
A offsetSet() 0 6 2
A offsetGet() 0 3 1
A toDate() 0 5 2
A toString() 0 2 1
A count() 0 2 1
A setName() 0 4 1
A attach() 0 7 3
A all() 0 2 1
A contains() 0 2 1
A last() 0 5 2
A get() 0 2 1
A first() 0 5 2
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
use ArrayAccess;
16
use Carbon\Carbon;
17
use ReturnTypeWillChange;
0 ignored issues
show
Bug introduced by
The type ReturnTypeWillChange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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