Files
kiri-gii/GiiController.php
T

700 lines
19 KiB
PHP
Raw Normal View History

2022-03-17 10:35:14 +08:00
<?php
declare(strict_types=1);
namespace Gii;
use Exception;
use Kiri;
2023-04-17 15:45:21 +08:00
use Kiri\ToArray;
2022-03-17 10:35:14 +08:00
use ReflectionException;
use function logger;
/**
* Class GiiController
* @package Gii
*/
class GiiController extends GiiBase
{
public string $className = '';
public array $fields = [];
/**
* GiiController constructor.
* @param $className
* @param $fields
*/
2023-04-11 16:40:59 +08:00
public function __construct($className, $fields, $tableName)
2022-03-17 10:35:14 +08:00
{
$this->className = $className;
$this->fields = $fields;
2023-04-11 16:40:59 +08:00
$this->tableName = $tableName;
2022-03-17 10:35:14 +08:00
}
/**
* @return string|bool
* @throws ReflectionException
* @throws Exception
*/
public function generate(): string|bool
{
$path = $this->getControllerPath();
$modelPath = $this->getModelPath();
$managerName = $this->className;
$namespace = rtrim($path['namespace'], '\\');
$model_namespace = rtrim($modelPath['namespace'], '\\');
$class = '';
$controller = str_replace('\\\\', '\\', "$namespace\\{$managerName}Controller");
$html = "<?php
namespace {$namespace};
";
if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) {
2023-04-17 15:49:43 +08:00
try {
$class = new \ReflectionClass($controller);
$import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class);
} catch (\Throwable $Exception) {
logger()->addError($Exception, 'throwable');
exit();
}
2022-03-17 10:35:14 +08:00
} else {
2023-04-17 16:05:52 +08:00
$import = "use Exception;
2022-03-17 10:35:14 +08:00
use Kiri\Core\Str;
use Kiri\Core\Json;
2023-04-17 16:05:52 +08:00
use Kiri\Router\Base\Controller;
2022-03-17 10:35:14 +08:00
use {$model_namespace}\\{$managerName};
2023-04-17 15:45:21 +08:00
use Kiri\Router\Validator\BindForm;
use Kiri\Router\Validator\Validator;
use Psr\Http\Message\ResponseInterface;
2023-04-17 16:05:52 +08:00
use Kiri\Router\Annotate\AutoController;
2023-04-17 15:45:21 +08:00
2022-03-17 10:35:14 +08:00
";
}
if (!empty($import)) {
$html .= $import;
}
$controllerName = $managerName;
$historyModel = "use {$model_namespace}\\{$managerName};";
if (!str_contains($html, $historyModel)) {
$html .= $historyModel;
}
$html .= "
/**
* Class {$controllerName}Controller
*
* @package controller
*/
2023-04-17 16:05:52 +08:00
#[AutoController] class {$controllerName}Controller extends Controller
2022-03-17 10:35:14 +08:00
{
";
$funcNames = [];
if (is_object($class)) {
$html .= $this->getClassProperty($class);
$html .= $this->getClassMethods($class);
}
2023-04-17 15:45:21 +08:00
$default = ['actionAdd', 'actionUpdate', 'actionAuditing', 'actionBatchAuditing', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList'];
2022-03-17 10:35:14 +08:00
foreach ($default as $key => $val) {
if (str_contains($html, ' function ' . $val . '(')) {
continue;
}
$html .= $this->{'controllerMethod' . str_replace('action', '', $val)}($this->fields, $managerName, $managerName, $path) . "\n";
}
$html .= '
}';
2023-04-11 16:40:59 +08:00
$file = APP_PATH . 'routes/' . $this->input->getOption('database') . '.php';
if (!file_exists($file)) {
2023-04-17 15:49:43 +08:00
touch($file);
file_put_contents($file, '<?php' . PHP_EOL);
file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, 'use Kiri\Message\Handler\Router;' . PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND);
2023-04-11 16:40:59 +08:00
}
$tableName = str_replace($this->db->tablePrefix, '', $this->tableName);
$tableName = str_replace('_', '-', $tableName);
2023-04-11 16:43:52 +08:00
$addRouter = 'Router::group([\'prefix\' => \'' . $tableName . '\',\'namespace\' => \'' . $namespace . '\'], function () {
2023-04-11 16:40:59 +08:00
Router::post(\'add\', \'' . $controllerName . 'Controller@actionAdd\');
Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\');
Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\');
Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\');
Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\');
Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\');
Router::post(\'delete\', \'' . $controllerName . 'Controller@actionDelete\');
Router::get(\'detail\', \'' . $controllerName . 'Controller@actionDetail\');
});
';
2023-04-11 16:43:52 +08:00
if (!str_contains($this->clearBlank(file_get_contents($file)), $this->clearBlank($addRouter))) {
2023-04-17 15:49:43 +08:00
file_put_contents($file, $addRouter, FILE_APPEND);
2023-04-11 16:40:59 +08:00
}
2022-09-21 11:44:43 +08:00
2022-03-17 10:35:14 +08:00
$file = $path['path'] . '/' . $controllerName . 'Controller.php';
if (file_exists($file)) {
2023-04-17 15:49:43 +08:00
unlink($file);
2022-03-17 10:35:14 +08:00
}
2023-04-17 15:49:43 +08:00
Kiri::writeFile($file, $html);
2022-03-17 10:35:14 +08:00
return $controllerName . 'Controller.php';
}
/**
* @return array
*/
private function getControllerPath(): array
{
$dbName = $this->db->id;
if (empty($dbName) || $dbName == 'db') {
$dbName = '';
}
$module = empty($this->module) ? '' : $this->module;
$modelPath['namespace'] = $this->controllerNamespace . $module;
$modelPath['path'] = $this->controllerPath . $module;
if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']);
}
if (!empty($dbName)) {
$modelPath['namespace'] = $this->controllerNamespace . ucfirst($dbName);
$modelPath['path'] = $this->controllerPath . ucfirst($dbName);
}
$modelPath['namespace'] = rtrim($modelPath['namespace'], '\\');
$modelPath['path'] = rtrim($modelPath['path'], '\\');
if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']);
}
return $modelPath;
}
/**
* @param $fields
* @param $className
* @param null $object
* @param $path
* @return string
* 新增
*/
public function controllerMethodAdd($fields, $className, $object, $path): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
2023-04-17 16:05:52 +08:00
$this->getData($className, $fields);
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionAdd(#[BindForm(' . $className . 'Form::class)] Validator $form): ResponseInterface
2022-03-17 10:35:14 +08:00
{
2023-04-17 15:45:21 +08:00
if (!$form->run($this->request)) {
return $this->response->json([\'code\' => 500, \'message\' => $form->error()]);
}
2022-03-17 10:35:14 +08:00
$model = new ' . $className . '();
2023-04-17 16:05:52 +08:00
$model->attributes = $form->getFormData()->toArray();
2022-03-17 10:35:14 +08:00
if (!$model->save()) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
2022-09-21 11:44:43 +08:00
} else {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 0, \'data\' => $model->toArray()]);
2022-09-21 11:44:43 +08:00
}
}';
}
public function controllerMethodAuditing($fields, $className, $object, $path): string
{
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-09-21 11:44:43 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionAuditing(): ResponseInterface
2022-09-21 11:44:43 +08:00
{
$model = ' . $className . '::findOne($this->request->post(\'id\', 0));
if (empty($model)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => \'必填项不能为空\']);
2022-09-21 11:44:43 +08:00
}
if (!$model->update([\'state\' => 1])) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
2022-09-21 11:44:43 +08:00
} else {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 0, \'data\' => $model->toArray()]);
2022-09-21 11:44:43 +08:00
}
}';
}
public function controllerMethodBatchAuditing($fields, $className, $object, $path): string
{
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-09-21 11:44:43 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionBatchAuditing(): ResponseInterface
2022-09-21 11:44:43 +08:00
{
2023-04-17 15:45:21 +08:00
$ids = $this->request->post(\'ids\', []);
2022-09-21 11:44:43 +08:00
if (empty($ids)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => \'必填项不能为空\']);
2022-09-21 11:44:43 +08:00
}
if (!' . $className . '::query()->whereIn(\'id\', $ids)->update([\'state\' => 1])) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => \'系统繁忙, 请稍后再试\']);
2022-09-21 11:44:43 +08:00
} else {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 0, \'message\' => \'ok\']);
2022-03-17 10:35:14 +08:00
}
}';
}
2022-09-21 11:44:43 +08:00
2022-03-17 10:35:14 +08:00
/**
* @param $fields
* @param $className
* @param null $object
* @param array $path
* @return string
* 构建更新
*/
public function controllerMethodUpdate($fields, $className, $object = NULL, $path = []): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionUpdate(#[BindForm(' . $className . 'Form::class)] Validator $form): ResponseInterface
2022-03-17 10:35:14 +08:00
{
2023-04-17 15:45:21 +08:00
if (!$form->run($this->request)) {
return $this->response->json([\'code\' => 500, \'message\' => $form->error()]);
}
2022-03-17 10:35:14 +08:00
$model = ' . $className . '::findOne($this->request->post(\'id\', 0));
if (empty($model)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => SELECT_IS_NULL]);
2022-03-17 10:35:14 +08:00
}
2023-04-17 16:05:52 +08:00
$model->attributes = $form->getFormData()->toArray();
2022-03-17 10:35:14 +08:00
if (!$model->save()) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
2022-09-21 11:44:43 +08:00
} else {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 0, \'data\' => $model->toArray()]);
2022-03-17 10:35:14 +08:00
}
}';
}
/**
* @param $fields
* @param $className
* @param null $object
* @param array $path
* @return string
* 构建更新
*/
public function controllerMethodBatchDelete($fields, $className, $object = NULL, $path = []): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionBatchDelete(): ResponseInterface
2022-03-17 10:35:14 +08:00
{
$_key = $this->request->array(\'ids\');
if (empty($_key)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => PARAMS_IS_NULL]);
2022-03-17 10:35:14 +08:00
}
2022-03-17 10:57:02 +08:00
$model = ' . $className . '::query()->whereIn(\'id\', $_key);
2022-03-17 10:35:14 +08:00
if (!$model->delete()) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => DB_ERROR_BUSY]);
} else {
return $this->response->json([\'code\' => 0, \'data\' => $_key]);
2022-03-17 10:35:14 +08:00
}
}';
}
/**
* @param $fields
* @param $className
* @param $managerName
* @param array $path
* @return string
* 构建详情
*/
public function controllerMethodDetail($fields, $className, $managerName, $path = []): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionDetail(): ResponseInterface
2022-03-17 10:35:14 +08:00
{
$model = ' . $managerName . '::findOne($this->request->query(\'id\'));
if (empty($model)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => SELECT_IS_NULL]);
} else {
return $this->response->json([\'code\' => 0, \'data\' => $model->toArray()]);
2022-03-17 10:35:14 +08:00
}
}';
}
/**
* @param $fields
* @param $className
* @param $managerName
* @param $path
* @return string
* 构建删除操作
*/
public function controllerMethodDelete($fields, $className, $managerName, $path): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionDelete(): ResponseInterface
2022-03-17 10:35:14 +08:00
{
$_key = $this->request->int(\'id\', true);
$model = ' . $managerName . '::findOne($_key);
if (empty($model)) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => SELECT_IS_NULL]);
2022-03-17 10:35:14 +08:00
}
if (!$model->delete()) {
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
} else {
return $this->response->json([\'code\' => 0, \'message\' => \'ok\']);
2022-03-17 10:35:14 +08:00
}
}';
}
/**
* @param $fields
* @param $className
* @param $managerName
* @param array $path
* @return string
* 构建查询列表
*/
public function controllerMethodList($fields, $className, $managerName, $path = []): string
{
$_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
2022-09-21 11:44:43 +08:00
$_path = ltrim($_path, '/');
2022-03-17 10:35:14 +08:00
return '
/**
2023-04-17 16:05:52 +08:00
* @return ResponseInterface
2022-03-17 10:35:14 +08:00
* @throws Exception
*/
2023-04-17 15:45:21 +08:00
public function actionList(): ResponseInterface
2022-03-17 10:35:14 +08:00
{
//分页处理
$count = $this->request->query(\'count\', -1);
$order = $this->request->query(\'order\', \'id\');
if (!empty($order)) {
$order .= !$this->request->query(\'isDesc\', 0) ? \' asc\' : \' desc\';
} else {
$order = \'id desc\';
}
2022-09-05 15:28:41 +08:00
$pWhere = [];
2022-09-21 11:44:43 +08:00
' . $this->getWhere($fields) . '
2022-03-17 10:35:14 +08:00
//列表输出
2022-09-05 15:28:41 +08:00
$model = ' . $managerName . '::query()->where($pWhere)->orderBy($order);
2022-03-17 10:35:14 +08:00
$keyword = $this->request->query(\'keyword\', null);
if (!empty($keyword)) {
2022-03-17 10:57:02 +08:00
$model->whereLike(\'keyword\', $keyword);
2022-03-17 10:35:14 +08:00
}
if ((int) $count === 1) {
$count = $model->count();
}
2023-04-17 15:45:21 +08:00
[$offset, $size] = $this->getPageInfo();
2022-03-17 10:35:14 +08:00
if ($count != -100) {
2023-04-17 15:45:21 +08:00
$model->offset($offset)->limit($size);
2022-03-17 10:35:14 +08:00
}
$data = $model->all()->toArray();
2023-04-17 15:45:21 +08:00
return $this->response->json([\'code\' => 0, \'data\' => $data, \'count\' => $count]);
2022-03-17 10:35:14 +08:00
}
';
}
2023-04-17 15:45:21 +08:00
private function getData($formClass, $fields): string
2022-03-17 10:35:14 +08:00
{
$html = '';
$length = $this->getMaxLength($fields);
2023-04-17 15:45:21 +08:00
$class = '';
$header = [];
$toArray = [];
2022-03-17 10:35:14 +08:00
foreach ($fields as $key => $val) {
2023-04-17 15:45:21 +08:00
if (str_starts_with($val['Type'], 'enum')) {
preg_match('/\(.*\)/', $val['Type'], $number);
$number[0] = trim($number[0], '()');
$values = explode(',', $number[0]);
$number[1] = 0;
foreach ($values as $evalue) {
$evalue = trim($evalue, '\'');
$leng = mb_strlen($evalue);
if ($number[1] < $leng) {
$number[1] = $leng;
}
}
2022-03-17 10:35:14 +08:00
2023-04-17 15:45:21 +08:00
$type = strtolower(preg_replace('/\(\'\w+\'(,\'\w+\')*\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type);
} else {
preg_match('/\((\d+)(,(\d+))*\)/', $val['Type'], $number);
$type = strtolower(preg_replace('/\(\d+(,\d+)*\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type);
}
if ($val['Extra'] == 'auto_increment') continue;
2022-03-17 10:35:14 +08:00
if ($type == 'timestamp') continue;
$_field = [];
$_field['required'] = $this->checkIsRequired($val);
foreach ($this->type as $_key => $value) {
if (!in_array(strtolower($first), $value)) continue;
2023-04-17 15:45:21 +08:00
$comment = $val['Comment'];
2022-03-17 10:35:14 +08:00
$_field['type'] = $_key;
2023-04-17 15:45:21 +08:00
$toArray[] = '
\'' . str_pad($val['Field'] . '\'', $length, ' ', STR_PAD_RIGHT) . ' => $this->' . $val['Field'] . ',';
2022-03-17 10:35:14 +08:00
if ($type == 'date' || $type == 'datetime' || $type == 'time') {
2023-04-17 15:45:21 +08:00
if (!in_array('use Kiri\Router\Validator\Inject\Length;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\Length;';
}
$class .= match ($type) {
'date' => '
/**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/
#[Length(10)]
public ?' . $_key . ' $' . $val['Field'] . ' = null;
',
'time' => '
/**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/
#[Length(5)]
public ?' . $_key . ' $' . $val['Field'] . ' = null;
',
default => '
/**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/
#[Length(16)]
public ?' . $_key . ' $' . $val['Field'] . ' = null;
',
2022-03-17 10:35:14 +08:00
};
2023-04-17 16:57:37 +08:00
} else if ($_key == 'json' || $_key == 'text' || $_key == 'longtext') {
2023-04-17 16:39:17 +08:00
$class .= '
/**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/
public ?array $' . $val['Field'] . ' = null;
';
2022-03-17 10:35:14 +08:00
} else {
if (isset($number[0])) {
if (strpos(',', $number[0])) {
$_field['min'] = $number[1];
$_field['max'] = $number[3];
} else {
$_field['min'] = 0;
$_field['max'] = $number[1];
}
}
2023-04-17 15:45:21 +08:00
if ($type == 'enum' && !in_array('use Kiri\Router\Validator\Inject\In;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\In;';
}
if ($_field['required'] == 'true' && !in_array('use Kiri\Router\Validator\Inject\Required;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\Required;';
}
if (!in_array('use Kiri\Router\Validator\Inject\MaxLength;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\MaxLength;';
2022-03-17 10:35:14 +08:00
}
2023-04-17 15:45:21 +08:00
$class .= '
/**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/' . ($type == 'enum' ? '
#[In([' . $number[0] . '])]' : '') . '' . ($_field['required'] == 'true' ? '
#[Required]' : '') . '
2023-04-17 16:31:03 +08:00
#[MaxLength(' . ($number[1] ?? 0) . ')]
2023-04-17 15:45:21 +08:00
public ?' . $_key . ' $' . $val['Field'] . ' = null;
';
2022-03-17 10:35:14 +08:00
}
}
$this->rules[$val['Field']] = $_field;
}
2023-04-17 15:45:21 +08:00
if (!is_dir($_SERVER['PWD'] . '/app/Form/')) {
mkdir($_SERVER['PWD'] . '/app/Form/');
}
if (!file_exists($_SERVER['PWD'] . '/app/Form/' . $formClass . 'Form.php')) {
touch($_SERVER['PWD'] . '/app/Form/' . $formClass . 'Form.php');
}
file_put_contents($_SERVER['PWD'] . '/app/Form/' . $formClass . 'Form.php', '<?php
namespace App\Form;
use Kiri\ToArray;
' . implode(PHP_EOL, $header) . PHP_EOL . PHP_EOL . '
/**
* FormData
*/
class ' . $formClass . 'Form implements ToArray, \JsonSerializable, \Stringable
{
' . $class . '
/**
* @return bool|string
*/
public function jsonSerialize(): bool|string
{
// TODO: Implement jsonSerialize() method.
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
}
/**
* @return string
*/
public function __toString(): string
{
// TODO: Implement __toString() method.
$json = $this->jsonSerialize();
if (!$json) {
return \'\';
}
return $json;
}
/**
* @return array
*/
public function toArray(): array
{
return [' . implode($toArray) . '
];
}
}');
2022-03-17 10:35:14 +08:00
return $html;
}
/**
* @param $fields
* @return int
*/
private function getMaxLength($fields): int
{
$length = 0;
foreach ($fields as $key => $val) {
if (mb_strlen($val['Field'] . ' >=') > $length) $length = mb_strlen($val['Field'] . ' >=');
}
return $length;
}
/**
* @param $fields
* @return string
*/
private function getWhere($fields): string
{
$html = '';
$length = $this->getMaxLength($fields);
foreach ($fields as $key => $val) {
preg_match('/\d+/', $val['Type'], $number);
$type = strtolower(preg_replace('/\(\d+\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type);
if ($type == 'timestamp') continue;
if ($type == 'json') continue;
foreach ($this->type as $_key => $value) {
if (!in_array(strtolower($first), $value)) continue;
$comment = '//' . $val['Comment'];
if ($type == 'date' || $type == 'datetime' || $type == 'time') {
$_tps = '$this->request->query(\'' . $val['Field'] . '\', null)';
$html .= '
$pWhere[\'' . str_pad($val['Field'] . ' <=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
$html .= '
$pWhere[\'' . str_pad($val['Field'] . ' >=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
} else {
$_tps = '$this->request->query(\'' . $val['Field'] . '\', null)';
$html .= '
$pWhere[\'' . str_pad($val['Field'] . '\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
}
}
}
return $html;
}
}