This commit is contained in:
2021-03-04 15:17:25 +08:00
parent 5a237cdd77
commit 09c22bd878
2 changed files with 30 additions and 16 deletions
+2 -2
View File
@@ -268,11 +268,11 @@ class ActiveRecord extends BaseActiveRecord
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
foreach ($this->getAnnotation('get') as $key => $item) { foreach ($this->getAnnotation(self::ANNOTATION_GET) as $key => $item) {
if (!isset($data[$key])) { if (!isset($data[$key])) {
continue; continue;
} }
$data[$key] = call_user_func($item, $data[$key]); $data[$key] = $this->runAnnotation($key, $data[$key]);
} }
$data = array_merge($data, $this->runRelate()); $data = array_merge($data, $this->runRelate());
$this->recover(); $this->recover();
+28 -14
View File
@@ -50,6 +50,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
const BEFORE_SAVE = 'before::save'; const BEFORE_SAVE = 'before::save';
const ANNOTATION_GET = 'get';
const ANNOTATION_SET = 'set';
#[Inject(SEvent::class)] #[Inject(SEvent::class)]
protected ?SEvent $event; protected ?SEvent $event;
@@ -161,10 +165,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['get'])) { if (!isset($this->_annotations[self::ANNOTATION_GET])) {
$this->_annotations['get'] = []; $this->_annotations[self::ANNOTATION_GET] = [];
} }
$this->_annotations['get'][$name] = [$this, $method]; $this->_annotations[self::ANNOTATION_GET][$name] = [$this, $method];
return $this; return $this;
} }
@@ -176,10 +180,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['set'])) { if (!isset($this->_annotations[self::ANNOTATION_SET])) {
$this->_annotations['set'] = []; $this->_annotations[self::ANNOTATION_SET] = [];
} }
$this->_annotations['set'][$name] = [$this, $method]; $this->_annotations[self::ANNOTATION_SET][$name] = [$this, $method];
return $this; return $this;
} }
@@ -613,15 +617,25 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public function getAttribute(string $name) public function getAttribute(string $name)
{ {
$method = 'get' . ucfirst($name) . 'Attribute'; if ($this->hasAnnotation($name)) {
return $this->runAnnotation($name, $this->_attributes[$name]);
if (method_exists($this, $method)) {
return $this->$method($this->_attributes[$name]);
} }
return $this->_attributes[$name] ?? null; return $this->_attributes[$name] ?? null;
} }
/**
* @param string $name
* @param mixed $value
* @param string $type
* @return mixed
*/
protected function runAnnotation(string $name, mixed $value, string $type = self::ANNOTATION_GET): mixed
{
return call_user_func($this->_annotations[$type][$name], $value);
}
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
@@ -819,7 +833,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return; return;
} }
if ($this->hasAnnotation($name, 'set')) { if ($this->hasAnnotation($name, 'set')) {
call_user_func($this->_annotations['set'][$name], $value); $this->runAnnotation($name, $value, self::ANNOTATION_SET);
} else { } else {
$this->_attributes[$name] = $value; $this->_attributes[$name] = $value;
} }
@@ -835,7 +849,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{ {
$value = $this->_attributes[$name] ?? null; $value = $this->_attributes[$name] ?? null;
if ($this->hasAnnotation($name)) { if ($this->hasAnnotation($name)) {
return call_user_func($this->_annotations['get'][$name], $value); return $this->runAnnotation($name, $value);
} }
if (array_key_exists($name, $this->_attributes)) { if (array_key_exists($name, $this->_attributes)) {
return static::getColumns()->_decode($name, $value); return static::getColumns()->_decode($name, $value);
@@ -851,7 +865,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @param string $type * @param string $type
* @return array * @return array
*/ */
protected function getAnnotation($type = 'get'): array protected function getAnnotation($type = self::ANNOTATION_GET): array
{ {
return $this->_annotations[$type] ?? []; return $this->_annotations[$type] ?? [];
} }
@@ -862,7 +876,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @param string $type * @param string $type
* @return bool * @return bool
*/ */
protected function hasAnnotation($name, $type = 'get'): bool protected function hasAnnotation($name, $type = self::ANNOTATION_GET): bool
{ {
if (!isset($this->_annotations[$type])) { if (!isset($this->_annotations[$type])) {
return false; return false;