Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Attribute.php

raw symbols SCIP
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 string $name;
27
28 /**
29 * Value holder
30 *
31 * @var array $values
32 */
33 protected array $values = [];
34
35 /**
36 * Attribute constructor.
37 * @param string $name
38 * @param mixed|null $value
39 */
40 public function __construct(string $name, mixed $value = null) {
41 $this->setName($name);
42 $this->add($value);
43 }
44
45 /**
46 * Handle class invocation calls
47 *
48 * @return array|string
49 */
50 public function __invoke(): array|string {
51 if ($this->count() > 1) {
52 return $this->toArray();
53 }
54 return $this->toString();
55 }
56
57 /**
58 * Return the serialized address
59 *
60 * @return array
61 */
62 public function __serialize(){
63 return $this->values;
64 }
65
66 /**
67 * Return the stringified attribute
68 *
69 * @return string
70 */
71 public function __toString() {
72 return implode(", ", $this->values);
73 }
74
75 /**
76 * Return the stringified attribute
77 *
78 * @return string
79 */
80 public function toString(): string {
81 return $this->__toString();
82 }
83
84 /**
85 * Convert instance to array
86 *
87 * @return array
88 */
89 public function toArray(): array {
90 return $this->__serialize();
91 }
92
93 /**
94 * Convert first value to a date object
95 *
96 * @return Carbon
97 */
98 public function toDate(): Carbon {
99 $date = $this->first();
100 if ($date instanceof Carbon) return $date;
101
102 return Carbon::parse($date);
103 }
104
105 /**
106 * Determine if a value exists at a given key.
107 *
108 * @param int|string $key
109 * @return bool
110 */
111 public function has(mixed $key = 0): bool {
112 return array_key_exists($key, $this->values);
113 }
114
115 /**
116 * Determine if a value exists at a given key.
117 *
118 * @param int|string $key
119 * @return bool
120 */
121 public function exist(mixed $key = 0): bool {
122 return $this->has($key);
123 }
124
125 /**
126 * Check if the attribute contains the given value
127 * @param mixed $value
128 *
129 * @return bool
130 */
131 public function contains(mixed $value): bool {
132 return in_array($value, $this->values, true);
133 }
134
135 /**
136 * Get a value by a given key.
137 *
138 * @param int|string $key
139 * @return mixed
140 */
141 public function get(int|string $key = 0): mixed {
142 return $this->values[$key] ?? null;
143 }
144
145 /**
146 * Set the value by a given key.
147 *
148 * @param mixed $key
149 * @param mixed $value
150 * @return Attribute
151 */
152 public function set(mixed $value, mixed $key = 0): Attribute {
153 if (is_null($key)) {
154 $this->values[] = $value;
155 } else {
156 $this->values[$key] = $value;
157 }
158 return $this;
159 }
160
161 /**
162 * Unset a value by a given key.
163 *
164 * @param int|string $key
165 * @return Attribute
166 */
167 public function remove(int|string $key = 0): Attribute {
168 if (isset($this->values[$key])) {
169 unset($this->values[$key]);
170 }
171 return $this;
172 }
173
174 /**
175 * Add one or more values to the attribute
176 * @param array|mixed $value
177 * @param boolean $strict
178 *
179 * @return Attribute
180 */
181 public function add(mixed $value, bool $strict = false): Attribute {
182 if (is_array($value)) {
183 return $this->merge($value, $strict);
184 }elseif ($value !== null) {
185 $this->attach($value, $strict);
186 }
187
188 return $this;
189 }
190
191 /**
192 * Merge a given array of values with the current values array
193 * @param array $values
194 * @param boolean $strict
195 *
196 * @return Attribute
197 */
198 public function merge(array $values, bool $strict = false): Attribute {
199 foreach ($values as $value) {
200 $this->attach($value, $strict);
201 }
202
203 return $this;
204 }
205
206 /**
207 * Attach a given value to the current value array
208 * @param $value
209 * @param bool $strict
210 * @return Attribute
211 */
212 public function attach($value, bool $strict = false): Attribute {
213 if ($strict === true) {
214 if ($this->contains($value) === false) {
215 $this->values[] = $value;
216 }
217 }else{
218 $this->values[] = $value;
219 }
220 return $this;
221 }
222
223 /**
224 * Set the attribute name
225 * @param $name
226 *
227 * @return Attribute
228 */
229 public function setName($name): Attribute {
230 $this->name = $name;
231
232 return $this;
233 }
234
235 /**
236 * Get the attribute name
237 *
238 * @return string
239 */
240 public function getName(): string {
241 return $this->name;
242 }
243
244 /**
245 * Get all values
246 *
247 * @return array
248 */
249 public function all(): array {
250 reset($this->values);
251 return $this->values;
252 }
253
254 /**
255 * Get the first value if possible
256 *
257 * @return mixed|null
258 */
259 public function first(): mixed {
260 return reset($this->values);
261 }
262
263 /**
264 * Get the last value if possible
265 *
266 * @return mixed|null
267 */
268 public function last(): mixed {
269 return end($this->values);
270 }
271
272 /**
273 * Get the number of values
274 *
275 * @return int
276 */
277 public function count(): int {
278 return count($this->values);
279 }
280
281 /**
282 * @see ArrayAccess::offsetExists
283 * @param mixed $offset
284 * @return bool
285 */
286 public function offsetExists(mixed $offset): bool {
287 return $this->has($offset);
288 }
289
290 /**
291 * @see ArrayAccess::offsetGet
292 * @param mixed $offset
293 * @return mixed
294 */
295 public function offsetGet(mixed $offset): mixed {
296 return $this->get($offset);
297 }
298
299 /**
300 * @see ArrayAccess::offsetSet
301 * @param mixed $offset
302 * @param mixed $value
303 * @return void
304 */
305 public function offsetSet(mixed $offset, mixed $value): void {
306 $this->set($value, $offset);
307 }
308
309 /**
310 * @see ArrayAccess::offsetUnset
311 * @param mixed $offset
312 * @return void
313 */
314 public function offsetUnset(mixed $offset): void {
315 $this->remove($offset);
316 }
317
318 /**
319 * @param callable $callback
320 * @return array
321 */
322 public function map(callable $callback): array {
323 return array_map($callback, $this->values);
324 }
325 }