Skip to content

Commit

Permalink
优化框架代码格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Oct 14, 2024
1 parent 34e01b8 commit 177bae3
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/Leevel/Database/Ddd/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ public function toArray(): array
foreach ($prop as $k => $config) {
$isRelationProp = static::isRelation($k);
$unCamelizeProp = static::unCamelizeProp($k);
$value = $this->propGetter($unCamelizeProp, true);
$value = $this->propGetter($unCamelizeProp);
if (null === $value) {
if (!\array_key_exists(self::SHOW_PROP_NULL, $config)) {
continue;
Expand All @@ -1858,13 +1858,12 @@ public function toArray(): array
$value = $showPropEachCallback($value, $k);
}

// 格式化后保留源数据
$shouldFormatRaw = $config[self::COLUMN_STRUCT]['format_raw'] ?? false;
if ($shouldFormatRaw) {
$result[$unCamelizeProp.'_format_raw'] = $this->getter($unCamelizeProp);
}

$result[$k] = $value;

// 格式化后用一个新字段存储
if ($this->shouldFormatPropValue($k)) {
$result[$unCamelizeProp.'_formated'] = $this->formatPropValue($k, $value);
}
}

if ($result) {
Expand Down Expand Up @@ -2075,12 +2074,14 @@ protected function parseDatabaseColumnType(string $type): string
};
}

protected function formatPropValue(string $camelizeProp, mixed $value): mixed
protected function formatPropValue(string $prop, mixed $value): mixed
{
if (!$fields = static::fields()) {
return $value;
}

$camelizeProp = static::camelizeProp($prop);

// 自定义格式化优先
if (method_exists($this, $transformValueMethod = $camelizeProp.'FormatValue')) {
return $this->{$transformValueMethod}($value);
Expand All @@ -2094,6 +2095,27 @@ protected function formatPropValue(string $camelizeProp, mixed $value): mixed
return $value;
}

protected function shouldFormatPropValue(string $prop): bool
{
if (!$fields = static::fields()) {
return false;
}

$camelizeProp = static::camelizeProp($prop);

// 自定义格式化优先
if (method_exists($this, $camelizeProp.'FormatValue')) {
return true;
}

$defaultFormat = $fields[static::unCamelizeProp($camelizeProp)][self::COLUMN_STRUCT]['format'] ?? null;
if (\is_callable($defaultFormat)) {
return true;
}

return false;
}

/**
* 转换值.
*/
Expand Down Expand Up @@ -2522,7 +2544,7 @@ protected function normalizeChangedData(array $propKey): array
/**
* 取得 getter 数据.
*/
protected function propGetter(string $prop, bool $transformGetterPropValue = false): mixed
protected function propGetter(string $prop): mixed
{
$method = 'get'.ucfirst($prop = static::camelizeProp($prop));
$value = $this->getter($prop);
Expand All @@ -2531,15 +2553,9 @@ protected function propGetter(string $prop, bool $transformGetterPropValue = fal
}

if (method_exists($this, $method)) {
// @todo 自定义 getter 是否需要纳入格式化
return $this->{$method}($prop);
}

// @todo 只有 toArray 纳入格式化,还是所有获取值都纳入格式化
if ($transformGetterPropValue) {
$value = $this->formatPropValue($prop, $value);
}

return $value;
}

Expand Down

0 comments on commit 177bae3

Please sign in to comment.