18
18
use function in_array ;
19
19
use function method_exists ;
20
20
use function property_exists ;
21
- use function ucfirst ;
22
21
23
22
/**
24
23
* Trait to define magic methods to access values of an ActiveRecord instance.
@@ -62,6 +61,11 @@ trait MagicPropertiesTrait
62
61
*/
63
62
public function __get (string $ name )
64
63
{
64
+ if (method_exists ($ this , $ getter = "get $ name " )) {
65
+ /** Read getter, e.g., getName() */
66
+ return $ this ->$ getter ();
67
+ }
68
+
65
69
if ($ this ->hasProperty ($ name )) {
66
70
return $ this ->get ($ name );
67
71
}
@@ -70,17 +74,12 @@ public function __get(string $name)
70
74
return $ this ->getRelatedRecords ()[$ name ];
71
75
}
72
76
73
- if (method_exists ($ this , $ getter = 'get ' . ucfirst ($ name ))) {
74
- /** Read getter, e.g., getName() */
75
- return $ this ->$ getter ();
76
- }
77
-
78
- if (method_exists ($ this , 'get ' . ucfirst ($ name ) . 'Query ' )) {
77
+ if (method_exists ($ this , "get {$ name }Query " )) {
79
78
/** Read relation query getter, e.g., getUserQuery() */
80
79
return $ this ->retrieveRelation ($ name );
81
80
}
82
81
83
- if (method_exists ($ this , ' set ' . ucfirst ( $ name) )) {
82
+ if (method_exists ($ this , " set $ name" )) {
84
83
throw new InvalidCallException ('Getting write-only property: ' . static ::class . ':: ' . $ name );
85
84
}
86
85
@@ -131,19 +130,19 @@ public function __unset(string $name): void
131
130
*/
132
131
public function __set (string $ name , mixed $ value ): void
133
132
{
134
- if ($ this -> hasProperty ( $ name )) {
135
- parent :: set ( $ name , $ value );
133
+ if (method_exists ( $ this , $ setter = " set $ name" )) {
134
+ $ this -> $ setter ( $ value );
136
135
return ;
137
136
}
138
137
139
- if (method_exists ( $ this , $ setter = ' set ' . ucfirst ($ name) )) {
140
- $ this -> $ setter ( $ value );
138
+ if ($ this -> hasProperty ($ name )) {
139
+ parent :: set ( $ name , $ value );
141
140
return ;
142
141
}
143
142
144
143
if (
145
- method_exists ($ this , ' get ' . ucfirst ( $ name) )
146
- || method_exists ($ this , ' get ' . ucfirst ( $ name) . ' Query ' )
144
+ method_exists ($ this , " get $ name" )
145
+ || method_exists ($ this , " get { $ name} Query " )
147
146
) {
148
147
throw new InvalidCallException ('Setting read-only property: ' . static ::class . ':: ' . $ name );
149
148
}
@@ -184,24 +183,24 @@ public function set(string $propertyName, mixed $value): void
184
183
*/
185
184
public function isProperty (string $ name , bool $ checkVars = true ): bool
186
185
{
187
- return method_exists ($ this , ' get ' . ucfirst ( $ name) )
188
- || method_exists ($ this , ' set ' . ucfirst ( $ name) )
189
- || method_exists ($ this , ' get ' . ucfirst ( $ name) . ' Query ' )
186
+ return method_exists ($ this , " get $ name" )
187
+ || method_exists ($ this , " set $ name" )
188
+ || method_exists ($ this , " get { $ name} Query " )
190
189
|| ($ checkVars && property_exists ($ this , $ name ))
191
190
|| $ this ->hasProperty ($ name );
192
191
}
193
192
194
193
public function canGetProperty (string $ name , bool $ checkVars = true ): bool
195
194
{
196
- return method_exists ($ this , ' get ' . ucfirst ( $ name) )
197
- || method_exists ($ this , ' get ' . ucfirst ( $ name) . ' Query ' )
195
+ return method_exists ($ this , " get $ name" )
196
+ || method_exists ($ this , " get { $ name} Query " )
198
197
|| ($ checkVars && property_exists ($ this , $ name ))
199
198
|| $ this ->hasProperty ($ name );
200
199
}
201
200
202
201
public function canSetProperty (string $ name , bool $ checkVars = true ): bool
203
202
{
204
- return method_exists ($ this , ' set ' . ucfirst ( $ name) )
203
+ return method_exists ($ this , " set $ name" )
205
204
|| ($ checkVars && property_exists ($ this , $ name ))
206
205
|| $ this ->hasProperty ($ name );
207
206
}
0 commit comments