Skip to content

Commit

Permalink
增加enumStrict属性 设置是否严格使用枚举类型写入数据
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 10, 2024
1 parent 399200e commit dda03da
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ trait Attribute
*/
protected $enumReadName = false;

/**
* 严格检查Enum数据类型.
*
* @var bool
*/
protected $enumStrict = false;

/**
* 是否严格字段大小写.
*
Expand Down Expand Up @@ -436,6 +443,9 @@ public function setAttr(string $name, $value, array $data = []): void
}
} elseif (!in_array($name, $this->json) && isset($this->type[$name])) {
// 类型转换
if ($this->enumStrict && is_subclass_of($this->type[$name], BackedEnum::class) && !($value instanceof BackedEnum)) {
throw new InvalidArgumentException('data type error: ' . $name . ' => ' . $this->type[$name]);
}
$value = $this->writeTransform($value, $this->type[$name]);
} elseif ($this->isRelationAttr($name)) {
// 关联属性
Expand Down

0 comments on commit dda03da

Please sign in to comment.