This commit is contained in:
2021-04-25 17:01:57 +08:00
parent 4b0e457318
commit c17647859a
5 changed files with 204 additions and 163 deletions
+26 -4
View File
@@ -139,6 +139,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else {
$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);
}
@@ -809,7 +814,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
parent::__set($name, $value);
return;
}
$method = annotation()->getSetMethodName(static::class, $name);
$method = $this->_get_annotation($name, self::ANNOTATION_SET);
if (!empty($method)) {
$value = $this->{$method}($value);
}
@@ -817,6 +822,22 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
}
/**
* @param $name
* @param string $method
* @return string|null
*/
protected function _get_annotation(string $name = null, string $method = self::ANNOTATION_GET): ?string
{
$matches = match ($method) {
self::ANNOTATION_GET => $this->_annotations[self::ANNOTATION_GET],
self::ANNOTATION_SET => $this->_annotations[self::ANNOTATION_SET],
default => $this->_relate
};
return $matches[$name] ?? null;
}
/**
* @param $name
* @return mixed
@@ -840,9 +861,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
private function _gets(string $name, mixed $value): mixed
{
$loader = Snowflake::app()->getAnnotation();
$method = $loader->getGetMethodName(static::class, $name);
if (empty($method)) {
if (!empty($method = $this->_get_annotation($name))) {
return $this->{$method}(...[$value]);
}
if (empty($method = $this->_get_annotation())) {
return $this->_decode($name, $value);
}
$_value = $this->{$method}(...[$value]);