加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ListModel.php 8.95 KB
一键复制 编辑 原始数据 按行查看 历史
NullFeng 提交于 2017-12-26 16:16 . 代码重构
<?php
/**
* Created by PhpStorm.
* User: NullFeng
* Date: 2017/12/22
* Time: 14:31
*/
namespace utils;
class ListModel
{
/**
* 数据源
*/
private $dataSource;
/**
* 列表头
*/
private $header;
/**
* 列表项
*/
private $rows;
/**
* 自定义按钮
*/
private $customs;
/**
* 自定义按钮的列名
*/
private $custom_header = ['title' => '操作'];
/**
* 设置数据源
* @param $dataSource
* @return $this
*/
public function setDataSource($dataSource)
{
$this->dataSource = $dataSource;
return $this;
}
/**
* 添加列
* @param $name
* @param $title
* @param array $option
* @return $this
*/
public function addColumn($name, $title, $option = array())
{
$pos = strpos($name, ' as ');
if ($pos !== false) {
$ref = substr($name, 0, $pos);
$name = substr($name, $pos + 1);
$option['ref'] = $ref;
}
$this->header[$name] = array(
'title' => $title,
'option' => $option
);
return $this;
}
/**
* 添加列
*/
public function setCustomHeader($title, $option = array())
{
$this->custom_header = array(
'title' => $title,
'option' => $option
);
return $this;
}
/**
* 添加自定义按钮
*/
public function addCustom($title, $url, $option = array())
{
$button = array(
'title' => $title,
'url' => $url,
'option' => $option
);
!empty($this->customs) || $this->header['__custom'] = &$this->custom_header;
$this->customs[] = $button;
return $this;
}
/**
* 解析条件表达式
*/
private function parse_expression($str, $scope)
{
$matches = [];
preg_match_all("/\\$\\{[a-zA-Z_]+[0-9_]*\\}/", $str, $matches);
foreach ($matches[0] as $k => $v) {
$matches[0][$k] = substr($v, 2, strlen($v) - 3);
}
foreach ($matches[0] as $k => $v) {
$str = str_replace('${' . $v . '}', '$scope[\'' . $v . '\']', $str);
}
$result = false;
eval('$result = ' . $str . ";");
return $result;
}
/**
* 解析 带函数的变量
* @param $str 含有变量的模板字符串
* @param $scope 其中变量所在的集合作用域数组
* @return mixed 解析之后的字符串
*/
private function parse_vars($str, $scope)
{
$matches = [];
preg_match_all('/\$\{[^$]*\}/', $str, $matches);
$vars = $matches[0];
foreach ($vars as $k => $v) {
$temp = [];
$result = "";
preg_match('/\$\{[^$^|]*/', $v, $temp);
$name = substr($temp[0], 2);
$flag = strpos($v, '|');
if ($flag !== false) {//没有函数调用
$len = strlen($v);
$fn = substr($v, $flag + 1, $len - 2 - $flag);
$flag = strpos($fn, '=');
if ($flag === false) {//没有参数
$result = $fn($scope[$name]);
} else {
$args_str = substr($fn, $flag + 1);
$fn = substr($fn, 0, $flag);
$args_str = str_replace('\\,', '&d;', $args_str);
$args = explode(',', $args_str);
foreach ($args as $key => $value) {
if ($value == "###") {
$args[$key] = $scope[$name];
} else {
$args[$key] = str_replace('&d;', ',', $value);
}
}
switch (count($args)) {
case 1:
$result = $fn($args[0]);
break;
case 2:
$result = $fn($args[0], $args[1]);
break;
case 3:
$result = $fn($args[0], $args[1], $args[2]);
break;
}
}
} else {
$name = substr($name, 0, strlen($name) - 1);
$result = $scope[$name];
}
$str = str_replace($v, $result, $str);
}
return $str;
}
/**
* 枚举
*/
private function parse_enum($params, $value)
{
if (is_array($params)) {
if (isset($params[$value])) {
$value = $params[$value];
} else {
if (isset($params['_default'])) {
$value = $params['_default'];
}
}
}
return $value;
}
/**
* 获取头部
*/
public function getHeaders()
{
return $this->header;
}
/**
* 生成数据
*/
public function fetchRows()
{
//处理数据源
foreach ($this->dataSource as $row_key => $row) {
foreach ($this->header as $col_key => $column) {
//根据表头处理ref引用列
if (!isset($row[$col_key])) {
if (isset($column['option']['ref']) && isset($row[$column['option']['ref']])) {
$row[$col_key] = $row[$column['option']['ref']];
} else {
$row[$col_key] = "";
}
}
if ($col_key == '__custom' && !empty($this->customs)) {
//处理自定义按钮
$custom_html = "";
foreach ($this->customs as $ctm_key => $custom) {
if (isset($custom['option'])) {
$option = $custom['option'];
//解析条件表达式
if (isset($option['show']) && is_string($option['show'])) {
if (!self::parse_expression($option['show'], $row))
continue;
}
$tag = isset($option['tag']) ? $option['tag'] : 'a';
$attr = isset($option['attr']) ? $option['attr'] : [];
$class_str = isset($option['class']) ? 'class="' . $option['class'] . '" ' : '';
$url = isset($custom['url']) ? $custom['url'] : '';
$url = urldecode($url);
$title = $custom['title'];
//处理自定义属性
$attr_html = "";
foreach ($attr as $k => $v) {
if ($k == "class" && !empty($class_str))
continue;
$attr_html .= "$k='$v' ";
}
switch ($tag) {
case 'a':
$custom_html .= "<$tag $attr_html $class_str href='$url'>$title</$tag>";
break;
case 'input':
$custom_html .= "<$tag $attr_html $class_str type='button' onclick='$url' value='$title' />";
break;
default:
$custom_html .= "<$tag $attr_html $class_str onclick='$url'>$title</$tag>";
break;
}
}
}
if (!empty($custom_html)) {
$custom_html = self::parse_vars($custom_html, $row);
}
$row['__custom'] = $custom_html;
} else if (isset($column['option'])) {
if (array_key_exists('default', $column['option'])) {
if (strlen(($row[$col_key] . '')) == 0) {
$row[$col_key] = $column['option']['default'];
continue;
}
}
if (array_key_exists('enum', $column['option'])) {
$row[$col_key] = self::parse_enum($column['option']['enum'], $row[$col_key]);
}
if (array_key_exists('format', $column['option'])) {
$row[$col_key] = self::parse_vars($column['option']['format'], $row);
}
}
}
$this->rows[] = $row;
}
return $this->rows;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化