|
2 | 2 |
|
3 | 3 | namespace CodinPro\DataTransferObject; |
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
| 6 | + |
5 | 7 | /** |
6 | 8 | * @property mixed $innerDTOData DTO data |
7 | 9 | * @property array $innerDTODefault DTO keys and default values |
8 | 10 | */ |
9 | 11 | trait DTOAccessorTrait |
10 | 12 | { |
11 | | - /** |
12 | | - * Get value by offset or nested structure |
13 | | - * @param string $offset |
14 | | - * @return mixed |
15 | | - */ |
16 | | - public abstract function get($offset); |
| 13 | + public function __get($key) |
| 14 | + { |
| 15 | + return $this->offsetGet($key); |
| 16 | + } |
17 | 17 |
|
18 | | - /** |
19 | | - * Check if offset exists |
20 | | - * @param string $offset |
21 | | - * @return bool |
22 | | - */ |
23 | | - public function offsetExists($offset) |
| 18 | + public function __set($key, $value) |
24 | 19 | { |
25 | | - return isset($this->innerDTOData[$offset]); |
| 20 | + $this->offsetSet($key, $value); |
26 | 21 | } |
27 | 22 |
|
28 | 23 | /** |
29 | | - * Get data at scalar offset or default value instead |
30 | | - * @param string $offset |
| 24 | + * Get data at offset or default value instead |
| 25 | + * @param string $offset |
31 | 26 | * @return mixed |
32 | | - * @throws \InvalidArgumentException |
| 27 | + * @throws InvalidArgumentException |
33 | 28 | */ |
34 | | - private function offsetGetScalar($offset) |
| 29 | + public function offsetGet($offset): mixed |
35 | 30 | { |
36 | | - if (array_key_exists($offset, $this->innerDTOData)) { |
37 | | - return $this->innerDTOData[$offset]; |
38 | | - } |
39 | | - |
40 | | - return $this->getDefaultValue($offset); |
| 31 | + return $this->get($offset); |
41 | 32 | } |
42 | 33 |
|
43 | 34 | /** |
44 | | - * Get default value at offset if set |
45 | | - * @param string $offset |
| 35 | + * Get value by offset or nested structure |
| 36 | + * @param string $offset |
46 | 37 | * @return mixed |
47 | | - * @throws \InvalidArgumentException |
48 | 38 | */ |
49 | | - private function getDefaultValue($offset) |
| 39 | + abstract public function get(string $offset): mixed; |
| 40 | + |
| 41 | + /** |
| 42 | + * Set data at offset |
| 43 | + * @param string $offset |
| 44 | + * @param mixed $value |
| 45 | + */ |
| 46 | + public function offsetSet($offset, mixed $value): void |
50 | 47 | { |
51 | | - if (array_key_exists($offset, $this->innerDTODefault)) { |
52 | | - return $this->innerDTODefault[$offset]; |
53 | | - } |
| 48 | + $this->innerDTOData[$offset] = $value; |
| 49 | + } |
54 | 50 |
|
55 | | - throw new \InvalidArgumentException('Offset '.$offset.' does not exist.'); |
| 51 | + public function __isset($key) |
| 52 | + { |
| 53 | + return isset($this->innerDTOData[$key]); |
56 | 54 | } |
57 | 55 |
|
58 | 56 | /** |
59 | | - * Get data at offset or default value instead |
60 | | - * @param string $offset |
61 | | - * @return mixed |
62 | | - * @throws \InvalidArgumentException |
| 57 | + * Count data elements |
| 58 | + * @return int |
63 | 59 | */ |
64 | | - public function offsetGet($offset) |
| 60 | + public function count(): int |
65 | 61 | { |
66 | | - return $this->get($offset); |
| 62 | + return count($this->innerDTOData); |
67 | 63 | } |
68 | 64 |
|
69 | 65 | /** |
70 | | - * Set data at offset |
71 | | - * @param string $offset |
72 | | - * @param mixed $value |
| 66 | + * Check if offset exists |
| 67 | + * @param string $offset |
| 68 | + * @return bool |
73 | 69 | */ |
74 | | - public function offsetSet($offset, $value) |
| 70 | + public function offsetExists($offset): bool |
75 | 71 | { |
76 | | - $this->innerDTOData[$offset] = $value; |
| 72 | + return isset($this->innerDTOData[$offset]); |
77 | 73 | } |
78 | 74 |
|
79 | 75 | /** |
80 | 76 | * Remove data at offset |
81 | | - * @param string $offset |
| 77 | + * @param string $offset |
82 | 78 | */ |
83 | | - public function offsetUnset($offset) |
| 79 | + public function offsetUnset($offset): void |
84 | 80 | { |
85 | 81 | unset($this->innerDTOData[$offset]); |
86 | 82 | } |
87 | 83 |
|
88 | 84 | /** |
89 | | - * Count data elements |
90 | | - * @return int |
| 85 | + * Get data at scalar offset or default value instead |
| 86 | + * @param string $offset |
| 87 | + * @param bool $strict |
| 88 | + * @return mixed |
91 | 89 | */ |
92 | | - public function count() |
| 90 | + private function offsetGetScalar(string $offset, bool $strict = false): mixed |
93 | 91 | { |
94 | | - return count($this->innerDTOData); |
95 | | - } |
| 92 | + if (array_key_exists($offset, $this->innerDTOData)) { |
| 93 | + return $this->innerDTOData[$offset]; |
| 94 | + } |
96 | 95 |
|
97 | | - public function __get($key) |
98 | | - { |
99 | | - return $this->offsetGet($key); |
| 96 | + return $this->getDefaultValue($offset, $strict); |
100 | 97 | } |
101 | 98 |
|
102 | | - public function __set($key, $value) |
| 99 | + /** |
| 100 | + * Get default value at offset if set |
| 101 | + * @param string $offset |
| 102 | + * @param bool $strict |
| 103 | + * @return mixed |
| 104 | + */ |
| 105 | + private function getDefaultValue(string $offset, bool $strict = false): mixed |
103 | 106 | { |
104 | | - $this->offsetSet($key, $value); |
105 | | - } |
| 107 | + if (array_key_exists($offset, $this->innerDTODefault)) { |
| 108 | + return $this->innerDTODefault[$offset]; |
| 109 | + } |
106 | 110 |
|
107 | | - public function __isset($key) |
108 | | - { |
109 | | - return isset($this->innerDTOData[$key]); |
| 111 | + if ($strict) { |
| 112 | + throw new InvalidArgumentException('Offset ' . $offset . ' does not exist.'); |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
110 | 116 | } |
111 | 117 | } |
0 commit comments