This commit is contained in:
2023-04-05 10:15:50 +08:00
parent bc3c286416
commit c86eb1a83d
3 changed files with 207 additions and 173 deletions
+22 -2
View File
@@ -42,8 +42,28 @@ class Getter
{ {
return isset($this->getter[$className]) && isset($this->getter[$className][$name]); return isset($this->getter[$className]) && isset($this->getter[$className][$name]);
} }
/**
* @param ModelInterface $class
* @param string $key
* @param mixed $value
* @return mixed
*/
public function override(ModelInterface $class, string $key, mixed $value): mixed
{
$method = $this->getter[$class::class][$key] ?? null;
if ($method !== null) {
return $class->{$method}($value);
}
return $value;
}
/**
* @param string $className
* @param string $name
* @return string|null
*/
public function get(string $className, string $name): ?string public function get(string $className, string $name): ?string
{ {
if (!$this->has($className,$name)) { if (!$this->has($className,$name)) {
+164 -169
View File
@@ -45,72 +45,73 @@ use validator\Validator;
*/ */
abstract class Model extends Component implements ModelInterface, ArrayAccess, ToArray abstract class Model extends Component implements ModelInterface, ArrayAccess, ToArray
{ {
const GET = 'get'; const GET = 'get';
const SET = 'set'; const SET = 'set';
/** @var array */ /** @var array */
protected array $_attributes = []; protected array $_attributes = [];
/** @var array */ /** @var array */
protected array $_oldAttributes = []; protected array $_oldAttributes = [];
/** @var array */ /** @var array */
protected array $_relate = []; protected array $_relate = [];
/** @var null|string */ /** @var null|string */
protected ?string $primary = NULL; protected ?string $primary = NULL;
/** /**
* @var array * @var array
*/ */
private array $_annotations = []; private array $_annotations = [];
/** /**
* @var bool * @var bool
*/ */
protected bool $isNewExample = TRUE; protected bool $isNewExample = TRUE;
/** /**
* @var array * @var array
*/ */
protected array $actions = []; protected array $actions = [];
/** /**
* @var string * @var string
*/ */
protected string $table = ''; protected string $table = '';
/** /**
* @var string * @var string
*/ */
protected string $connection = 'db'; protected string $connection = 'db';
/** /**
* @var array * @var array
*/ */
protected array $_with = []; protected array $_with = [];
/** /**
* @var array * @var Getter
*/ */
protected array $overrideGetter = []; #[Inject(Getter::class, [self::class])]
protected Getter $overrideGetter;
/** /**
* @var array * @var Setter
*/ */
#[Inject(Setter::class, [self::class])] #[Inject(Setter::class, [self::class])]
protected array $overrideSetter = []; protected Setter $overrideSetter;
/** /**
* @return array * @return array
*/ */
@@ -118,8 +119,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return []; return [];
} }
/** /**
* @param array $data * @param array $data
* @return Model * @return Model
@@ -129,8 +130,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->_with = $data; $this->_with = $data;
return $this; return $this;
} }
/** /**
* @return array * @return array
*/ */
@@ -138,8 +139,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->_with; return $this->_with;
} }
/** /**
* @return bool * @return bool
*/ */
@@ -147,8 +148,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return count($this->_with) > 0; return count($this->_with) > 0;
} }
/** /**
* object init * object init
*/ */
@@ -157,8 +158,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->_attributes = []; $this->_attributes = [];
$this->_oldAttributes = []; $this->_oldAttributes = [];
} }
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -167,8 +168,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$an = Kiri::getDi()->get(Annotation::class); $an = Kiri::getDi()->get(Annotation::class);
$an->injectProperty($this); $an->injectProperty($this);
} }
/** /**
* @return array * @return array
*/ */
@@ -176,8 +177,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->actions; return $this->actions;
} }
/** /**
* @return bool * @return bool
*/ */
@@ -185,8 +186,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->isNewExample === TRUE; return $this->isNewExample === TRUE;
} }
/** /**
* @param bool $bool * @param bool $bool
* @return $this * @return $this
@@ -196,7 +197,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->isNewExample = $bool; $this->isNewExample = $bool;
return $this; return $this;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -207,8 +208,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$logger = Kiri::getDi()->get(StdoutLoggerInterface::class); $logger = Kiri::getDi()->get(StdoutLoggerInterface::class);
return $logger->getLastError('mysql'); return $logger->getLastError('mysql');
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
@@ -224,8 +225,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return FALSE; return FALSE;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -233,7 +234,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->getAutoIncrement() !== NULL; return $this->getAutoIncrement() !== NULL;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -241,7 +242,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->getColumns()->getAutoIncrement(); return $this->getColumns()->getAutoIncrement();
} }
/** /**
* @return null|string * @return null|string
* @throws Exception * @throws Exception
@@ -253,8 +254,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $this->primary; return $this->primary;
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
@@ -266,8 +267,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return false; return false;
} }
/** /**
* @return int|null * @return int|null
* @throws Exception * @throws Exception
@@ -279,7 +280,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return null; return null;
} }
/** /**
* @param int|array|string|null $param * @param int|array|string|null $param
* @param null $db * @param null $db
@@ -298,8 +299,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return static::query()->where($param)->first(); return static::query()->where($param)->first();
} }
/** /**
* @param $param * @param $param
* @return array * @return array
@@ -316,8 +317,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return [$primary => $param]; return [$primary => $param];
} }
/** /**
* @param null $field * @param null $field
* @return ModelInterface|null * @return ModelInterface|null
@@ -340,8 +341,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $first[$field]; return $first[$field];
} }
/** /**
* @param string|int $param * @param string|int $param
* @return Model|null * @return Model|null
@@ -357,8 +358,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return static::query()->where([$columns => $param])->first(); return static::query()->where([$columns => $param])->first();
} }
/** /**
* @return static * @return static
*/ */
@@ -366,8 +367,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return new static(); return new static();
} }
/** /**
* @param $condition * @param $condition
* @return static|null * @return static|null
@@ -377,8 +378,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return static::query()->where($condition)->first(); return static::query()->where($condition)->first();
} }
/** /**
* @return ActiveQuery * @return ActiveQuery
*/ */
@@ -386,8 +387,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return new ActiveQuery(new static()); return new ActiveQuery(new static());
} }
/** /**
* @return Connection * @return Connection
* @throws Exception * @throws Exception
@@ -396,8 +397,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return Kiri::service()->get($this->connection); return Kiri::service()->get($this->connection);
} }
/** /**
* @param null $condition * @param null $condition
* @param array $attributes * @param array $attributes
@@ -414,8 +415,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return (bool)$model->delete(); return (bool)$model->delete();
} }
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
@@ -424,7 +425,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->toArray(); return $this->toArray();
} }
/** /**
* @return array * @return array
*/ */
@@ -432,7 +433,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->_oldAttributes; return $this->_oldAttributes;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
@@ -448,7 +449,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $this->_attributes[$name] = $value; return $this->_attributes[$name] = $value;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
@@ -462,7 +463,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $this->_oldAttributes[$name] = $value; return $this->_oldAttributes[$name] = $value;
} }
/** /**
* @param array $param * @param array $param
* @return $this * @return $this
@@ -478,8 +479,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $this; return $this;
} }
/** /**
* @param $param * @param $param
* @return $this * @return $this
@@ -495,8 +496,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $this; return $this;
} }
/** /**
* @param $attributes * @param $attributes
* @param $param * @param $param
@@ -507,22 +508,22 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
[$sql, $param] = SqlBuilder::builder(static::query())->insert($param); [$sql, $param] = SqlBuilder::builder(static::query())->insert($param);
$dbConnection = $this->getConnection()->createCommand($sql, $param); $dbConnection = $this->getConnection()->createCommand($sql, $param);
$lastId = $dbConnection->save(); $lastId = $dbConnection->save();
if ($this->isAutoIncrement()) { if ($this->isAutoIncrement()) {
$lastId = $this->setPrimary((int)$lastId, $param); $lastId = $this->setPrimary((int)$lastId, $param);
} else { } else {
$lastId = $this; $lastId = $this;
} }
$this->setIsNowExample(false); $this->setIsNowExample(false);
$this->refresh()->afterSave($attributes, $param); $this->refresh()->afterSave($attributes, $param);
return $lastId; return $lastId;
} }
/** /**
* @param $lastId * @param $lastId
* @param $param * @param $param
@@ -535,19 +536,19 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->setAttribute($this->getAutoIncrement(), (int)$lastId); $this->setAttribute($this->getAutoIncrement(), (int)$lastId);
return $this; return $this;
} }
if (!$this->hasPrimary()) { if (!$this->hasPrimary()) {
return $this; return $this;
} }
$primary = $this->getPrimary(); $primary = $this->getPrimary();
if (empty($param[$primary])) { if (empty($param[$primary])) {
$this->setAttribute($primary, (int)$lastId); $this->setAttribute($primary, (int)$lastId);
} }
return $this; return $this;
} }
/** /**
* @param $fields * @param $fields
* @param $condition * @param $condition
@@ -573,7 +574,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return FALSE; return FALSE;
} }
/** /**
* @param null $data * @param null $data
* @return bool|$this * @return bool|$this
@@ -594,8 +595,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $this->insert($change, $fields); return $this->insert($change, $fields);
} }
} }
/** /**
* @param $value * @param $value
* @return $this * @return $this
@@ -607,8 +608,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->setIsNowExample(FALSE); $this->setIsNowExample(FALSE);
return $this; return $this;
} }
/** /**
* @param array|null $rule * @param array|null $rule
* @return bool * @return bool
@@ -624,7 +625,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return TRUE; return TRUE;
} }
} }
/** /**
* @param $rule * @param $rule
* @return Validator * @return Validator
@@ -644,7 +645,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $validate; return $validate;
} }
/** /**
* @param string $name * @param string $name
* @return null * @return null
@@ -654,8 +655,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->_attributes[$name] ?? NULL; return $this->_attributes[$name] ?? NULL;
} }
/** /**
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
@@ -666,8 +667,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return call_user_func($this->_annotations[$type][$name], $value); return call_user_func($this->_annotations[$type][$name], $value);
} }
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
@@ -675,9 +676,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
private function separation(): array private function separation(): array
{ {
$assoc = array_diff_assoc($this->_attributes, $this->_oldAttributes); $assoc = array_diff_assoc($this->_attributes, $this->_oldAttributes);
$column = $this->getColumns(); $column = $this->getColumns();
$uassoc = array_intersect_assoc($this->_attributes, $this->_oldAttributes); $uassoc = array_intersect_assoc($this->_attributes, $this->_oldAttributes);
foreach ($assoc as $key => $item) { foreach ($assoc as $key => $item) {
$encode = $column->get_fields($key); $encode = $column->get_fields($key);
@@ -687,8 +688,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return [$assoc, $uassoc, array_keys($assoc)]; return [$assoc, $uassoc, array_keys($assoc)];
} }
/** /**
* @param $columns * @param $columns
* @param $format * @param $format
@@ -703,8 +704,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $value; return $value;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
@@ -713,8 +714,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
$this->_relate[$name] = $value; $this->_relate[$name] = $value;
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
@@ -723,8 +724,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return isset($this->_relate[$name]); return isset($this->_relate[$name]);
} }
/** /**
* @param array $relates * @param array $relates
*/ */
@@ -737,7 +738,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->setRelate($key, $val); $this->setRelate($key, $val);
} }
} }
/** /**
* @return array * @return array
*/ */
@@ -745,8 +746,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->_relate; return $this->_relate;
} }
/** /**
* @return Relation|null * @return Relation|null
*/ */
@@ -754,8 +755,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return Kiri::getDi()->get(Relation::class); return Kiri::getDi()->get(Relation::class);
} }
/** /**
* @param $attribute * @param $attribute
* @return bool * @return bool
@@ -765,7 +766,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return static::makeNewInstance()->getColumns()->hasField($attribute); return static::makeNewInstance()->getColumns()->hasField($attribute);
} }
/**ƒ /**ƒ
* @return string * @return string
* @throws Exception * @throws Exception
@@ -773,7 +774,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function getTable(): string public function getTable(): string
{ {
$connection = static::getConnection(); $connection = static::getConnection();
$tablePrefix = $connection->tablePrefix; $tablePrefix = $connection->tablePrefix;
if (empty($this->table)) { if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.'); throw new Exception('You need add static method `tableName` and return table name.');
@@ -784,8 +785,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return '`' . $connection->database . '`.' . $table; return '`' . $connection->database . '`.' . $table;
} }
/** /**
* @param $attributes * @param $attributes
* @param $changeAttributes * @param $changeAttributes
@@ -796,8 +797,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return TRUE; return TRUE;
} }
/** /**
* @param $model * @param $model
* @return bool * @return bool
@@ -806,8 +807,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return TRUE; return TRUE;
} }
/** /**
* @return static * @return static
*/ */
@@ -816,7 +817,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$this->_oldAttributes = $this->_attributes; $this->_oldAttributes = $this->_attributes;
return $this; return $this;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
@@ -827,17 +828,14 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$method = 'set' . ucfirst($name); $method = 'set' . ucfirst($name);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->{$method}($value); $this->{$method}($value);
return;
}
$method = $method . 'Attribute';
if (method_exists($this, $method)) {
$this->_attributes[$name] = $this->{$method}($value);
} else { } else {
$value = $this->overrideSetter->override($this, $name, $value);
$this->_attributes[$name] = $value; $this->_attributes[$name] = $value;
} }
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
@@ -847,11 +845,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
if (isset($this->_attributes[$name])) { if (isset($this->_attributes[$name])) {
return $this->withPropertyOverride($name); return $this->withPropertyOverride($name);
} else {
return $this->getRelateValue($name);
} }
return $this->getRelateValue($name);
} }
/** /**
* @param $name * @param $name
* @param null $value * @param null $value
@@ -863,15 +862,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (is_null($value)) { if (is_null($value)) {
$value = $this->_attributes[$name] ?? NULL; $value = $this->_attributes[$name] ?? NULL;
} }
$getter = Kiri::getDi()->get(Getter::class);
if ($getter->has(static::class, $name)) { return $this->overrideGetter->override($this, $name, $value);
return $this->{$getter->get(static::class, $name)}($value);
} else {
return $value;
}
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
@@ -880,8 +875,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return method_exists($this, 'get' . ucfirst($name)); return method_exists($this, 'get' . ucfirst($name));
} }
/** /**
* @param $name * @param $name
* @return mixed|null * @return mixed|null
@@ -894,8 +889,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $response; return $response;
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
@@ -911,8 +906,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $response; return $response;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
@@ -923,8 +918,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->getColumns()->_decode($name, $value); return $this->getColumns()->_decode($name, $value);
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
@@ -937,8 +932,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
return $data; return $data;
} }
/** /**
* @param $item * @param $item
* @param $data * @param $data
@@ -948,7 +943,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return call_user_func($item, $data); return call_user_func($item, $data);
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
@@ -957,8 +952,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return isset($this->_attributes[$name]); return isset($this->_attributes[$name]);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @return bool * @return bool
@@ -968,7 +963,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]); return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @return mixed * @return mixed
@@ -978,7 +973,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return $this->__get($offset); return $this->__get($offset);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @param mixed $value * @param mixed $value
@@ -988,7 +983,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
$this->__set($offset, $value); $this->__set($offset, $value);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @throws Exception * @throws Exception
@@ -1005,7 +1000,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
unset($this->_relate[$offset]); unset($this->_relate[$offset]);
} }
} }
/** /**
* @param string ...$params * @param string ...$params
* @return array * @return array
@@ -1014,8 +1009,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return array_diff_assoc($params, $this->_attributes); return array_diff_assoc($params, $this->_attributes);
} }
/** /**
* @return Columns * @return Columns
* @throws Exception * @throws Exception
@@ -1025,7 +1020,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $this->getConnection()->getSchema()->getColumns() return $this->getConnection()->getSchema()->getColumns()
->table($this->getTable()); ->table($this->getTable());
} }
/** /**
* @param array $data * @param array $data
* @return static * @return static
@@ -1039,8 +1034,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$model->setIsNowExample(FALSE); $model->setIsNowExample(FALSE);
return $model; return $model;
} }
/** /**
* @param $method * @param $method
* @param $value * @param $value
@@ -1052,8 +1047,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return $this->{$method}($value); return $this->{$method}($value);
}; };
} }
/** /**
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
@@ -1063,5 +1058,5 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
return (new static())->{$name}(...$arguments); return (new static())->{$name}(...$arguments);
} }
} }
+21 -2
View File
@@ -2,6 +2,8 @@
namespace Database\Base; namespace Database\Base;
use Database\ModelInterface;
class Setter class Setter
{ {
@@ -42,8 +44,25 @@ class Setter
{ {
return $this->setter[$className] ?? null; return $this->setter[$className] ?? null;
} }
/**
* @param ModelInterface $class
* @param string $key
* @param mixed $value
* @return mixed
*/
public function override(ModelInterface $class, string $key, mixed $value): mixed
{
$method = $this->setter[$class::class][$key] ?? null;
if ($method !== null) {
return $class->{$method}($value);
}
return $value;
}
/** /**
* @param string $className * @param string $className
* @param string $name * @param string $name