升级Thinkphp到5127版本

This commit is contained in:
cxdbz committed 2019-06-06 19:31:46 +08:00
1 parent 22a8f915e3
commit 9b8bd06b33
24 files changed
+504 -185

No files matched your search

@@ -130,38 +130,52 @@ trait Conversion
*/
public function toArray()
{
$item = [];
$visible = [];
$hidden = [];
$item = [];
$hasVisible = false;
foreach ($this->visible as $key => $val) {
if (is_string($val)) {
if (strpos($val, '.')) {
list($relation, $name) = explode('.', $val);
$this->visible[$relation][] = $name;
} else {
$this->visible[$val] = true;
$hasVisible = true;
}
unset($this->visible[$key]);
}
}
foreach ($this->hidden as $key => $val) {
if (is_string($val)) {
if (strpos($val, '.')) {
list($relation, $name) = explode('.', $val);
$this->hidden[$relation][] = $name;
} else {
$this->hidden[$val] = true;
}
unset($this->hidden[$key]);
}
}
// 合并关联数据
$data = array_merge($this->data, $this->relation);
// 过滤属性
if (!empty($this->visible)) {
$array = $this->parseAttr($this->visible, $visible);
if (!empty($array)) {
$data = array_intersect_key($data, array_flip($array));
}
}
if (empty($array) && !empty($this->hidden)) {
$array = $this->parseAttr($this->hidden, $hidden);
$data = array_diff_key($data, array_flip($array));
}
foreach ($data as $key => $val) {
if ($val instanceof Model || $val instanceof ModelCollection) {
// 关联模型对象
if (isset($visible[$key])) {
$val->visible($visible[$key]);
} elseif (isset($hidden[$key])) {
$val->hidden($hidden[$key]);
if (isset($this->visible[$key]) && is_array($this->visible[$key])) {
$val->visible($this->visible[$key]);
} elseif (isset($this->hidden[$key]) && is_array($this->hidden[$key])) {
$val->hidden($this->hidden[$key]);
}
// 关联模型对象
$item[$key] = $val->toArray();
} else {
// 模型属性
if (!isset($this->hidden[$key]) || true !== $this->hidden[$key]) {
$item[$key] = $val->toArray();
}
} elseif (isset($this->visible[$key])) {
$item[$key] = $this->getAttr($key);
} elseif (!isset($this->hidden[$key]) && !$hasVisible) {
$item[$key] = $this->getAttr($key);
}
}
@@ -252,28 +266,4 @@ trait Conversion
return $collection;
}
/**
* 解析隐藏及显示属性
* @access protected
* @param array $attrs 属性
* @param array $result 结果集
* @return array
*/
protected function parseAttr($attrs, &$result)
{
$array = [];
foreach ($attrs as $key => $val) {
if (is_array($val)) {
$result[$key] = $val;
} elseif (strpos($val, '.')) {
list($key, $name) = explode('.', $val);
$result[$key][] = $name;
} else {
$array[] = $val;
}
}
return $array;
}
}