This commit is contained in:
2021-08-17 16:22:18 +08:00
parent d639aa0fab
commit 01cfdb8320
10 changed files with 315 additions and 69 deletions
+2 -2
View File
@@ -11,6 +11,7 @@ namespace Database;
use Database\Base\BaseActiveRecord; use Database\Base\BaseActiveRecord;
use Database\Base\Getter;
use Database\Traits\HasBase; use Database\Traits\HasBase;
use Exception; use Exception;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
@@ -311,8 +312,7 @@ class ActiveRecord extends BaseActiveRecord
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
$lists = di(Getter::class)->getGetter(static::class);
$lists = Kiri::getAnnotation()->getGets(static::class);
foreach ($lists as $key => $item) { foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null); $data[$key] = $this->{$item}($data[$key] ?? null);
} }
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace Database\Annotation;
use Attribute;
use Database\Base\Getter;
use Exception;
/**
* Class Get
* @package Annotation\Model
*/
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
{
/**
* Get constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Getter::class)->addGetter($this->name, $class, $method);
return true;
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
namespace Database\Annotation;
use Annotation\Attribute;
use Database\ActiveRecord;
use Database\Base\Relate;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Kiri;
/**
* Class Relation
* @package Annotation\Model
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
{
/**
* Relation constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Relate::class)->addRelate($class, $this->name, $method);
return true;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Database\Annotation;
use Annotation\Attribute;
use Database\Base\Setter;
use Exception;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
{
/**
* Set constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Setter::class)->addSetter($this->name, $class, $method);
return true;
}
}
+76 -65
View File
@@ -13,6 +13,7 @@ namespace Database\Base;
use Annotation\Event; use Annotation\Event;
use Annotation\Inject; use Annotation\Inject;
use ArrayAccess; use ArrayAccess;
use Closure;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use Database\Connection; use Database\Connection;
@@ -26,7 +27,6 @@ use Database\SqlBuilder;
use Database\Traits\HasBase; use Database\Traits\HasBase;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\TraitApplication; use Kiri\Abstracts\TraitApplication;
@@ -35,6 +35,7 @@ use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException;
use validator\Validator; 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 * @return EventDispatch
* @throws NotFindClassException * @throws NotFindClassException
@@ -375,7 +432,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess,
* @return bool * @return bool
* @throws Exception * @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 (empty($condition)) {
if (!$if_condition_is_null) { if (!$if_condition_is_null) {
@@ -706,7 +763,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess,
*/ */
public function getRelate($name): null|array|string 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))) { if (method_exists($this, 'set' . ucfirst($name))) {
$this->{'set' . ucfirst($name)}($value); $this->{'set' . ucfirst($name)}($value);
} else { } else {
$method = $this->_get_annotation($name, self::SET); $this->_setter($name, $value);
if (!empty($method)) {
$value = $this->{$method}($value);
}
$this->_attributes[$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 * @param $name
* @return mixed * @return mixed
@@ -834,45 +868,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess,
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->{$method}(); return $this->{$method}();
} }
if (isset($this->_attributes[$name])) { $value = $this->_attributes[$name] ?? null;
return $this->runGetter($name, $this->_attributes[$name]);
} else {
return $this->runRelation($name);
}
}
return $this->_getter($name, $value);
/**
* @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;
} }
@@ -1072,4 +1070,17 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess,
return $model; return $model;
} }
/**
* @param $method
* @param $value
* @return Closure
*/
protected function dispatcher($method, $value): Closure
{
return function () use ($method, $value) {
return $this->{$method}($value);
};
}
} }
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace Database\Base;
class Getter
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addGetter($name, $class, $method)
{
$this->_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] ?? [];
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace Database\Base;
class Relate
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addRelate($name, $class, $method)
{
$this->_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] ?? [];
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Database\Base;
class Setter
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addSetter($name, $class, $method)
{
$this->_classMapping[$class][$name] = $method;
}
/**
* @param $class
* @param $name
* @return mixed|null
*/
public function getSetter($class, $name): ?string
{
return $this->_classMapping[$class][$name] ?? null;
}
}
+1 -2
View File
@@ -21,7 +21,7 @@ use Exception;
* *
* @include Query * @include Query
*/ */
abstract class HasBase abstract class HasBase implements \Database\Traits\Relation
{ {
/** @var ActiveRecord|Collection */ /** @var ActiveRecord|Collection */
@@ -67,7 +67,6 @@ abstract class HasBase
$this->value = $value; $this->value = $value;
} }
abstract public function get();
/** /**
* @param $name * @param $name
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Database\Traits;
interface Relation
{
public function get(): mixed;
}