1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\Tessitura\Base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for Tessitura API resources. |
7
|
|
|
* |
8
|
|
|
* Provides a foundation for API resources with explicit getter methods |
9
|
|
|
* for better IDE support and type safety. Magic methods are still supported |
10
|
|
|
* for backward compatibility but are deprecated for common properties. |
11
|
|
|
* |
12
|
|
|
* @package Clubdeuce\Tessitura\Base |
13
|
|
|
*/ |
14
|
|
|
class Base |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Holds the state of the object. |
18
|
|
|
* |
19
|
|
|
* This property is used to store the values of the object's properties |
20
|
|
|
* that are set through the constructor or other methods. |
21
|
|
|
* |
22
|
|
|
* @var mixed[] |
23
|
|
|
*/ |
24
|
|
|
protected array $_extraArgs = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Base constructor. |
28
|
|
|
* |
29
|
|
|
* Initializes the object with the provided arguments. |
30
|
|
|
* |
31
|
|
|
* @param mixed[] $args |
32
|
|
|
*/ |
33
|
74 |
|
public function __construct(array $args = []) |
34
|
|
|
{ |
35
|
74 |
|
$this->setState($args); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the state of the object based on the provided arguments. |
40
|
|
|
* |
41
|
|
|
* This method updates the object's properties based on the keys in the |
42
|
|
|
* provided array. If a key corresponds to a property that exists in the |
43
|
|
|
* object, it will be set. Otherwise, it will be stored in the `_extraArgs` |
44
|
|
|
* array for later use. |
45
|
|
|
* |
46
|
|
|
* @param mixed[] $args |
47
|
|
|
*/ |
48
|
74 |
|
protected function setState(array $args = []): void |
49
|
|
|
{ |
50
|
74 |
|
foreach ($args as $key => $value) { |
51
|
70 |
|
$property = "_{$key}"; |
52
|
|
|
|
53
|
70 |
|
if (property_exists($this, $property)) { |
54
|
12 |
|
$this->{$property} = $value; |
55
|
|
|
|
56
|
12 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
59 |
|
if (property_exists($this, $key)) { |
60
|
21 |
|
$this->{$key} = $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
59 |
|
$this->_extraArgs[$key] = $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Magic method to access properties dynamically. |
69
|
|
|
* |
70
|
|
|
* @deprecated Use explicit getter methods instead. |
71
|
|
|
* @param string $name |
72
|
|
|
* @param mixed[] $args |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
2 |
|
public function __call(string $name, array $args = []): mixed |
76
|
|
|
{ |
77
|
|
|
// Add deprecation warning for magic method usage |
78
|
|
|
// This helps encourage developers to use explicit getter methods |
79
|
2 |
|
$commonMethods = ['id', 'name', 'description']; |
80
|
2 |
|
if (in_array(strtolower($name), $commonMethods)) { |
81
|
|
|
$methodName = 'get' . ucfirst($name); |
82
|
|
|
trigger_error( |
83
|
|
|
"Magic method $name() is deprecated. Use explicit method $methodName() instead.", |
84
|
|
|
E_USER_DEPRECATED |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$property = "_{$name}"; |
89
|
|
|
|
90
|
2 |
|
if (property_exists($this, $property)) { |
91
|
|
|
return $this->{$property}; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Retrieves the value of an extra argument by name. |
96
|
|
|
* |
97
|
|
|
* @param string $name The name of the extra argument to retrieve. |
98
|
|
|
* @return mixed The value of the extra argument if set, or false if not found. |
99
|
|
|
*/ |
100
|
2 |
|
if (isset($this->_extraArgs[$name])) { |
101
|
2 |
|
return $this->_extraArgs[$name]; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Parses the provided arguments and fills in defaults. |
109
|
|
|
* |
110
|
|
|
* For each key in $defaults that is not present in $args, the default value is added to $args. |
111
|
|
|
* |
112
|
|
|
* @param mixed[] $args The arguments to parse. |
113
|
|
|
* @param mixed[] $defaults The default values to use for missing arguments. |
114
|
|
|
* @return mixed[] The resulting array with defaults filled in. |
115
|
|
|
*/ |
116
|
24 |
|
public function parseArgs(array $args = [], array $defaults = []): array |
117
|
|
|
{ |
118
|
24 |
|
foreach ($defaults as $key => $value) { |
119
|
24 |
|
if (!array_key_exists($key, $args)) { |
120
|
24 |
|
$args[$key] = $value; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
24 |
|
return $args; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns all extra arguments as an associative array. |
129
|
|
|
* |
130
|
|
|
* @return mixed[] The array of extra arguments. |
131
|
|
|
*/ |
132
|
9 |
|
public function extraArgs(): array |
133
|
|
|
{ |
134
|
9 |
|
return $this->_extraArgs; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get ID value if it exists |
139
|
|
|
* |
140
|
|
|
* @return int The ID value or 0 if not found |
141
|
|
|
*/ |
142
|
|
|
public function getId(): int |
143
|
|
|
{ |
144
|
|
|
$id = $this->extraArgs()['id'] ?? 0; |
145
|
|
|
|
146
|
|
|
return intval($id); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get name value if it exists |
151
|
|
|
* |
152
|
|
|
* @return string The name value or empty string if not found |
153
|
|
|
*/ |
154
|
|
|
public function getName(): string |
155
|
|
|
{ |
156
|
|
|
$name = $this->extraArgs()['name'] ?? ''; |
157
|
|
|
|
158
|
|
|
return (string)$name; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get description value if it exists |
163
|
|
|
* |
164
|
|
|
* @return string The description value or empty string if not found |
165
|
|
|
*/ |
166
|
|
|
public function getDescription(): string |
167
|
|
|
{ |
168
|
|
|
$description = $this->extraArgs()['description'] ?? ''; |
169
|
|
|
|
170
|
|
|
return (string)$description; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|