改进属性过滤输出

This commit is contained in:
yunwuxin
2016-07-11 11:35:41 +08:00
parent e1406b969b
commit 6349adf19d
3 changed files with 37 additions and 26 deletions

View File

@@ -40,10 +40,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return empty($this->items);
}
public function toArray($allow = [])
public function toArray()
{
return array_map(function ($value) use ($allow) {
return ($value instanceof Model || $value instanceof self) ? $value->toArray($allow) : $value;
return array_map(function ($value) {
return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
}, $this->items);
}
@@ -344,13 +344,12 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 转换当前数据集为JSON字符串
* @access public
* @param array $allow 允许输出的属性列表
* @param integer $options json参数
* @return string
*/
public function toJson($allow = [], $options = JSON_UNESCAPED_UNICODE)
public function toJson($options = JSON_UNESCAPED_UNICODE)
{
return json_encode($this->toArray($allow), $options);
return json_encode($this->toArray(), $options);
}
public function __toString()

View File

@@ -62,6 +62,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 字段属性
protected $field = [];
// 显示属性
protected $visible = [];
// 隐藏属性
protected $hidden = [];
// 追加属性
@@ -469,35 +471,46 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this;
}
/**
* 设置需要输出的属性
* @param array $visible
* @return $this
*/
public function visible($visible = [])
{
$this->visible = $visible;
return $this;
}
/**
* 转换当前模型对象为数组
* @access public
* @param array $allow 允许输出的属性列表
* @return array
*/
public function toArray($allow = [])
public function toArray()
{
$item = [];
if (empty($allow)) {
$allow = array_keys($this->data);
}
$allow = array_diff($allow, $this->hidden);
foreach ($this->data as $key => $val) {
// 属性过滤输出
if (!in_array($key, $allow)) {
continue;
}
//过滤属性
if (!empty($this->visible)) {
$data = array_intersect_key($this->data, array_flip($this->visible));
} elseif (!empty($this->hidden)) {
$data = array_diff_key($this->data, array_flip($this->hidden));
} else {
$data = $this->data;
}
foreach ($data as $key => $val) {
if ($val instanceof Model || $val instanceof Collection) {
// 关联模型对象
$item[$key] = $val->toArray();
} elseif (is_array($val) && reset($val) instanceof Model) {
// 关联模型数据集
$data = [];
$arr = [];
foreach ($val as $k => $value) {
$data[$k] = $value->toArray();
$arr[$k] = $value->toArray();
}
$item[$key] = $data;
$item[$key] = $arr;
} else {
// 模型属性
$item[$key] = $this->getAttr($key);
@@ -515,13 +528,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/**
* 转换当前模型对象为JSON字符串
* @access public
* @param array $allow 允许输出的属性列表
* @param integer $options json参数
* @return string
*/
public function toJson($allow = [], $options = JSON_UNESCAPED_UNICODE)
public function toJson($options = JSON_UNESCAPED_UNICODE)
{
return json_encode($this->toArray($allow), $options);
return json_encode($this->toArray(), $options);
}
/**

View File

@@ -51,7 +51,7 @@ class Collection extends \think\Collection
return $this->paginator;
}
public function toArray($allow = [])
public function toArray()
{
if ($this->paginator) {
try {
@@ -64,10 +64,10 @@ class Collection extends \think\Collection
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'data' => parent::toArray($allow)
'data' => parent::toArray()
];
} else {
return parent::toArray($allow);
return parent::toArray();
}
}