|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the eav package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alex Kuperwood <[email protected]> |
|
6
|
|
|
* @copyright 2025 Alex Kuperwood |
|
7
|
|
|
* @license https://opensource.org/license/mit The MIT License |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Kuperwood\Eav; |
|
12
|
|
|
|
|
13
|
|
|
class Transporter |
|
14
|
|
|
{ |
|
15
|
|
|
private array $data = []; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __get(string $field) |
|
18
|
|
|
{ |
|
19
|
1 |
|
return $this->getField($field); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function __set(string $field, $value): void |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->setField($field, $value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function __isset(string $field): bool |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->hasField($field); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function __unset(string $field): void |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->removeField($field); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function __toString(): string |
|
38
|
|
|
{ |
|
39
|
1 |
|
return json_encode($this->data); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function __toArray(): array |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->data; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function __toJson(): string |
|
48
|
|
|
{ |
|
49
|
1 |
|
return json_encode($this->data); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function __toObject(): object |
|
53
|
|
|
{ |
|
54
|
1 |
|
return (object) $this->data; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function setField($field, $value): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->data[$field] = $value; |
|
60
|
|
|
|
|
61
|
1 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $field |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getField($field) |
|
68
|
|
|
{ |
|
69
|
1 |
|
if (!$this->hasField($field)) { |
|
70
|
1 |
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $this->data[$field]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function getData(): array |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function setData(array $data): self |
|
82
|
|
|
{ |
|
83
|
1 |
|
$this->data = $data; |
|
84
|
|
|
|
|
85
|
1 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function hasField($field): bool |
|
89
|
|
|
{ |
|
90
|
1 |
|
return array_key_exists($field, $this->data); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function removeField($field): void |
|
94
|
|
|
{ |
|
95
|
1 |
|
unset($this->data[$field]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function clear(): void |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->data = []; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|