Skip to content

Commit

Permalink
重构 Model Meta 的注解管理 (imiphp#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored and NHZEX committed Jun 11, 2024
1 parent f358cfd commit d02a308
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 209 deletions.
6 changes: 6 additions & 0 deletions src/Components/model/src/Annotation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public function __construct(
/**
* save/update 模型时是否将当前时间写入该字段;支持 date/time/datetime/timestamp/year/int/bigint;当字段为 int 类型,写入秒级时间戳;当字段为 bigint 类型,写入毫秒级时间戳.
*
* @deprecated 3.1
*
* @var bool|int
*/
public $updateTime = false,
/**
* 列表分割字符串;如果字段类型为list,并且此字段不为null,读取时会处理为数组,写入时会处理为字符串.
*
* @deprecated 3.1
*/
public ?string $listSeparator = null,
/**
Expand All @@ -80,6 +84,8 @@ public function __construct(
/**
* save/create 模型时是否将当前时间写入该字段,save时表有自增ID主键才支持;支持 date/time/datetime/timestamp/year/int/bigint;当字段为 int 类型,写入秒级时间戳;当字段为 bigint 类型,写入毫秒级时间戳.
*
* @deprecated 3.1
*
* @var bool|int
*/
public $createTime = false
Expand Down
26 changes: 26 additions & 0 deletions src/Components/model/src/Annotation/CreateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 创建时间.
*
* 有此注解就表示启用.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class CreateTime extends Base
{
public function __construct(
/**
* 时间精度.
*
* 仅 bigint 有效,例:1000为毫秒
*/
public int $timeAccuracy = 1000
) {
}
}
22 changes: 22 additions & 0 deletions src/Components/model/src/Annotation/ListSeparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 字符串字段分割为列表.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ListSeparator extends Base
{
public function __construct(
/**
* 用于分割的字符串.
*/
public string $separator = ','
) {
}
}
26 changes: 26 additions & 0 deletions src/Components/model/src/Annotation/UpdateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 更新时间.
*
* 有此注解就表示启用.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class UpdateTime extends Base
{
public function __construct(
/**
* 时间精度.
*
* 仅 bigint 有效,例:1000为毫秒
*/
public int $timeAccuracy = 1000
) {
}
}
7 changes: 5 additions & 2 deletions src/Components/model/src/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Imi\Event\IEvent;
use Imi\Event\TEvent;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\ExtractProperty;
use Imi\Model\Annotation\JsonNotNull;
use Imi\Model\Annotation\Relation\AutoSelect;
use Imi\Model\Event\ModelEvents;
use Imi\Model\Event\Param\InitEventParam;
Expand Down Expand Up @@ -302,7 +304,8 @@ public function offsetSet(mixed $key, mixed $value): void
if (\is_array($value) || \is_object($value))
{
// 提取字段中的属性到当前模型
$extractProperties = $meta->getExtractPropertys();
/** @var ExtractProperty[][] $extractProperties */
$extractProperties = $meta->getPropertyAnnotations()[ExtractProperty::class] ?? [];
if (
(($name = $key) && isset($extractProperties[$name]))
|| (($name = Text::toUnderScoreCase($key)) && isset($extractProperties[$name]))
Expand Down Expand Up @@ -387,7 +390,7 @@ public function toArray(): array
if (null === $value)
{
// JsonNotNull 注解支持
if (isset(($propertyJsonNotNullMap ??= ($meta ??= $this->__meta)->getPropertyJsonNotNullMap())[$name]))
if (isset(($meta ??= $this->__meta)->getPropertyAnnotations()[JsonNotNull::class][$name]))
{
continue;
}
Expand Down
Loading

0 comments on commit d02a308

Please sign in to comment.