改名
This commit is contained in:
+13
-16
@@ -77,36 +77,33 @@ class Annotation extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @param string $setName
|
* @param string|null $setName
|
||||||
* @return mixed|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
public function getGetMethodName(string $class, string $setName): ?string
|
public function getGetMethodName(string $class, string $setName = null): array|null|string
|
||||||
{
|
{
|
||||||
$gets = $this->_model_gets[$class] ?? $this->_model_relate[$class] ?? null;
|
$gets = $this->_model_gets[$class] ?? null;
|
||||||
if ($gets == null) {
|
if ($gets == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (empty($setName)) return $gets;
|
||||||
return $gets[$setName] ?? null;
|
return $gets[$setName] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @return array
|
* @param string|null $method
|
||||||
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
public function getModelMethods(string $class): array
|
public function getRelateMethods(string $class, string $method = null): array|null|string
|
||||||
{
|
{
|
||||||
return $this->_model_gets[$class] ?? [];
|
$gets = $this->_model_relate[$class] ?? null;
|
||||||
|
if ($gets == null) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
if (empty($method)) return $gets;
|
||||||
|
return $gets[$method] ?? null;
|
||||||
/**
|
|
||||||
* @param string $class
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getRelateMethods(string $class): array
|
|
||||||
{
|
|
||||||
return $this->_model_relate[$class] ?? [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -307,14 +307,14 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
{
|
{
|
||||||
$data = $this->_attributes;
|
$data = $this->_attributes;
|
||||||
|
|
||||||
$lists = $this->getAnnotation(self::ANNOTATION_GET);
|
$lists = $this->getAnnotation(self::GET);
|
||||||
foreach ($lists as $key => $item) {
|
foreach ($lists as $key => $item) {
|
||||||
$data[$key] = $this->{$item}($data[$key] ?? null);
|
$data[$key] = $this->{$item}($data[$key] ?? null);
|
||||||
}
|
}
|
||||||
$data = array_merge($data, $this->runRelate());
|
$data = array_merge($data, $this->runRelate());
|
||||||
|
|
||||||
$class = Snowflake::app()->getChannel();
|
// $class = Snowflake::app()->getChannel();
|
||||||
$class->push($this, static::class);
|
// $class->push($this, static::class);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,8 +52,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
const BEFORE_SAVE = 'before::save';
|
const BEFORE_SAVE = 'before::save';
|
||||||
|
|
||||||
|
|
||||||
const ANNOTATION_GET = 'get';
|
const GET = 'get';
|
||||||
const ANNOTATION_SET = 'set';
|
const SET = 'set';
|
||||||
|
const RELATE = 'RELATE';
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected array $_attributes = [];
|
protected array $_attributes = [];
|
||||||
@@ -139,11 +140,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
} else {
|
} else {
|
||||||
$this->_relation = Context::getContext(Relation::class);
|
$this->_relation = Context::getContext(Relation::class);
|
||||||
}
|
}
|
||||||
$annotation = Snowflake::app()->getAnnotation();
|
|
||||||
$this->_annotations[self::ANNOTATION_GET] = $annotation->getGets(static::class);
|
|
||||||
$this->_annotations[self::ANNOTATION_SET] = $annotation->getSets(static::class);
|
|
||||||
$this->_relate = $annotation->getRelateMethods(static::class);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -154,10 +150,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function addGets($name, $method): static
|
public function addGets($name, $method): static
|
||||||
{
|
{
|
||||||
if (!isset($this->_annotations[self::ANNOTATION_GET])) {
|
if (!isset($this->_annotations[self::GET])) {
|
||||||
$this->_annotations[self::ANNOTATION_GET] = [];
|
$this->_annotations[self::GET] = [];
|
||||||
}
|
}
|
||||||
$this->_annotations[self::ANNOTATION_GET][$name] = [$this, $method];
|
$this->_annotations[self::GET][$name] = [$this, $method];
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,10 +165,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function addSets($name, $method): static
|
public function addSets($name, $method): static
|
||||||
{
|
{
|
||||||
if (!isset($this->_annotations[self::ANNOTATION_SET])) {
|
if (!isset($this->_annotations[self::SET])) {
|
||||||
$this->_annotations[self::ANNOTATION_SET] = [];
|
$this->_annotations[self::SET] = [];
|
||||||
}
|
}
|
||||||
$this->_annotations[self::ANNOTATION_SET][$name] = [$this, $method];
|
$this->_annotations[self::SET][$name] = [$this, $method];
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -611,7 +607,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
* @param string $type
|
* @param string $type
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function runAnnotation(string $name, mixed $value, string $type = self::ANNOTATION_GET): mixed
|
protected function runAnnotation(string $name, mixed $value, string $type = self::GET): mixed
|
||||||
{
|
{
|
||||||
return call_user_func($this->_annotations[$type][$name], $value);
|
return call_user_func($this->_annotations[$type][$name], $value);
|
||||||
}
|
}
|
||||||
@@ -713,15 +709,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return array|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getRelate($name): mixed
|
public function getRelate($name): null|array|string
|
||||||
{
|
{
|
||||||
if (empty($this->_relate[$name])) {
|
return Snowflake::getAnnotation()->getRelateMethods(static::class, $name);
|
||||||
$this->_relate = Snowflake::getAnnotation()->getRelateMethods(static::class);
|
|
||||||
}
|
|
||||||
return $this->_relate[$name] ?? null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -814,7 +807,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
parent::__set($name, $value);
|
parent::__set($name, $value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$method = $this->_get_annotation($name, self::ANNOTATION_SET);
|
$method = $this->_get_annotation($name, self::SET);
|
||||||
if (!empty($method)) {
|
if (!empty($method)) {
|
||||||
$value = $this->{$method}($value);
|
$value = $this->{$method}($value);
|
||||||
}
|
}
|
||||||
@@ -828,21 +821,16 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
* @return string|null
|
* @return string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function _get_annotation(string $name = null, string $method = self::ANNOTATION_GET): ?string
|
protected function _get_annotation(string $name = null, string $method = self::GET): ?string
|
||||||
{
|
{
|
||||||
$annotation = Snowflake::app()->getAnnotation();
|
$annotation = Snowflake::app()->getAnnotation();
|
||||||
if (!isset($this->_annotations[self::ANNOTATION_GET])) {
|
if ($method == static::SET) {
|
||||||
$this->_annotations[self::ANNOTATION_GET] = $annotation->getGets(static::class);
|
return $annotation->getSetMethodName(static::class, $name);
|
||||||
}
|
}
|
||||||
if (!isset($this->_annotations[self::ANNOTATION_SET])) {
|
if ($method == static::GET) {
|
||||||
$this->_annotations[self::ANNOTATION_SET] = $annotation->getSets(static::class);
|
return $annotation->getGetMethodName(static::class, $name);
|
||||||
}
|
}
|
||||||
$matches = match ($method) {
|
return $annotation->getRelateMethods(static::class, $name);
|
||||||
self::ANNOTATION_GET => $this->_annotations[self::ANNOTATION_GET],
|
|
||||||
self::ANNOTATION_SET => $this->_annotations[self::ANNOTATION_SET],
|
|
||||||
default => $this->_relate
|
|
||||||
};
|
|
||||||
return $matches[$name] ?? null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -853,33 +841,21 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
if (isService($name)) {
|
|
||||||
return Snowflake::getApp($name);
|
|
||||||
}
|
|
||||||
$value = $this->_attributes[$name] ?? null;
|
$value = $this->_attributes[$name] ?? null;
|
||||||
return $this->_gets($name, $value);
|
$method = $this->_get_annotation($name, static::GET);
|
||||||
}
|
if (!empty($method)) {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param mixed $value
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function _gets(string $name, mixed $value): mixed
|
|
||||||
{
|
|
||||||
if (!empty($method = $this->_get_annotation($name))) {
|
|
||||||
return $this->{$method}(...[$value]);
|
return $this->{$method}(...[$value]);
|
||||||
}
|
}
|
||||||
if (empty($method = $this->_get_annotation())) {
|
|
||||||
|
$relation = $this->_get_annotation($name, static::RELATE);
|
||||||
|
if (empty($relation)) {
|
||||||
return $this->_decode($name, $value);
|
return $this->_decode($name, $value);
|
||||||
}
|
}
|
||||||
$_value = $this->{$method}(...[$value]);
|
|
||||||
if ($_value instanceof HasBase) {
|
if (($value = $this->{$relation}(...[$value])) instanceof HasBase) {
|
||||||
return $_value->get();
|
return $value->get();
|
||||||
}
|
}
|
||||||
return $_value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -913,7 +889,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
* @param string $type
|
* @param string $type
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getAnnotation($type = self::ANNOTATION_GET): array
|
protected function getAnnotation($type = self::GET): array
|
||||||
{
|
{
|
||||||
return $this->_annotations[$type] ?? [];
|
return $this->_annotations[$type] ?? [];
|
||||||
}
|
}
|
||||||
@@ -924,7 +900,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
* @param string $type
|
* @param string $type
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function hasAnnotation($name, $type = self::ANNOTATION_GET): bool
|
protected function hasAnnotation($name, $type = self::GET): bool
|
||||||
{
|
{
|
||||||
if (!isset($this->_annotations[$type])) {
|
if (!isset($this->_annotations[$type])) {
|
||||||
return false;
|
return false;
|
||||||
@@ -1060,11 +1036,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function populate(array $data): static
|
public static function populate(array $data): static
|
||||||
{
|
{
|
||||||
$class = Snowflake::app()->getChannel();
|
// $class = Snowflake::app()->getChannel();
|
||||||
/** @var static $model */
|
// /** @var static $model */
|
||||||
$model = $class->pop(static::class, function () {
|
// $model = $class->pop(static::class, function () {
|
||||||
return new static();
|
// return new static();
|
||||||
});
|
// });
|
||||||
|
$model = new static();
|
||||||
$model->_attributes = $data;
|
$model->_attributes = $data;
|
||||||
$model->_oldAttributes = $data;
|
$model->_oldAttributes = $data;
|
||||||
$model->setIsCreate(false);
|
$model->setIsCreate(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user