Fixe Lang::get for empty null in params + Add optionnal dynamic methods for models getXyAttribute and SetXyAttribute to allow modifie the manner who properties are get/set dyanmically

This commit is contained in:
Thommy Bucaille 2024-02-10 14:53:26 +01:00
parent 2d44fe236f
commit 56f22c1b58
2 changed files with 14 additions and 1 deletions

View File

@ -302,11 +302,17 @@ class BaseModel implements \IteratorAggregate {
// When call property that not exists in this class
public function __get($name) {
if(empty($this->datas)) {
$this->datas = new Collection();
}
// Handle dynamic attributes
if(method_exists($this, 'get'.ucfirst($name).'Attribute')) {
return call_user_func([$this, 'get'.ucfirst($name).'Attribute'], $this->datas->$name ?? null);
}
// Return the value if exists
if(isset($this->datas->$name)) {
return $this->datas->$name;
}
@ -318,9 +324,15 @@ class BaseModel implements \IteratorAggregate {
$this->datas = new Collection();
}
// Handle dynamic attributes
if(method_exists($this, 'set'.ucfirst($name).'Attribute')) {
$value = call_user_func([$this, 'set'.ucfirst($name).'Attribute'], $value);
}
$this->datas->$name = $value;
$this->touch($name);
}
public function __isset($name) {
return isset($this->datas->$name);
}

View File

@ -135,6 +135,7 @@ class Lang {
$key = $instance->getKey($key);
if (count($params) > 0) {
foreach ($params as $k => $v) {
if(empty($k) || empty($v)) continue;
$key = str_replace(':' . $k, $v, $key);
}
}