|
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
|
33 |
|
$this->{$property} = $value; |
|
55
|
33 |
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
59 |
|
$this->_extraArgs[$key] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Magic method to access properties dynamically. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param mixed[] $args |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function __call(string $name, array $args = []): mixed |
|
70
|
|
|
{ |
|
71
|
|
|
// Add deprecation warning for magic method usage |
|
72
|
|
|
// This helps encourage developers to use explicit getter methods |
|
73
|
2 |
|
$commonMethods = ['id', 'name', 'description']; |
|
74
|
2 |
|
if (in_array(strtolower($name), $commonMethods)) { |
|
75
|
|
|
$methodName = 'get' . ucfirst($name); |
|
76
|
|
|
trigger_error( |
|
77
|
|
|
"Magic method $name() is deprecated. Use explicit method $methodName() instead.", |
|
78
|
|
|
E_USER_DEPRECATED |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$property = "_{$name}"; |
|
83
|
|
|
|
|
84
|
2 |
|
if (property_exists($this, $property)) { |
|
85
|
|
|
return $this->{$property}; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
if (isset($this->_extraArgs[$name])) { |
|
89
|
2 |
|
return $this->_extraArgs[$name]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Parses the provided arguments and fills in defaults. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed[] $args |
|
99
|
|
|
* @param mixed[] $defaults |
|
100
|
|
|
* @return mixed[] |
|
101
|
|
|
*/ |
|
102
|
24 |
|
public function parseArgs(array $args = [], array $defaults = []): array |
|
103
|
|
|
{ |
|
104
|
24 |
|
foreach ($defaults as $key => $value) { |
|
105
|
24 |
|
if (!isset($args[$key])) { |
|
106
|
24 |
|
$args[$key] = $value; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
24 |
|
return $args; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return mixed[] |
|
115
|
|
|
*/ |
|
116
|
9 |
|
public function extraArgs(): array |
|
117
|
|
|
{ |
|
118
|
9 |
|
return $this->_extraArgs; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get ID value if it exists |
|
123
|
|
|
* |
|
124
|
|
|
* @return int The ID value or 0 if not found |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getId(): int |
|
127
|
|
|
{ |
|
128
|
|
|
$id = $this->extraArgs()['id'] ?? 0; |
|
129
|
|
|
return intval($id); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get name value if it exists |
|
134
|
|
|
* |
|
135
|
|
|
* @return string The name value or empty string if not found |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getName(): string |
|
138
|
|
|
{ |
|
139
|
|
|
$name = $this->extraArgs()['name'] ?? ''; |
|
140
|
|
|
return (string)$name; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get description value if it exists |
|
145
|
|
|
* |
|
146
|
|
|
* @return string The description value or empty string if not found |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getDescription(): string |
|
149
|
|
|
{ |
|
150
|
|
|
$description = $this->extraArgs()['description'] ?? ''; |
|
151
|
|
|
return (string)$description; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|