Files
kiri-databases/Base/Model.php
T

832 lines
18 KiB
PHP
Raw Normal View History

2023-12-13 14:47:23 +08:00
<?php /** @noinspection ALL */
2022-01-09 03:49:51 +08:00
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database\Base;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
use ArrayAccess;
use Database\ActiveQuery;
2023-04-11 17:05:03 +08:00
use Database\Collection;
2022-01-09 03:49:51 +08:00
use Database\Connection;
2023-11-30 17:02:20 +08:00
use Database\DatabasesProviders;
2022-01-09 03:49:51 +08:00
use Database\ModelInterface;
use Database\Relation;
use Database\SqlBuilder;
use Exception;
2022-02-23 16:32:08 +08:00
use Kiri;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
2022-06-22 16:29:42 +08:00
use ReturnTypeWillChange;
2022-01-09 03:49:51 +08:00
use ReflectionException;
use validator\Validator;
/**
* Class BOrm
*
* @package Kiri\Abstracts
*
* @property bool $isNowExample
* @property array $attributes
* @property array $oldAttributes
*/
2023-11-13 17:52:38 +08:00
abstract class Model extends Component implements ModelInterface, ArrayAccess, \Arrayable
2022-01-09 03:49:51 +08:00
{
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/** @var array */
protected array $_attributes = [];
2023-04-05 10:15:50 +08:00
2023-04-10 17:13:24 +08:00
2023-07-31 23:26:58 +08:00
/** @var array */
protected array $_oldAttributes = [];
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/** @var null|string */
2023-12-22 11:41:24 +08:00
protected string $primary = '';
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @var bool
*/
protected bool $isNewExample = TRUE;
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @var bool
*/
protected bool $skipValidate = false;
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @var string
*/
protected string $table = '';
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @var string
*/
protected string $connection = 'db';
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @var array
*/
protected array $_with = [];
2023-04-05 10:15:50 +08:00
2023-07-31 23:26:58 +08:00
/**
* @throws Exception
*/
public function __construct()
{
parent::__construct();
$this->init();
}
/**
* @return array
*/
public function rules(): array
{
return [];
}
/**
* @param array $data
* @return Model
*/
public function setWith(array $data): static
{
$this->_with = $data;
return $this;
}
/**
* @return array
*/
public function getWith(): array
{
return $this->_with;
}
/**
* @return bool
*/
public function hasWith(): bool
{
return count($this->_with) > 0;
}
/**
* object init
*/
public function clean(): void
{
2023-11-14 15:01:30 +08:00
$this->_attributes = [];
2023-07-31 23:26:58 +08:00
$this->_oldAttributes = [];
}
/**
* @throws Exception
*/
public function init(): void
{
$container = Kiri::getDi();
$container->resolveProperties($container->getReflectionClass(get_called_class()), $this);
}
/**
* @return bool
*/
public function getIsNowExample(): bool
{
return $this->isNewExample === TRUE;
}
/**
* @param bool $bool
* @return $this
*/
public function setIsNowExample(bool $bool = FALSE): static
{
$this->isNewExample = $bool;
return $this;
}
/**
* @return string
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
* get last exception or other error
*/
public function getLastError(): string
{
2023-12-13 14:47:23 +08:00
return $this->getLogger()->getLastError('mysql');
2023-07-31 23:26:58 +08:00
}
/**
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function hasPrimary(): bool
{
2023-12-22 11:41:24 +08:00
return $this->primary != '';
2023-07-31 23:26:58 +08:00
}
/**
* @return null|string
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-22 11:41:24 +08:00
public function getPrimary(): string
2023-07-31 23:26:58 +08:00
{
if (!$this->hasPrimary()) {
2023-12-22 11:41:24 +08:00
return '';
2023-07-31 23:26:58 +08:00
}
return $this->primary;
}
/**
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function hasPrimaryValue(): bool
{
if ($this->hasPrimary()) {
return $this->getPrimaryValue() === null;
}
return false;
}
/**
* @return int|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function getPrimaryValue(): ?int
{
2023-12-22 11:41:24 +08:00
if ($this->hasPrimary()) {
return (int)$this->_oldAttributes[$this->getPrimary()] ?? null;
} else {
2023-08-17 17:33:28 +08:00
return null;
}
2023-07-31 23:26:58 +08:00
}
/**
* @param int|string|array $param
* @return Model|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
public static function findOne(int|string|array $param): ?static
2023-07-31 23:26:58 +08:00
{
2023-12-13 14:47:23 +08:00
$model = static::instance();
$query = new ActiveQuery($model);
$query->from($model->getTable())->alias('t1');
2023-07-31 23:26:58 +08:00
if (is_numeric($param)) {
2023-12-13 14:47:23 +08:00
$query->where([$model->getPrimary() => $param]);
2023-07-31 23:28:59 +08:00
} else if (is_array($param)) {
2023-12-13 14:47:23 +08:00
$query->where($param);
2023-07-31 23:28:59 +08:00
} else {
2023-12-13 14:47:23 +08:00
$query->whereRaw($param);
2023-07-31 23:26:58 +08:00
}
2023-12-13 16:25:17 +08:00
$data = $query->first();
if ($data === false) {
throw new Exception($model->getLastError());
}
return $data;
2023-07-31 23:26:58 +08:00
}
/**
* @param int $param
* @return Model|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
public static function primary(int $param): ?static
2023-07-31 23:26:58 +08:00
{
2023-12-13 14:47:23 +08:00
$model = static::instance();
$query = new ActiveQuery($model);
$query->from($model->getTable())->alias('t1')->where([$model->getPrimary() => $param]);
return $query->first();
2023-07-31 23:26:58 +08:00
}
2023-08-14 18:56:45 +08:00
/**
2023-12-13 14:47:23 +08:00
* @return bool|int
* @throws Exception
2023-08-14 18:56:45 +08:00
*/
2023-12-13 14:47:23 +08:00
public function optimize(): bool|int
2023-08-14 18:56:45 +08:00
{
2023-12-13 14:47:23 +08:00
return static::query()->execute('OPTIMIZE TABLE ' . $this->getTable());
2023-08-14 18:56:45 +08:00
}
2023-07-31 23:26:58 +08:00
/**
* @return static
*/
2023-12-13 14:47:23 +08:00
protected static function instance(): static
2023-07-31 23:26:58 +08:00
{
return new static();
}
/**
* @param int|string|array $condition
* @return static|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public static function first(int|string|array $condition): ?static
{
return static::findOne($condition);
}
/**
* @param string|array $condition
* @return Collection
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public static function all(string|array $condition): Collection
{
2023-12-13 14:47:23 +08:00
$model = new ActiveQuery(static::instance());
2023-07-31 23:26:58 +08:00
$model->from($model->getTable())->alias('t1');
2023-07-31 23:28:59 +08:00
if (is_array($condition)) {
$model->where($condition);
} else {
$model->whereRaw($condition);
}
2023-07-31 23:26:58 +08:00
return $model->get();
}
/**
* @return ActiveQuery
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public static function query(): ActiveQuery
{
2023-12-13 14:47:23 +08:00
$model = new ActiveQuery(static::instance());
2023-07-31 23:26:58 +08:00
$model->from($model->getTable())->alias('t1');
return $model;
}
/**
* @return Connection
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function getConnection(): Connection
{
2023-11-30 17:02:20 +08:00
return Kiri::getDi()->get(DatabasesProviders::class)->get($this->connection);
2023-07-31 23:26:58 +08:00
}
/**
2023-12-13 14:47:23 +08:00
* @param array|string $condition
2023-07-31 23:26:58 +08:00
* @param array $attributes
*
* @return bool
*/
2023-12-13 14:47:23 +08:00
protected static function deleteByCondition(array|string $condition = [], array $attributes = []): bool
2023-07-31 23:26:58 +08:00
{
2023-12-13 14:47:23 +08:00
$model = static::query();
$model->bindParams($attributes);
if (is_string($condition)) {
2023-07-31 23:26:58 +08:00
$model->whereRaw($condition);
2023-12-13 14:47:23 +08:00
} else {
$model->where($condition);
2023-07-31 23:26:58 +08:00
}
2023-12-13 14:47:23 +08:00
return $model->delete();
2023-07-31 23:26:58 +08:00
}
/**
* @return array
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function getAttributes(): array
{
2023-12-18 15:46:06 +08:00
return $this->_attributes;
2023-07-31 23:26:58 +08:00
}
/**
* @return array
*/
public function getOldAttributes(): array
{
return $this->_oldAttributes;
}
/**
2023-08-25 00:18:44 +08:00
* @param string $name
* @param mixed $value
2023-07-31 23:26:58 +08:00
* @return mixed
*/
2023-08-25 00:18:44 +08:00
public function setAttribute(string $name, mixed $value): mixed
2023-07-31 23:26:58 +08:00
{
$method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$value = $this->{$method}($value);
}
return $this->_attributes[$name] = $value;
}
/**
2023-08-25 00:18:44 +08:00
* @param string $name
* @param mixed $value
2023-07-31 23:26:58 +08:00
* @return mixed
*/
2023-08-25 00:18:44 +08:00
public function setOldAttribute(string $name, mixed $value): mixed
2023-07-31 23:26:58 +08:00
{
$method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$value = $this->{$method}($value);
}
return $this->_oldAttributes[$name] = $value;
}
/**
* @param array $param
* @return $this
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function setAttributes(array $param): static
{
foreach ($param as $key => $attribute) {
$this->setAttribute($key, $attribute);
}
return $this;
}
/**
* @param array $param
* @return $this
*/
public function setOldAttributes(array $param): static
{
foreach ($param as $key => $attribute) {
$this->setOldAttribute($key, $attribute);
}
return $this;
}
/**
* @return $this|bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
private function insert(): bool|static
{
2023-12-18 22:00:50 +08:00
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
2023-12-18 21:59:43 +08:00
$lastId = $this->getConnection()->createCommand($sql, $query->params)->save();
2023-07-31 23:26:58 +08:00
if ($lastId === false) {
return false;
}
2023-11-30 17:02:20 +08:00
if ($this->hasPrimary()) {
$this->_attributes[$this->getPrimary()] = $lastId;
}
return $this;
2023-07-31 23:26:58 +08:00
}
/**
* @param array $old
2023-07-31 23:28:59 +08:00
* @param array $condition
2023-07-31 23:26:58 +08:00
* @param array $change
* @return $this|bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-07-31 23:28:59 +08:00
protected function updateInternal(array $old, array $condition, array $change): bool|static
2023-07-31 23:26:58 +08:00
{
$query = static::query()->where($condition);
2023-08-02 14:46:01 +08:00
if (count($change) < 1) {
2023-08-02 14:37:59 +08:00
return true;
}
2023-07-31 23:26:58 +08:00
$generate = SqlBuilder::builder($query)->update($change);
if ($generate === false) {
return false;
}
2023-12-13 19:09:09 +08:00
if (!$this->getConnection()->createCommand($generate, $query->params)->save()) {
2023-07-31 23:26:58 +08:00
return FALSE;
}
2023-11-30 17:02:20 +08:00
return $this->refresh()->afterSave($old, $change);
2023-07-31 23:26:58 +08:00
}
/**
* @return bool|$this
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-08-25 00:16:49 +08:00
public function save(): static|bool
2023-07-31 23:26:58 +08:00
{
2023-08-25 10:00:48 +08:00
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
2023-07-31 23:26:58 +08:00
if (!$this->isNewExample) {
2023-12-14 09:50:49 +08:00
return $this->updateInternal(...$this->arrayIntersect($this->_attributes));
2023-07-31 23:26:58 +08:00
} else {
2023-08-25 10:00:48 +08:00
return $this->insert();
2023-07-31 23:26:58 +08:00
}
}
/**
2023-12-14 09:50:49 +08:00
* @return array<array, array, array>
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-14 09:50:49 +08:00
protected function arrayIntersect(array $params): array
{
$condition = [];
$oldPrams = [];
foreach ($this->_oldAttributes as $key => $attribute) {
if (!array_key_exists($key, $params) || $params[$key] == $attribute) {
$condition[$key] = $attribute;
unset($params[$key]);
} else {
2023-12-20 18:03:36 +08:00
$oldPrams[$key] = $this->_oldAttributes[$key];
2023-12-14 09:50:49 +08:00
}
2023-08-17 17:24:46 +08:00
}
2023-12-14 09:50:49 +08:00
return [$oldPrams, $condition, $params];
2023-07-31 23:26:58 +08:00
}
2023-12-18 15:01:08 +08:00
/**
* @return array
*/
public function getChanges(): array
{
if (!$this->isNewExample) {
2023-12-20 17:21:25 +08:00
return \array_intersect_assoc($this->_oldAttributes, $this->_attributes);
2023-12-18 15:01:08 +08:00
}
return $this->_attributes;
}
2023-07-31 23:26:58 +08:00
/**
2023-12-18 17:59:42 +08:00
* @param array $value
2023-07-31 23:26:58 +08:00
* @return $this
*/
2023-12-18 17:59:42 +08:00
public function populates(array $value): static
2023-07-31 23:26:58 +08:00
{
2023-11-14 15:01:30 +08:00
$this->_attributes = $value;
2023-07-31 23:26:58 +08:00
$this->_oldAttributes = $value;
$this->setIsNowExample();
return $this;
}
/**
* @param array $rule
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function validator(array $rule): bool
{
if (count($rule) < 1 || $this->skipValidate) {
return TRUE;
}
$validate = $this->resolve($rule);
2023-12-18 15:01:08 +08:00
if (!$validate->validation($this)) {
2023-12-18 17:39:43 +08:00
return \Kiri::getLogger()->failure($validate->getError() . PHP_EOL, 'mysql');
2023-07-31 23:26:58 +08:00
} else {
return TRUE;
}
}
2023-12-18 17:40:01 +08:00
2023-07-31 23:26:58 +08:00
/**
2023-12-18 17:59:42 +08:00
* @param array $rule
2023-07-31 23:26:58 +08:00
* @return Validator
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
private function resolve(array $rule): Validator
2023-07-31 23:26:58 +08:00
{
2023-12-18 15:01:08 +08:00
$validate = new Validator();
2023-07-31 23:26:58 +08:00
foreach ($rule as $val) {
$field = array_shift($val);
2023-12-18 15:01:08 +08:00
if (is_string($field)) {
$validate->make($this, [$field], $val);
} else {
$validate->make($this, $field, $val);
}
2023-07-31 23:26:58 +08:00
}
return $validate;
}
2023-12-18 17:11:57 +08:00
2023-07-31 23:26:58 +08:00
/**
* @param string $name
* @return null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-18 16:09:21 +08:00
public function getAttribute(string $name): mixed
2023-07-31 23:26:58 +08:00
{
return $this->_attributes[$name] ?? NULL;
}
/**
* @return Relation|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function getRelation(): ?Relation
{
return Kiri::getDi()->get(Relation::class);
}
/**
2023-12-18 17:59:42 +08:00
* @param string $attribute
2023-07-31 23:26:58 +08:00
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
public function has(string $attribute): bool
2023-07-31 23:26:58 +08:00
{
return true;
}
2023-12-18 17:11:57 +08:00
2023-07-31 23:26:58 +08:00
/**ƒ
* @return string
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function getTable(): string
{
2023-11-30 17:02:20 +08:00
$connection = $this->getConnection();
2023-07-31 23:26:58 +08:00
$tablePrefix = $connection->tablePrefix;
if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.');
}
$table = trim($this->table, '{%}');
if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) {
$table = $tablePrefix . $table;
}
return '`' . $connection->database . '`.' . $table;
}
/**
2023-12-18 17:59:42 +08:00
* @param array $oldAttributes
* @param array $changeAttributes
2023-07-31 23:26:58 +08:00
* @return bool
*/
2023-12-18 17:59:42 +08:00
public function afterSave(array $oldAttributes, array $changeAttributes): bool
2023-07-31 23:26:58 +08:00
{
return TRUE;
}
/**
* @param self $model
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function beforeSave(self $model): bool
{
return TRUE;
}
/**
* @return static
*/
public function refresh(): static
{
$this->_oldAttributes = $this->_attributes;
return $this;
}
2023-12-18 17:59:42 +08:00
2023-07-31 23:26:58 +08:00
/**
2023-12-18 17:59:42 +08:00
* @param string $name
* @param mixed $value
* @return void
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
public function __set(string $name, mixed $value): void
2023-07-31 23:26:58 +08:00
{
if ($this->hasRelateMethod($name, 'set')) {
$this->{'set' . ucfirst($name)}($value);
} else {
$method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$value = $this->{$method} ($value);
}
$this->_attributes[$name] = $value;
}
}
/**
2023-12-18 17:59:42 +08:00
* @param string $name
2023-07-31 23:26:58 +08:00
* @return mixed
*/
2023-12-18 17:59:42 +08:00
public function __get(string $name): mixed
2023-07-31 23:26:58 +08:00
{
$value = $this->_attributes[$name] ?? null;
if (!$this->hasRelateMethod($name)) {
return $this->withPropertyOverride($name, $value);
} else {
return $this->withRelate($name);
}
}
/**
2023-12-18 17:59:42 +08:00
* @param string $name
* @param mixed|null $value
2023-07-31 23:26:58 +08:00
* @return mixed
*/
2023-12-18 17:59:42 +08:00
protected function withPropertyOverride(string $name, mixed $value = null): mixed
2023-07-31 23:26:58 +08:00
{
$method = 'get' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
return $this->{$method}($value);
} else {
return $value;
}
}
/**
* @param string $name
* @param string $prefix
* @return bool
*/
protected function hasRelateMethod(string $name, string $prefix = 'get'): bool
{
return method_exists($this, $prefix . ucfirst($name));
}
/**
2023-12-18 17:59:42 +08:00
* @param string $name
* @return mixed
2023-07-31 23:26:58 +08:00
*/
2023-12-18 17:59:42 +08:00
protected function withRelate(string $name): mixed
2023-07-31 23:26:58 +08:00
{
$response = $this->{'get' . ucfirst($name)}();
if ($response instanceof \Database\Traits\Relation) {
$response = $response->get();
}
return $response;
}
/**
* @param $name
* @return bool
*/
2023-12-18 17:59:42 +08:00
public function __isset(string $name): bool
2023-07-31 23:26:58 +08:00
{
return isset($this->_attributes[$name]);
}
/**
* @param mixed $offset
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]);
}
/**
* @param mixed $offset
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
public function offsetGet(mixed $offset): mixed
{
return $this->__get($offset);
}
/**
* @param mixed $offset
* @param mixed $value
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-12 15:35:35 +08:00
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value): void
2023-07-31 23:26:58 +08:00
{
$this->__set($offset, $value);
}
/**
* @param mixed $offset
2023-12-12 15:35:35 +08:00
* @throws
2023-07-31 23:26:58 +08:00
*/
2023-12-12 15:35:35 +08:00
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset): void
2023-07-31 23:26:58 +08:00
{
2023-11-14 15:01:30 +08:00
if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
2023-07-31 23:26:58 +08:00
return;
}
unset($this->_attributes[$offset]);
unset($this->_oldAttributes[$offset]);
}
/**
* @param string ...$params
* @return array
*/
public function unset(string ...$params): array
{
return array_diff_assoc($params, $this->_attributes);
}
/**
* @param array $data
* @return static
* @throws
*/
public static function populate(array $data): static
{
2023-11-14 15:01:30 +08:00
$model = new static();
$model->_attributes = $data;
2023-07-31 23:26:58 +08:00
$model->_oldAttributes = $data;
$model->setIsNowExample();
return $model;
}
/**
* @param string $name
* @param array $arguments
* @return mixed
*/
public static function __callStatic(string $name, array $arguments)
{
return (new static())->{$name}(...$arguments);
}
2023-04-05 10:15:50 +08:00
2023-12-18 15:11:04 +08:00
/**
* @param string $field
* @return array
*/
2023-12-18 16:09:21 +08:00
public function getOldAttribute(string $field): mixed
2023-12-18 15:11:04 +08:00
{
2023-12-18 16:09:21 +08:00
return $this->_oldAttributes[$field] ?? null;
2023-12-18 15:11:04 +08:00
}
2022-01-09 03:49:51 +08:00
}