| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
use ArrayAccess; |
| 16 |
use Carbon\Carbon; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Attribute implements ArrayAccess { |
| 24 |
|
| 25 |
|
| 26 |
protected string $name; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected array $values = []; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __construct(string $name, mixed $value = null) { |
| 41 |
$this->setName($name); |
| 42 |
$this->add($value); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function __serialize(){ |
| 63 |
return $this->values; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function __toString() { |
| 72 |
return implode(", ", $this->values); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function toString(): string { |
| 81 |
return $this->__toString(); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function toArray(): array { |
| 90 |
return $this->__serialize(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function has(mixed $key = 0): bool { |
| 112 |
return array_key_exists($key, $this->values); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function exist(mixed $key = 0): bool { |
| 122 |
return $this->has($key); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function contains(mixed $value): bool { |
| 132 |
return in_array($value, $this->values, true); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function get(int|string $key = 0): mixed { |
| 142 |
return $this->values[$key] ?? null; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function setName($name): Attribute { |
| 230 |
$this->name = $name; |
| 231 |
|
| 232 |
return $this; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function getName(): string { |
| 241 |
return $this->name; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function all(): array { |
| 250 |
reset($this->values); |
| 251 |
return $this->values; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function first(): mixed { |
| 260 |
return reset($this->values); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function last(): mixed { |
| 269 |
return end($this->values); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function count(): int { |
| 278 |
return count($this->values); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function offsetExists(mixed $offset): bool { |
| 287 |
return $this->has($offset); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function offsetGet(mixed $offset): mixed { |
| 296 |
return $this->get($offset); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function offsetSet(mixed $offset, mixed $value): void { |
| 306 |
$this->set($value, $offset); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function offsetUnset(mixed $offset): void { |
| 315 |
$this->remove($offset); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function map(callable $callback): array { |
| 323 |
return array_map($callback, $this->values); |
| 324 |
} |
| 325 |
} |