You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
3.8 KiB
157 lines
3.8 KiB
<?php |
|
/** |
|
* |
|
* Validate.php |
|
* User: ChenLong |
|
* DateTime: 2020/3/17 17:28 |
|
*/ |
|
|
|
|
|
namespace sdModule\makeAdminBasics\makeItem; |
|
|
|
|
|
use sdModule\makeAdminBasics\Basics; |
|
use sdModule\makeAdminBasics\ConfigHelper; |
|
use sdModule\makeAdminBasics\Make; |
|
|
|
class Validate implements MakeItemInterface |
|
{ |
|
|
|
/** |
|
* @var Make |
|
*/ |
|
private $make; |
|
|
|
/** |
|
* @var |
|
*/ |
|
private $rule = []; |
|
|
|
/** |
|
* @var |
|
*/ |
|
private $scene = []; |
|
|
|
|
|
/** |
|
* 构造函数 |
|
* MakeItem constructor. |
|
* @param Make $make |
|
*/ |
|
public function __construct(Make $make) |
|
{ |
|
$this->make = $make; |
|
$this->fieldRuleHandle(); |
|
} |
|
|
|
/** |
|
* 创建文件 |
|
* @return mixed |
|
*/ |
|
public function make(): bool |
|
{ |
|
$table = parse_name($this->make->table, 1); |
|
$time = date('Y-m-d H:i'); |
|
$namespace = ConfigHelper::get('namespace.validate'); |
|
|
|
$code = <<<CODE |
|
<?php |
|
|
|
/** |
|
* |
|
* {$table}.php |
|
* User: ChenLong |
|
* DateTime: {$time} |
|
*/ |
|
|
|
namespace {$namespace}; |
|
|
|
use think\Validate; |
|
|
|
/** |
|
* Class {$table} |
|
* @package {$namespace}\\{$table} |
|
* @author chenlong <vip_chenlong@163.com> |
|
*/ |
|
class {$table} extends Validate |
|
{ |
|
protected \$rule = {$this->getRule()} |
|
|
|
protected \$scene = {$this->getScene()} |
|
} |
|
|
|
CODE; |
|
|
|
return $this->make->makeFile(ConfigHelper::get('file_url.validate') . $table . '.php', $code); |
|
} |
|
|
|
|
|
/** |
|
* 字段规则处理 |
|
*/ |
|
private function fieldRuleHandle() |
|
{ |
|
$tableInfo = (new Basics())->getTableInfo($this->make->table); |
|
$tableComment = array_column($tableInfo, 'column_comment', 'column_name'); |
|
$fieldType = array_column($tableInfo, 'column_type', 'column_name'); |
|
|
|
foreach ($this->make->makeData as $field => $makeDatum) { |
|
if (!$makeDatum['type']) continue; |
|
$title = $makeDatum['label'] ?: $tableComment[$field]; |
|
$verifyRule = 'require'; |
|
|
|
if (in_array($fieldType[$field], ['tinyint', 'int', 'smallint', 'bigint'])) $verifyRule .= '|number'; |
|
|
|
if (in_array($fieldType[$field], ['float', 'decimal', 'double'])) $verifyRule .= '|float'; |
|
|
|
if (preg_match('/([a-zA-Z_]*phone)$|([a-zA-Z_]*tel)$/', $field)) $verifyRule .= '|mobile'; |
|
|
|
if ($makeDatum['join'] && is_array($makeDatum['join'])) { |
|
$verifyRule .= '|in:' . implode(',', array_keys($makeDatum['join'])); |
|
} |
|
$this->scene[] = $field; |
|
$this->rule["{$field}|{$title}"] = $verifyRule; |
|
} |
|
} |
|
|
|
/** |
|
* 获取规则 |
|
* @return string |
|
*/ |
|
private function getRule() |
|
{ |
|
$ruleText = '['; |
|
foreach ($this->rule as $field => $rule) { |
|
$ruleText .= Basics::indentAndLineFeed(2, Basics::BEFORE) . |
|
"'{$field}' => '{$rule}',"; |
|
} |
|
$ruleText .= Basics::indentAndLineFeed(2, Basics::BEFORE) . |
|
"'{$this->make->primaryKey}|{$this->make->tableComment}' => 'require|number',"; |
|
return $ruleText . Basics::indentAndLineFeed(1, Basics::BEFORE) . '];'; |
|
} |
|
|
|
/** |
|
* 获取场景 |
|
* @return string |
|
*/ |
|
private function getScene() |
|
{ |
|
return "[" . Basics::indentAndLineFeed(2, Basics::BEFORE) . |
|
"'add' => ['" . implode("', '", $this->scene) . "',]," . Basics::indentAndLineFeed(2, Basics::BEFORE) . |
|
"'edit' => ['" . implode("', '", $this->scene) . "', '{$this->make->primaryKey}',]," . |
|
Basics::indentAndLineFeed(1, Basics::BEFORE) . "];"; |
|
} |
|
|
|
/** |
|
* @return bool|int |
|
*/ |
|
private function getMaxLength() |
|
{ |
|
$maxLength = 0; |
|
foreach ($this->rule as $field => $rule) { |
|
mb_strlen($field, 'utf8') > $maxLength and $maxLength = mb_strlen($field, 'utf8'); |
|
} |
|
return $maxLength; |
|
} |
|
} |
|
|
|
|