Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构 Model Meta 的注解管理 #701

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(

Check warning on line 15 in src/Components/model/src/Annotation/ListSeparator.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Annotation/ListSeparator.php#L15

Added line #L15 was not covered by tests
/**
* 用于分割的字符串.
*/
public string $separator = ','
) {
}

Check warning on line 21 in src/Components/model/src/Annotation/ListSeparator.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Annotation/ListSeparator.php#L21

Added line #L21 was not covered by tests
}
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
Loading