mirror of
https://github.com/standardebooks/web.git
synced 2025-07-16 03:16:36 -04:00
Change Accessor from class to trait
This commit is contained in:
parent
10bea9ad9e
commit
41dd9db4aa
13 changed files with 43 additions and 24 deletions
45
lib/Traits/Accessor.php
Normal file
45
lib/Traits/Accessor.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?
|
||||
namespace Traits;
|
||||
|
||||
trait Accessor{
|
||||
public function __get(string $var): mixed{
|
||||
$function = 'Get' . $var;
|
||||
$privateVar = '_' . $var;
|
||||
|
||||
if(method_exists($this, $function)){
|
||||
return $this->$function();
|
||||
}
|
||||
elseif(property_exists($this, $var . 'Id') && property_exists($this, $privateVar) && method_exists($var, 'Get')){
|
||||
// If we're asking for a private `_Var` property,
|
||||
// and we have a public `VarId` property,
|
||||
// and the `Var` class also has a `Var::Get` method,
|
||||
// call that method and return the result.
|
||||
if($this->$privateVar === null && $this->{$var . 'Id'} !== null){
|
||||
$this->$privateVar = $var::Get($this->{$var . 'Id'});
|
||||
}
|
||||
|
||||
return $this->$privateVar;
|
||||
}
|
||||
elseif(property_exists($this, $privateVar)){
|
||||
return $this->{$privateVar};
|
||||
}
|
||||
else{
|
||||
return $this->$var;
|
||||
}
|
||||
}
|
||||
|
||||
public function __set(string $var, mixed $val): void{
|
||||
$function = 'Set' . $var;
|
||||
$privateVar = '_' . $var;
|
||||
|
||||
if(method_exists($this, $function)){
|
||||
$this->$function($val);
|
||||
}
|
||||
elseif(property_exists($this, $privateVar)){
|
||||
$this->$privateVar = $val;
|
||||
}
|
||||
else{
|
||||
$this->$var = $val;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue