diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 05138a6..7e0abd4 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -11,6 +11,7 @@ namespace Database; use Database\Base\BaseActiveRecord; +use Database\Base\Getter; use Database\Traits\HasBase; use Exception; use Kiri\Exception\NotFindClassException; @@ -311,8 +312,7 @@ class ActiveRecord extends BaseActiveRecord public function toArray(): array { $data = $this->_attributes; - - $lists = Kiri::getAnnotation()->getGets(static::class); + $lists = di(Getter::class)->getGetter(static::class); foreach ($lists as $key => $item) { $data[$key] = $this->{$item}($data[$key] ?? null); } diff --git a/src/Annotation/Get.php b/src/Annotation/Get.php new file mode 100644 index 0000000..c670948 --- /dev/null +++ b/src/Annotation/Get.php @@ -0,0 +1,42 @@ +addGetter($this->name, $class, $method); + return true; + } + + +} diff --git a/src/Annotation/Relation.php b/src/Annotation/Relation.php new file mode 100644 index 0000000..1b7e9c1 --- /dev/null +++ b/src/Annotation/Relation.php @@ -0,0 +1,44 @@ +addRelate($class, $this->name, $method); + return true; + } + +} diff --git a/src/Annotation/Set.php b/src/Annotation/Set.php new file mode 100644 index 0000000..7c7a2ce --- /dev/null +++ b/src/Annotation/Set.php @@ -0,0 +1,37 @@ +addSetter($this->name, $class, $method); + return true; + } + + +} diff --git a/src/Base/BaseActiveRecord.php b/src/Base/BaseActiveRecord.php index b1b26d3..18266d8 100644 --- a/src/Base/BaseActiveRecord.php +++ b/src/Base/BaseActiveRecord.php @@ -13,6 +13,7 @@ namespace Database\Base; use Annotation\Event; use Annotation\Inject; use ArrayAccess; +use Closure; use Database\ActiveQuery; use Database\ActiveRecord; use Database\Connection; @@ -26,7 +27,6 @@ use Database\SqlBuilder; use Database\Traits\HasBase; use Exception; use JetBrains\PhpStorm\Pure; -use ReflectionException; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; use Kiri\Abstracts\TraitApplication; @@ -35,6 +35,7 @@ use Kiri\Events\EventDispatch; use Kiri\Exception\ConfigException; use Kiri\Exception\NotFindClassException; use Kiri\Kiri; +use ReflectionException; use validator\Validator; /** @@ -119,6 +120,62 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, } + /** + * @param string $name + * @param mixed $value + * @return mixed + * @throws NotFindClassException + * @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 $this->_attributes[$name] = $value; + } + + + /** + * @param string $name + * @param $value + * @return mixed + * @throws NotFindClassException + * @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 NotFindClassException + * @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); + } + + /** * @return EventDispatch * @throws NotFindClassException @@ -375,7 +432,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, * @return bool * @throws Exception */ - public static function deleteByCondition($condition = NULL, array $attributes = [], bool $if_condition_is_null = false): bool + protected static function deleteByCondition($condition = NULL, array $attributes = [], bool $if_condition_is_null = false): bool { if (empty($condition)) { if (!$if_condition_is_null) { @@ -706,7 +763,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, */ public function getRelate($name): null|array|string { - return Kiri::getAnnotation()->getRelateMethods(static::class, $name); + return di(Relate::class)->getRelate(static::class, $name); } @@ -795,34 +852,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, if (method_exists($this, 'set' . ucfirst($name))) { $this->{'set' . ucfirst($name)}($value); } else { - $method = $this->_get_annotation($name, self::SET); - if (!empty($method)) { - $value = $this->{$method}($value); - } - $this->_attributes[$name] = $value; + $this->_setter($name, $value); } } - /** - * @param string|null $name - * @param string $method - * @return string|null - * @throws Exception - */ - protected function _get_annotation(string $name = null, string $method = self::GET): ?string - { - $annotation = Kiri::app()->getAnnotation(); - if ($method == static::SET) { - return $annotation->getSetMethodName(static::class, $name); - } - if ($method == static::GET) { - return $annotation->getGetMethodName(static::class, $name); - } - return $annotation->getRelateMethods(static::class, $name); - } - - /** * @param $name * @return mixed @@ -834,45 +868,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, if (method_exists($this, $method)) { return $this->{$method}(); } - if (isset($this->_attributes[$name])) { - return $this->runGetter($name, $this->_attributes[$name]); - } else { - return $this->runRelation($name); - } - } + $value = $this->_attributes[$name] ?? null; - - /** - * @param $name - * @param $value - * @return void|null - * @throws Exception - */ - private function runGetter($name, $value) - { - $relation = $this->_get_annotation($name, static::GET); - if (empty($relation)) { - return $this->_decode($name, $value); - } - return $this->{$relation}($value); - } - - - /** - * @param $name - * @return mixed - * @throws Exception - */ - private function runRelation($name): mixed - { - $relation = $this->_get_annotation($name, static::RELATE); - if (empty($relation)) { - return null; - } - if (($value = $this->{$relation}()) instanceof HasBase) { - return $value->get(); - } - return $value; + return $this->_getter($name, $value); } @@ -1072,4 +1070,17 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess, return $model; } + + /** + * @param $method + * @param $value + * @return Closure + */ + protected function dispatcher($method, $value): Closure + { + return function () use ($method, $value) { + return $this->{$method}($value); + }; + } + } diff --git a/src/Base/Getter.php b/src/Base/Getter.php new file mode 100644 index 0000000..163efe7 --- /dev/null +++ b/src/Base/Getter.php @@ -0,0 +1,35 @@ +_classMapping[$class][$name] = $method; + } + + + /** + * @param $class + * @param $name + * @return mixed|null + */ + public function getGetter($class, $name = null): ?string + { + if (!empty($name)) { + return $this->_classMapping[$class][$name] ?? null; + } + return $this->_classMapping[$class] ?? []; + } + +} diff --git a/src/Base/Relate.php b/src/Base/Relate.php new file mode 100644 index 0000000..ae45b32 --- /dev/null +++ b/src/Base/Relate.php @@ -0,0 +1,36 @@ +_classMapping[$class][$name] = $method; + } + + + /** + * @param $class + * @param $name + * @return mixed|null + */ + public function getRelate($class, $name = null): ?string + { + if (!empty($name)) { + return $this->_classMapping[$class][$name] ?? null; + } + return $this->_classMapping[$class] ?? []; + } + + +} diff --git a/src/Base/Setter.php b/src/Base/Setter.php new file mode 100644 index 0000000..7df0ed2 --- /dev/null +++ b/src/Base/Setter.php @@ -0,0 +1,32 @@ +_classMapping[$class][$name] = $method; + } + + + /** + * @param $class + * @param $name + * @return mixed|null + */ + public function getSetter($class, $name): ?string + { + return $this->_classMapping[$class][$name] ?? null; + } + +} diff --git a/src/Traits/HasBase.php b/src/Traits/HasBase.php index cad3c09..51d6413 100644 --- a/src/Traits/HasBase.php +++ b/src/Traits/HasBase.php @@ -21,7 +21,7 @@ use Exception; * * @include Query */ -abstract class HasBase +abstract class HasBase implements \Database\Traits\Relation { /** @var ActiveRecord|Collection */ @@ -67,7 +67,6 @@ abstract class HasBase $this->value = $value; } - abstract public function get(); /** * @param $name diff --git a/src/Traits/Relation.php b/src/Traits/Relation.php new file mode 100644 index 0000000..95915f5 --- /dev/null +++ b/src/Traits/Relation.php @@ -0,0 +1,10 @@ +