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.
220 lines
4.3 KiB
220 lines
4.3 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 Model implements MakeItemInterface |
|
{ |
|
|
|
/** |
|
* @var Make |
|
*/ |
|
private $make; |
|
|
|
/** |
|
* @var |
|
*/ |
|
private $attr = ''; |
|
/** |
|
* @var |
|
*/ |
|
private $commonAttr = ''; |
|
|
|
private $useArr = [ |
|
'use think\\Model;' |
|
]; |
|
|
|
private $getAttrData = []; |
|
|
|
public $tag = ['red', 'orange', 'green', 'cyan', 'blue', 'black', 'gray', 'rim']; |
|
|
|
/** |
|
* 构造函数 |
|
* MakeItem constructor. |
|
* @param Make $make |
|
*/ |
|
public function __construct(Make $make) |
|
{ |
|
$this->make = $make; |
|
$this->getAttr(); |
|
$this->attrCode(); |
|
shuffle($this->tag); |
|
} |
|
|
|
/** |
|
* 获取返回代码文本 |
|
* @return mixed |
|
*/ |
|
public function make(): bool |
|
{ |
|
return $this->commonModel() && $this->model(); |
|
} |
|
|
|
/** |
|
* 创建 公共Model |
|
* @return bool|int |
|
*/ |
|
private function commonModel() |
|
{ |
|
$table = parse_name($this->make->table, 1); |
|
$time = date('Y-m-d H:i'); |
|
$namespace = ConfigHelper::get('namespace.common_model'); |
|
|
|
$use = implode(Basics::indentAndLineFeed(0, Basics::BEFORE),$this->useArr); |
|
|
|
$code = <<<CODE |
|
<?php |
|
/** |
|
* |
|
* {$table}.php |
|
* User: ChenLong |
|
* DateTime: {$time} |
|
*/ |
|
|
|
|
|
namespace {$namespace}; |
|
|
|
{$use} |
|
|
|
/** |
|
* Class {$table} |
|
* @package {$namespace}\\{$table} |
|
* @author chenlong <vip_chenlong@163.com> |
|
*/ |
|
class {$table} extends Model |
|
{ |
|
|
|
protected \$schema = {$this->fieldSet()}; |
|
|
|
{$this->commonAttr} |
|
} |
|
|
|
CODE; |
|
|
|
return $this->make->makeFile(ConfigHelper::get('file_url.common_model') . "{$table}.php", $code); |
|
} |
|
|
|
/** |
|
* 创建 model |
|
* @return bool|int |
|
*/ |
|
private function model() |
|
{ |
|
$table = parse_name($this->make->table, 1); |
|
$time = date('Y-m-d H:i'); |
|
$namespace = ConfigHelper::get('namespace.model'); |
|
$parentNamespace = ConfigHelper::get('namespace.common_model'); |
|
|
|
$code = <<<CODE |
|
<?php |
|
/** |
|
* |
|
* {$table}.php |
|
* User: ChenLong |
|
* DateTime: {$time} |
|
*/ |
|
|
|
namespace {$namespace}; |
|
|
|
class {$table} extends \\{$parentNamespace}\\{$table} |
|
{ |
|
|
|
{$this->attr} |
|
|
|
} |
|
|
|
CODE; |
|
|
|
return $this->make->makeFile(ConfigHelper::get('file_url.model') . "{$table}.php", $code); |
|
} |
|
|
|
|
|
/** |
|
* 字段设置 |
|
* @return string |
|
*/ |
|
private function fieldSet() |
|
{ |
|
$tableField = (new Basics())->getTableInfo($this->make->table); |
|
|
|
return Basics::toLegalArrayCode(array_column($tableField, 'column_type', 'column_name')); |
|
} |
|
|
|
/** |
|
* 分类值展示处理 |
|
*/ |
|
private function getAttr() |
|
{ |
|
foreach ($this->make->makeData as $field => $datum) { |
|
if (!is_array($datum['join'])) { |
|
continue; |
|
} |
|
|
|
$this->getAttrData[$field] = $datum['join']; |
|
} |
|
$this->getAttrData and $this->useArr[] = "use sdModule\layui\Tag;"; |
|
} |
|
|
|
|
|
/** |
|
* 分类值处理 |
|
*/ |
|
private function attrCode() |
|
{ |
|
foreach ($this->getAttrData as $field => $getAttrDatum) { |
|
$field = parse_name($field, 1); |
|
$i = 0; |
|
$getAttrDatumTag = array_map(function ($value)use(&$i) { |
|
return "|Tag::init()->{$this->tag[++$i]}('{$value}')|"; |
|
}, $getAttrDatum); |
|
|
|
$getAttrDatum = Basics::toLegalArrayCode($getAttrDatum, 4); |
|
$getAttrDatumTag = Basics::toLegalArrayCode($getAttrDatumTag, 4); |
|
|
|
$this->attr .= <<<CODE |
|
|
|
/** |
|
* {$this->make->makeData[parse_name($field)]['label']}展示处理 |
|
* @param \$value |
|
* @return string |
|
*/ |
|
public function get{$field}Attr(\$value) |
|
{ |
|
\$field = self::get{$field}Sc(); |
|
|
|
return \$field[\$value] ?? \$value; |
|
} |
|
|
|
CODE; |
|
$this->commonAttr .= <<<CODE |
|
|
|
/** |
|
* {$this->make->makeData[parse_name($field)]['label']}返回值处理 |
|
* @param bool \$tag |
|
* @return array |
|
*/ |
|
public static function get{$field}Sc(\$tag = true) |
|
{ |
|
return \$tag === true |
|
? {$getAttrDatumTag} |
|
: {$getAttrDatum}; |
|
} |
|
|
|
CODE; |
|
|
|
|
|
} |
|
} |
|
} |
|
|
|
|