Skip to content

Commit

Permalink
增加枚举支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Sep 5, 2024
1 parent d5c987e commit e3d5207
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/db/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace think\db;

use BackedEnum;
use Closure;
use think\db\BaseQuery as Query;
use think\db\exception\DbException as Exception;
Expand Down Expand Up @@ -532,6 +533,8 @@ protected function parseCompare(Query $query, string $key, string $exp, $value,
$value = $this->parseClosure($query, $value);
} elseif ($value instanceof Raw) {
$value = $this->parseRaw($query, $value);
} elseif ($value instanceof BackedEnum) {
$value = $value->value;
}

if ('=' == $exp && is_null($value)) {
Expand Down
8 changes: 6 additions & 2 deletions src/db/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace think\db;

use BackedEnum;
use Closure;
use Stringable;
use think\db\BaseQuery as Query;
Expand Down Expand Up @@ -59,8 +60,9 @@ protected function parseData(Query $query, array $data = [], array $fields = [],

foreach ($data as $key => $val) {
$item = $this->parseKey($query, $key, true);

if ($val instanceof Raw) {
if ($val instanceof BackedEnum) {
$val = $val->value;
} elseif ($val instanceof Raw) {
$result[$item] = $this->parseRaw($query, $val);
continue;
} elseif (is_null($val) && in_array($key, $fields, true)) {
Expand Down Expand Up @@ -265,6 +267,8 @@ protected function parseWhereItem(Query $query, $field, array $val, array $binds
} elseif ($value instanceof Stringable) {
// 对象数据写入
$value = $value->__toString();
} elseif ($value instanceof BackedEnum) {
$value = $value->value;
}

if (is_scalar($value) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && !str_contains($exp, 'TIME')) {
Expand Down
9 changes: 7 additions & 2 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace think\model\concern;

use BackedEnum;
use Closure;
use InvalidArgumentException;
use Stringable;
Expand Down Expand Up @@ -468,7 +469,9 @@ protected function writeTransform($value, string | array $type)

$typeTransform = static function (string $type, $value, $model) {
if (str_contains($type, '\\') && class_exists($type)) {
if (is_subclass_of($type, FieldTypeTransform::class)) {
if ($value instanceof BackedEnum) {
$value = $value->value;
} elseif (is_subclass_of($type, FieldTypeTransform::class)) {
$value = $type::set($value, $model);
} elseif ($value instanceof Stringable) {
$value = $value->__toString();
Expand Down Expand Up @@ -641,7 +644,9 @@ protected function readTransform($value, string | array $type)

$typeTransform = static function (string $type, $value, $model) {
if (str_contains($type, '\\') && class_exists($type)) {
if (is_subclass_of($type, FieldTypeTransform::class)) {
if (is_subclass_of($type, BackedEnum::class)) {
$value = $type::from($value);
} elseif (is_subclass_of($type, FieldTypeTransform::class)) {
$value = $type::get($value, $model);
} else {
// 对象类型
Expand Down

0 comments on commit e3d5207

Please sign in to comment.