-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractModel.php
More file actions
127 lines (111 loc) · 2.93 KB
/
Copy pathAbstractModel.php
File metadata and controls
127 lines (111 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Dotcms\PhpSdk\Model\Core;
use Symfony\Component\Serializer\Annotation\Ignore;
/**
* Abstract base class for models that need to handle additional properties
*
* @implements \ArrayAccess<string, mixed>
*/
abstract class AbstractModel implements \ArrayAccess, \JsonSerializable
{
/**
* @var array<string, mixed> Additional properties not explicitly defined
* @Ignore()
*/
private array $additionalProperties = [];
/**
* Set additional properties
*
* @param array<string, mixed> $additionalProperties
*/
protected function setAdditionalProperties(array $additionalProperties): void
{
$this->additionalProperties = $additionalProperties;
}
/**
* Get a property value
*
* @param string $name Property name
* @return mixed Property value or null if not found
*/
protected function get(string $name): mixed
{
if (property_exists($this, $name)) {
return $this->$name;
}
return $this->additionalProperties[$name] ?? null;
}
/**
* Check if a property exists
*
* @param string $name Property name
* @return bool True if the property exists
*/
protected function has(string $name): bool
{
return property_exists($this, $name) || isset($this->additionalProperties[$name]);
}
/**
* Magic method to get property values
* This enables property access syntax (e.g., $obj->propertyName) for additional properties
*
* @param string $name Property name
* @return mixed Property value or null if not found
*/
public function __get(string $name): mixed
{
return $this->get($name);
}
/**
* Magic method to check if a property exists
* This enables isset() checks on additional properties
*
* @param string $name Property name
* @return bool True if the property exists
*/
public function __isset(string $name): bool
{
return $this->has($name);
}
/**
* @inheritDoc
*/
public function offsetExists(mixed $offset): bool
{
return is_string($offset) && $this->has($offset);
}
/**
* @inheritDoc
*/
public function offsetGet(mixed $offset): mixed
{
if (! is_string($offset)) {
return null;
}
return $this->get($offset);
}
/**
* @inheritDoc
*/
public function offsetSet(mixed $offset, mixed $value): void
{
throw new \RuntimeException('Properties are read-only');
}
/**
* @inheritDoc
*/
public function offsetUnset(mixed $offset): void
{
throw new \RuntimeException('Properties are read-only');
}
/**
* Get all additional properties
*
* @return array<string, mixed>
*/
protected function getAdditionalProperties(): array
{
return $this->additionalProperties;
}
}