From 5c9f92f95539e52cec4792432c58a283a01b104e Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Thu, 30 Mar 2023 20:39:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Get.php | 17 ++- Annotation/Set.php | 4 +- Base/Getter.php | 49 ++++++- Base/Model.php | 310 +++++++++++++++++++++++---------------------- Base/Setter.php | 56 +++++++- Model.php | 6 +- 6 files changed, 275 insertions(+), 167 deletions(-) diff --git a/Annotation/Get.php b/Annotation/Get.php index 43fed28..1004b76 100644 --- a/Annotation/Get.php +++ b/Annotation/Get.php @@ -5,6 +5,7 @@ namespace Database\Annotation; use Attribute; +use Database\Base\Getter; use Kiri\Annotation\AbstractAttribute; @@ -14,8 +15,8 @@ use Kiri\Annotation\AbstractAttribute; */ #[Attribute(Attribute::TARGET_METHOD)] class Get extends AbstractAttribute { - - + + /** * Get constructor. * @param string $name @@ -23,17 +24,19 @@ use Kiri\Annotation\AbstractAttribute; public function __construct(public string $name) { } - - + + /** * @param mixed $class * @param mixed|null $method * @return bool */ - public function execute(mixed $class, mixed $method = null): bool + public function execute(mixed $class, mixed $method = null): bool { + $keys = \Kiri::getDi()->get(Getter::class); + $keys->write($this->name, $class, $method); return true; } - - + + } diff --git a/Annotation/Set.php b/Annotation/Set.php index ee5ec5e..f39876d 100644 --- a/Annotation/Set.php +++ b/Annotation/Set.php @@ -4,8 +4,8 @@ namespace Database\Annotation; +use Database\Base\Setter; use Kiri\Annotation\AbstractAttribute; -use Exception; #[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute { @@ -27,6 +27,8 @@ use Exception; */ public function execute(mixed $class, mixed $method = null): bool { + $keys = \Kiri::getDi()->get(Setter::class); + $keys->write($this->name, $class, $method); return true; } diff --git a/Base/Getter.php b/Base/Getter.php index fcfe010..f6bfef8 100644 --- a/Base/Getter.php +++ b/Base/Getter.php @@ -2,7 +2,54 @@ namespace Database\Base; +use Database\ModelInterface; + class Getter { - + + private array $getter = []; + + + /** + * @param string $name + * @param string $className + * @param string $method + * @return void + */ + public function write(string $name, string $className, string $method): void + { + if (!isset($this->getter[$className])) { + $this->getter[$className] = []; + } + $this->getter[$className][$name] = $method; + } + + /** + * @param string $className + * @return array + */ + public function getAll(string $className): array + { + return $this->getter[$className] ?? []; + } + + /** + * @param string $className + * @param string $name + * @return bool + */ + public function has(string $className, string $name): bool + { + return isset($this->getter[$className]) && isset($this->getter[$className][$name]); + } + + + public function get(string $className, string $name): ?string + { + if (!$this->has($className,$name)) { + return null; + } + return $this->getter[$className][$name]; + } + } diff --git a/Base/Model.php b/Base/Model.php index 1d606b6..b0ed56e 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -44,60 +44,60 @@ use validator\Validator; */ abstract class Model extends Component implements ModelInterface, ArrayAccess, ToArray { - + const GET = 'get'; - - + + const SET = 'set'; - + /** @var array */ protected array $_attributes = []; - + /** @var array */ protected array $_oldAttributes = []; - + /** @var array */ protected array $_relate = []; - + /** @var null|string */ protected ?string $primary = NULL; - + /** * @var array */ private array $_annotations = []; - - + + /** * @var bool */ protected bool $isNewExample = TRUE; - - + + /** * @var array */ protected array $actions = []; - - + + /** * @var string */ protected string $table = ''; - - + + /** * @var string */ protected string $connection = 'db'; - - + + /** * @var array */ private array $_with = []; - - + + /** * @return array */ @@ -105,8 +105,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return []; } - - + + /** * @param $data * @return Model @@ -119,8 +119,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->_with = $data; return $this; } - - + + /** * @return array|null */ @@ -128,8 +128,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->_with; } - - + + /** * object init */ @@ -138,8 +138,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->_attributes = []; $this->_oldAttributes = []; } - - + + /** * @throws Exception */ @@ -148,8 +148,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $an = Kiri::getDi()->get(Annotation::class); $an->injectProperty($this); } - - + + /** * @return array */ @@ -157,8 +157,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->actions; } - - + + /** * @return bool */ @@ -166,8 +166,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->isNewExample === TRUE; } - - + + /** * @param bool $bool * @return $this @@ -177,7 +177,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->isNewExample = $bool; return $this; } - + /** * @return string * @throws Exception @@ -188,8 +188,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $logger = Kiri::getDi()->get(StdoutLoggerInterface::class); return $logger->getLastError('mysql'); } - - + + /** * @return bool * @throws Exception @@ -205,8 +205,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return FALSE; } - - + + /** * @throws Exception */ @@ -214,7 +214,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->getAutoIncrement() !== NULL; } - + /** * @throws Exception */ @@ -222,7 +222,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->getColumns()->getAutoIncrement(); } - + /** * @return null|string * @throws Exception @@ -234,8 +234,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $this->primary; } - - + + /** * @return bool * @throws Exception @@ -247,8 +247,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return false; } - - + + /** * @return int|null * @throws Exception @@ -260,7 +260,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return null; } - + /** * @param int|array|string|null $param * @param null $db @@ -279,8 +279,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return static::query()->where($param)->first(); } - - + + /** * @param $param * @return array @@ -297,8 +297,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return [$primary => $param]; } - - + + /** * @param null $field * @return ModelInterface|null @@ -321,8 +321,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $first[$field]; } - - + + /** * @param string|int $param * @return Model|null @@ -338,8 +338,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return static::query()->where([$columns => $param])->first(); } - - + + /** * @return static */ @@ -347,8 +347,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return new static(); } - - + + /** * @param $condition * @return static|null @@ -358,8 +358,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return static::query()->where($condition)->first(); } - - + + /** * @return ActiveQuery */ @@ -367,8 +367,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return new ActiveQuery(new static()); } - - + + /** * @return Connection * @throws Exception @@ -377,8 +377,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return Kiri::service()->get($this->connection); } - - + + /** * @param null $condition * @param array $attributes @@ -395,8 +395,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return (bool)$model->delete(); } - - + + /** * @return array * @throws Exception @@ -405,7 +405,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->toArray(); } - + /** * @return array */ @@ -413,7 +413,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->_oldAttributes; } - + /** * @param $name * @param $value @@ -422,12 +422,14 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function setAttribute($name, $value): mixed { - if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) { - $value = $this->{'set' . ucfirst($name) . 'Attribute'}($value); + $keys = Kiri::getDi()->get(Setter::class); + if ($keys->has(static::class, $name)) { + $method = $keys->get(static::class, $name); + $value = $this->{$method}($value); } return $this->_attributes[$name] = $value; } - + /** * @param $name * @param $value @@ -441,7 +443,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $this->_oldAttributes[$name] = $value; } - + /** * @param array $param * @return $this @@ -457,8 +459,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $this; } - - + + /** * @param $param * @return $this @@ -474,8 +476,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $this; } - - + + /** * @param $attributes * @param $param @@ -486,22 +488,22 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { [$sql, $param] = SqlBuilder::builder(static::query())->insert($param); $dbConnection = $this->getConnection()->createCommand($sql, $param); - + $lastId = $dbConnection->save(); if ($this->isAutoIncrement()) { $lastId = $this->setPrimary((int)$lastId, $param); } else { $lastId = $this; } - + $this->setIsNowExample(false); - + $this->refresh()->afterSave($attributes, $param); - + return $lastId; } - - + + /** * @param $lastId * @param $param @@ -514,19 +516,19 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->setAttribute($this->getAutoIncrement(), (int)$lastId); return $this; } - + if (!$this->hasPrimary()) { return $this; } - + $primary = $this->getPrimary(); if (empty($param[$primary])) { $this->setAttribute($primary, (int)$lastId); } return $this; } - - + + /** * @param $fields * @param $condition @@ -552,7 +554,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return FALSE; } - + /** * @param null $data * @return bool|$this @@ -573,8 +575,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return $this->insert($change, $fields); } } - - + + /** * @param $value * @return $this @@ -586,8 +588,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->setIsNowExample(FALSE); return $this; } - - + + /** * @param array|null $rule * @return bool @@ -603,7 +605,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return TRUE; } } - + /** * @param $rule * @return Validator @@ -623,7 +625,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $validate; } - + /** * @param string $name * @return null @@ -633,8 +635,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->_attributes[$name] ?? NULL; } - - + + /** * @param string $name * @param mixed $value @@ -645,8 +647,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return call_user_func($this->_annotations[$type][$name], $value); } - - + + /** * @return array * @throws Exception @@ -654,9 +656,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T private function separation(): array { $assoc = array_diff_assoc($this->_attributes, $this->_oldAttributes); - + $column = $this->getColumns(); - + $uassoc = array_intersect_assoc($this->_attributes, $this->_oldAttributes); foreach ($assoc as $key => $item) { $encode = $column->get_fields($key); @@ -666,8 +668,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return [$assoc, $uassoc, array_keys($assoc)]; } - - + + /** * @param $columns * @param $format @@ -682,8 +684,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $value; } - - + + /** * @param $name * @param $value @@ -692,8 +694,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { $this->_relate[$name] = $value; } - - + + /** * @param $name * @return bool @@ -702,8 +704,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return isset($this->_relate[$name]); } - - + + /** * @param array $relates */ @@ -716,7 +718,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->setRelate($key, $val); } } - + /** * @return array */ @@ -724,8 +726,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->_relate; } - - + + /** * @return Relation|null */ @@ -733,8 +735,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return Kiri::getDi()->get(Relation::class); } - - + + /** * @param $attribute * @return bool @@ -744,7 +746,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return static::makeNewInstance()->getColumns()->hasField($attribute); } - + /**ƒ * @return string * @throws Exception @@ -752,7 +754,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T public function getTable(): string { $connection = static::getConnection(); - + $tablePrefix = $connection->tablePrefix; if (empty($this->table)) { throw new Exception('You need add static method `tableName` and return table name.'); @@ -763,8 +765,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return '`' . $connection->database . '`.' . $table; } - - + + /** * @param $attributes * @param $changeAttributes @@ -775,8 +777,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return TRUE; } - - + + /** * @param $model * @return bool @@ -785,8 +787,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return TRUE; } - - + + /** * @return static */ @@ -795,7 +797,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->_oldAttributes = $this->_attributes; return $this; } - + /** * @param $name * @param $value @@ -815,8 +817,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $this->_attributes[$name] = $value; } } - - + + /** * @param $name * @return mixed @@ -829,8 +831,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $this->getRelateValue($name); } - - + + /** * @param $name * @param null $value @@ -842,15 +844,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T if (is_null($value)) { $value = $this->_attributes[$name] ?? NULL; } - $method = 'get' . ucfirst($name) . 'Attribute'; - if (!method_exists($this, $method)) { + $getter = Kiri::getDi()->get(Getter::class); + if ($getter->has(static::class, $name)) { + return $this->{$getter->get(static::class, $name)}($value); + } else { return $value; - return $this->_decode($name, $value); } - return $this->{$method}($value); } - - + + /** * @param $name * @return bool @@ -859,8 +861,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return method_exists($this, 'get' . ucfirst($name)); } - - + + /** * @param $name * @return mixed|null @@ -873,8 +875,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $response; } - - + + /** * @param $name * @return mixed @@ -890,8 +892,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $response; } - - + + /** * @param $name * @param $value @@ -902,8 +904,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->getColumns()->_decode($name, $value); } - - + + /** * @param $name * @return mixed @@ -916,8 +918,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } return $data; } - - + + /** * @param $item * @param $data @@ -927,7 +929,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return call_user_func($item, $data); } - + /** * @param $name * @return bool @@ -936,8 +938,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return isset($this->_attributes[$name]); } - - + + /** * @param mixed $offset * @return bool @@ -947,7 +949,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]); } - + /** * @param mixed $offset * @return mixed @@ -957,7 +959,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return $this->__get($offset); } - + /** * @param mixed $offset * @param mixed $value @@ -967,7 +969,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { $this->__set($offset, $value); } - + /** * @param mixed $offset * @throws Exception @@ -984,7 +986,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T unset($this->_relate[$offset]); } } - + /** * @param string ...$params * @return array @@ -993,8 +995,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return array_diff_assoc($params, $this->_attributes); } - - + + /** * @return Columns * @throws Exception @@ -1004,7 +1006,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return $this->getConnection()->getSchema()->getColumns() ->table($this->getTable()); } - + /** * @param array $data * @return static @@ -1018,8 +1020,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T $model->setIsNowExample(FALSE); return $model; } - - + + /** * @param $method * @param $value @@ -1031,8 +1033,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return $this->{$method}($value); }; } - - + + /** * @param string $name * @param array $arguments @@ -1042,5 +1044,5 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { return (new static())->{$name}(...$arguments); } - + } diff --git a/Base/Setter.php b/Base/Setter.php index 6f11271..779286c 100644 --- a/Base/Setter.php +++ b/Base/Setter.php @@ -4,6 +4,58 @@ namespace Database\Base; class Setter { - - + + private array $setter = []; + + + /** + * @param string $name + * @param string $className + * @param string $method + * @return void + */ + public function write(string $name, string $className, string $method): void + { + if (!isset($this->setter[$className])) { + $this->setter[$className] = []; + } + $this->setter[$className][$name] = $method; + } + + + /** + * @param string $className + * @param string $name + * @return bool + */ + public function has(string $className, string $name): bool + { + return isset($this->setter[$className]) && isset($this->setter[$className][$name]); + } + + + /** + * @param string $className + * @return array|null + */ + public function getAll(string $className): ?array + { + return $this->setter[$className] ?? null; + } + + + /** + * @param string $className + * @param string $name + * @return string|null + */ + public function get(string $className, string $name): ?string + { + if (!$this->has($className, $name)) { + return null; + } + return $this->setter[$className][$name]; + } + + } diff --git a/Model.php b/Model.php index 4385c49..af0699f 100644 --- a/Model.php +++ b/Model.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Database; +use Database\Base\Getter; use Exception; use Kiri; use Kiri\Exception\NotFindClassException; @@ -264,8 +265,9 @@ class Model extends Base\Model public function toArray(): array { $data = $this->_attributes; - foreach ($data as $key => $datum) { - $data[$key] = $this->withPropertyOverride($key, $datum); + $keys = Kiri::getDi()->get(Getter::class); + foreach ($keys->getAll(static::class) as $key => $datum) { + $data[$key] = $this->{$datum}($data[$key]); } return $this->withRelates($data); }