65 lines
1.6 KiB
PHP
Executable File
65 lines
1.6 KiB
PHP
Executable File
<?php
|
||
|
||
declare(strict_types=1);
|
||
/**
|
||
* This file is part of the youyao/admin-api.
|
||
*
|
||
* (c) youyao <info@nuancebiotech.cn>
|
||
* This source file is subject to the license under the project that is bundled.
|
||
*/
|
||
namespace App\Kernel\Visitor;
|
||
|
||
use Hyperf\Database\Commands\Ast\ModelUpdateVisitor as Visitor;
|
||
use Hyperf\Utils\Str;
|
||
|
||
class ModelUpdateVisitor extends Visitor
|
||
{
|
||
protected function formatDatabaseType(string $type): ?string
|
||
{
|
||
switch ($type) {
|
||
case 'tinyint':
|
||
case 'smallint':
|
||
case 'mediumint':
|
||
case 'int':
|
||
case 'bigint':
|
||
return 'integer';
|
||
case 'decimal':
|
||
// 设置为 decimal,并设置对应精度
|
||
return 'decimal:2';
|
||
case 'float':
|
||
case 'double':
|
||
case 'real':
|
||
return 'float';
|
||
case 'bool':
|
||
case 'boolean':
|
||
return 'boolean';
|
||
default:
|
||
return null;
|
||
}
|
||
}
|
||
|
||
protected function formatPropertyType(string $type, ?string $cast): ?string
|
||
{
|
||
if (! isset($cast)) {
|
||
$cast = $this->formatDatabaseType($type) ?? 'string';
|
||
}
|
||
|
||
switch ($cast) {
|
||
case 'integer':
|
||
return 'int';
|
||
case 'date':
|
||
case 'datetime':
|
||
return '\Carbon\Carbon';
|
||
case 'json':
|
||
return 'array';
|
||
}
|
||
|
||
if (Str::startsWith($cast, 'decimal')) {
|
||
// 如果 cast 为 decimal,则 @property 改为 string
|
||
return 'string';
|
||
}
|
||
|
||
return $cast;
|
||
}
|
||
}
|