This commit is contained in:
2021-01-20 15:45:53 +08:00
parent e58ab63a24
commit 239a981b34
4 changed files with 69 additions and 44 deletions
+2 -28
View File
@@ -311,40 +311,14 @@ class ActiveRecord extends BaseActiveRecord
*/
public function toArray(): array
{
$attributes = Snowflake::app()->getAttributes();
$callback = $attributes->getByClass(static::class);
$data = $this->_attributes;
foreach ($callback as $key => $item) {
$data = $this->resolveAttributes($item, $data);
foreach ($this->getAnnotation() as $key => $item) {
$data[$key] = call_user_func($item, $data[$key]);
}
return array_merge($data, $this->runRelate());
}
/**
* @param $item
* @param $data
* @return array
*/
private function resolveAttributes($item, $data): array
{
if (!isset($item['attributes'])) {
return $data;
}
foreach ($item['attributes'] as $attribute) {
if (!($attribute instanceof Get)) {
continue;
}
$name = $attribute->name;
$result = call_user_func($item['handler'], $data[$name]);
$data[$name] = $result;
}
return $data;
}
/**
* @return array
* @throws Exception