diff --git a/Base/AbstractCollection.php b/Base/AbstractCollection.php index cc8a808..921e1b0 100644 --- a/Base/AbstractCollection.php +++ b/Base/AbstractCollection.php @@ -13,11 +13,11 @@ namespace Database\Base; use ArrayIterator; use Database\ActiveQuery; use Database\ModelInterface; -use Kiri\ToArray; use Exception; use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Component; -use ReturnTypeWillChange; +use Kiri\ReturnTypeWillChange; +use Kiri\ToArray; use Traversable; /** diff --git a/Base/Model.php b/Base/Model.php index 83b4ae3..ffa715c 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -17,8 +17,6 @@ use ArrayAccess; use Closure; use Database\ActiveQuery; use Database\Connection; -use Database\HasMany; -use Database\HasOne; use Database\ModelInterface; use Database\Mysql\Columns; use Database\Relation; @@ -30,9 +28,9 @@ use Kiri\Abstracts\Component; use Kiri\Annotation\Annotation; use Kiri\Error\StdoutLoggerInterface; use Kiri\Exception\NotFindClassException; +use ReturnTypeWillChange; use Kiri\ToArray; use ReflectionException; -use ReturnTypeWillChange; use validator\Validator; /** @@ -109,59 +107,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } - /** - * @param string $name - * @param mixed $value - * @return mixed - * @throws ReflectionException - */ - private function _setter(string $name, mixed $value): mixed - { - $method = di(Setter::class)->getSetter(static::class, $name); - if (!empty($method)) { - $value = $this->{$method}($value); - } - return $value; - } - - - /** - * @param string $name - * @param $value - * @return mixed - * @throws ReflectionException - */ - private function _getter(string $name, $value): mixed - { - $data = di(Getter::class)->getGetter(static::class, $name); - if (empty($data)) { - return $this->_relater($name, $value); - } - return $this->{$data}($value); - } - - - /** - * @param string $name - * @param $value - * @return mixed - * @throws ReflectionException - * @throws Exception - */ - private function _relater(string $name, $value): mixed - { - $data = di(Relate::class)->getRelate(static::class, $name); - if (!empty($data)) { - $data = $this->{$data}(); - if ($data instanceof HasBase) { - return $data->get(); - } - return $data; - } - return $this->_decode($name, $value); - } - - /** * @param $data * @return Model @@ -310,10 +255,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function getPrimaryValue(): ?int { - if (!$this->hasPrimary()) { - return NULL; + if ($this->hasPrimary()) { + return $this->getAttribute($this->primary); } - return $this->getAttribute($this->primary); + return null; } /** @@ -407,8 +352,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T /** * @param $condition * @return static|null - * @throws NotFindClassException - * @throws ReflectionException * @throws Exception */ public static function first($condition): ?static @@ -428,10 +371,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T /** * @return Connection + * @throws Exception */ public function getConnection(): Connection { - return Kiri::app()->get($this->connection); + return Kiri::service()->get($this->connection); } @@ -484,7 +428,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function setAttribute($name, $value): mixed { - return $this->_attributes[$name] = $this->_setter($name, $value); + if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) { + $value = $this->{'set' . ucfirst($name) . 'Attribute'}($value); + } + return $this->_attributes[$name] = $value; } /** @@ -495,7 +442,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function setOldAttribute($name, $value): mixed { - return $this->_oldAttributes[$name] = $this->_setter($name, $value); + if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) { + $value = $this->{'set' . ucfirst($name) . 'Attribute'}($value); + } + return $this->_oldAttributes[$name] = $value; } /** @@ -687,9 +637,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function getAttribute(string $name) { - if ($this->hasAnnotation($name)) { - return $this->runAnnotation($name, $this->_attributes[$name]); - } return $this->_attributes[$name] ?? NULL; } @@ -866,10 +813,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function __set($name, $value): void { - if (method_exists($this, 'set' . ucfirst($name))) { - $this->{'set' . ucfirst($name)}($value); + $method = 'set' . ucfirst($name) . 'Attribute'; + if (method_exists($this, $method)) { + $this->_attributes[$name] = $this->{$method}($value); } else { - $this->_attributes[$name] = $this->_setter($name, $value); + $this->_attributes[$name] = $value; } } @@ -881,13 +829,70 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function __get($name): mixed { - $method = 'get' . ucfirst($name); - if (method_exists($this, $method)) { - return $this->{$method}(); + if (isset($this->_attributes[$name])) { + return $this->withPropertyOverride($name); } - $value = $this->_attributes[$name] ?? NULL; + return $this->getRelateValue($name); + } - return $this->_getter($name, $value); + + /** + * @param $name + * @param null $value + * @return mixed + * @throws Exception + */ + protected function withPropertyOverride($name, $value = null): mixed + { + if (is_null($value)) { + $value = $this->_attributes[$name] ?? NULL; + } + $method = 'get' . ucfirst($name) . 'Attribute'; + if (!method_exists($this, $method)) { + return $this->_decode($name, $value); + } + return $this->{$method}($value); + } + + + /** + * @param $name + * @return bool + */ + protected function hasRelateMethod($name): bool + { + return method_exists($this, 'get' . ucfirst($name)); + } + + + /** + * @param $name + * @return mixed|null + */ + protected function withRelate($name): mixed + { + $response = $this->getRelateValue($name); + if ($response instanceof ToArray) { + $response = $response->toArray(); + } + return $response; + } + + + /** + * @param $name + * @return mixed + */ + protected function getRelateValue($name): mixed + { + if (!$this->hasRelateMethod($name)) { + return null; + } + $response = $this->{'get' . ucfirst($name)}(); + if ($response instanceof HasBase) { + $response = $response->get(); + } + return $response; } @@ -897,7 +902,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T * @return mixed * @throws Exception */ - private function _decode($name, $value): mixed + protected function _decode($name, $value): mixed { return $this->getColumns()->_decode($name, $value); } @@ -917,30 +922,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } - /** - * @param string $type - * @return array - */ - protected function getAnnotation(string $type = self::GET): array - { - return $this->_annotations[$type] ?? []; - } - - - /** - * @param $name - * @param string $type - * @return bool - */ - protected function hasAnnotation($name, string $type = self::GET): bool - { - if (!isset($this->_annotations[$type])) { - return FALSE; - } - return isset($this->_annotations[$type][$name]); - } - - /** * @param $item * @param $data @@ -960,22 +941,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return isset($this->_attributes[$name]); } - /** - * @param $call - * @return mixed - * @throws Exception - */ - private function resolveClass($call): mixed - { - if ($call instanceof HasOne) { - return $call->get(); - } else if ($call instanceof HasMany) { - return $call->get(); - } else { - return $call; - } - } - /** * @param mixed $offset @@ -1025,19 +990,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } /** + * @param string ...$params * @return array */ - public function unset(): array + public function unset(string ...$params): array { - $fields = func_get_args(); - $fields = array_shift($fields); - if (!is_array($fields)) { - $fields = explode(',', $fields); - } - - $array = array_combine($fields, $fields); - - return array_diff_assoc($array, $this->_attributes); + return array_diff_assoc($params, $this->_attributes); } diff --git a/CreateConnectionPool.php b/CreateConnectionPool.php deleted file mode 100644 index 8eba942..0000000 --- a/CreateConnectionPool.php +++ /dev/null @@ -1,8 +0,0 @@ -get($name); + return Kiri::service()->get($name); } diff --git a/Model.php b/Model.php index 047c274..2d226f3 100644 --- a/Model.php +++ b/Model.php @@ -113,14 +113,18 @@ class Model extends Base\Model if (empty($attributes)) { return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } + /** @var static $select */ $select = static::query()->where($condition)->first(); - if (empty($select)) { - $select = new static(); - $select->attributes = $attributes; - if (!$select->save()) { - throw new Exception($select->getLastError()); - } + if (!empty($select)) { + return $select; + } + + $select = new static(); + $select->setAttributes($condition); + $select->setAttributes($attributes); + if (!$select->save()) { + throw new Exception($select->getLastError()); } return $select; } @@ -142,8 +146,9 @@ class Model extends Base\Model $select = static::query()->where($condition)->first(); if (empty($select)) { $select = new static(); + $select->setAttributes($condition); } - $select->attributes = $attributes; + $select->setAttributes($attributes); if (!$select->save()) { throw new Exception($select->getLastError()); } @@ -152,17 +157,14 @@ class Model extends Base\Model /** - * @param $action * @param $columns - * @param null|array $condition + * @param $action * @return array|bool|int|string|null * @throws Exception */ - private function mathematics($columns, $action, ?array $condition = null): int|bool|array|string|null + private function mathematics($columns, $action): int|bool|array|string|null { - if (empty($condition)) { - $condition = [$this->getPrimary() => $this->getPrimaryValue()]; - } + $condition = [$this->getPrimary() => $this->getPrimaryValue()]; $activeQuery = static::query()->where($condition); $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); @@ -191,13 +193,10 @@ class Model extends Base\Model */ public static function inserts(array $data): bool { - $result = false; if (empty($data)) { - error('Insert data empty.', 'mysql'); - } else { - $result = static::query()->batchInsert($data); + return error('Insert data empty.', 'mysql'); } - return $result; + return static::query()->batchInsert($data); } /** @@ -210,14 +209,14 @@ class Model extends Base\Model if (empty($primary) || !$this->hasPrimaryValue()) { return $this->logger->addError("Only primary key operations are supported.", 'mysql'); } - if ($this->beforeDelete()) { - $result = static::deleteByCondition([$primary => $this->getPrimaryValue()]); - Coroutine::create(function () use ($result) { - $this->afterDelete($result); - }); - return $result; + if (!$this->beforeDelete()) { + return false; } - return false; + $result = static::deleteByCondition([$primary => $this->getPrimaryValue()]); + + $this->afterDelete($result); + + return $result; } @@ -226,8 +225,6 @@ class Model extends Base\Model * @param array $attributes * * @return bool - * @throws NotFindClassException - * @throws ReflectionException * @throws Exception */ public static function updateAll(mixed $condition, array $attributes = []): bool @@ -240,8 +237,6 @@ class Model extends Base\Model /** * @param $condition * @return array|Collection - * @throws NotFindClassException - * @throws ReflectionException */ public static function get($condition): Collection|array { @@ -265,30 +260,6 @@ class Model extends Base\Model return $query->all(); } - /** - * @param $method - * @return mixed - * @throws Exception - */ - private function withRelation($method): mixed - { - $method = $this->getRelate($method); - if (empty($method)) { - return null; - } - $resolve = $this->{$method}(); - if ($resolve instanceof HasBase) { - $resolve = $resolve->get(); - } - if ($resolve instanceof ToArray) { - return $resolve->toArray(); - } else if (is_object($resolve)) { - return get_object_vars($resolve); - } else { - return $resolve; - } - } - /** * @return array @@ -297,9 +268,8 @@ class Model extends Base\Model public function toArray(): array { $data = $this->_attributes; - $lists = di(Getter::class)->getGetter(static::class); - foreach ($lists as $key => $item) { - $data[$key] = $this->{$item}($data[$key] ?? null); + foreach ($data as $key => $datum) { + $data[$key] = $this->withPropertyOverride($key, $datum); } return $this->withRelates($data); } @@ -315,7 +285,7 @@ class Model extends Base\Model return $relates; } foreach ($with as $val) { - $relates[$val] = $this->withRelation($val); + $relates[$val] = $this->withRelate($val); } return $relates; }