69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
||
/**
|
||
* MineAdmin is committed to providing solutions for quickly building web applications
|
||
* Please view the LICENSE file that was distributed with this source code,
|
||
* For the full copyright and license information.
|
||
* Thank you very much for using MineAdmin.
|
||
*
|
||
* @Author X.Mo<root@imoi.cn>
|
||
* @Link https://gitee.com/xmo/MineAdmin
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
namespace Builder;
|
||
|
||
use Hyperf\Database\Commands\Ast\ModelUpdateVisitor as Visitor;
|
||
use Hyperf\Utils\Str;
|
||
|
||
/**
|
||
* Class ModelVisitor
|
||
* @package System
|
||
*/
|
||
class ModelVisitor extends Visitor
|
||
{
|
||
/**
|
||
* @param string $type
|
||
* @return string|null
|
||
*/
|
||
protected function formatDatabaseType(string $type): ?string
|
||
{
|
||
return match ($type) {
|
||
'tinyint', 'smallint', 'mediumint', 'int', 'bigint' => 'integer',
|
||
'decimal' => 'decimal:2',
|
||
'float', 'double', 'real' => 'float',
|
||
'bool', 'boolean' => 'boolean',
|
||
default => null,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @param string $type
|
||
* @param string|null $cast
|
||
* @return string|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 'array':
|
||
return 'json';
|
||
case 'json':
|
||
return 'array';
|
||
}
|
||
|
||
if (Str::startsWith($cast, 'decimal')) {
|
||
// 如果 cast 为 decimal,则 @property 改为 string
|
||
return 'string';
|
||
}
|
||
|
||
return $cast;
|
||
}
|
||
} |