改进属性过滤输出

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

View File

@@ -62,6 +62,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 字段属性 // 字段属性
protected $field = []; protected $field = [];
// 显示属性
protected $visible = [];
// 隐藏属性 // 隐藏属性
protected $hidden = []; protected $hidden = [];
// 追加属性 // 追加属性
@@ -469,35 +471,46 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this; return $this;
} }
/**
* 设置需要输出的属性
* @param array $visible
* @return $this
*/
public function visible($visible = [])
{
$this->visible = $visible;
return $this;
}
/** /**
* 转换当前模型对象为数组 * 转换当前模型对象为数组
* @access public * @access public
* @param array $allow 允许输出的属性列表
* @return array * @return array
*/ */
public function toArray($allow = []) public function toArray()
{ {
$item = []; $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) { if ($val instanceof Model || $val instanceof Collection) {
// 关联模型对象 // 关联模型对象
$item[$key] = $val->toArray(); $item[$key] = $val->toArray();
} elseif (is_array($val) && reset($val) instanceof Model) { } elseif (is_array($val) && reset($val) instanceof Model) {
// 关联模型数据集 // 关联模型数据集
$data = []; $arr = [];
foreach ($val as $k => $value) { foreach ($val as $k => $value) {
$data[$k] = $value->toArray(); $arr[$k] = $value->toArray();
} }
$item[$key] = $data; $item[$key] = $arr;
} else { } else {
// 模型属性 // 模型属性
$item[$key] = $this->getAttr($key); $item[$key] = $this->getAttr($key);
@@ -515,13 +528,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 转换当前模型对象为JSON字符串 * 转换当前模型对象为JSON字符串
* @access public * @access public
* @param array $allow 允许输出的属性列表
* @param integer $options json参数 * @param integer $options json参数
* @return string * @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; return $this->paginator;
} }
public function toArray($allow = []) public function toArray()
{ {
if ($this->paginator) { if ($this->paginator) {
try { try {
@@ -64,10 +64,10 @@ class Collection extends \think\Collection
'total' => $total, 'total' => $total,
'per_page' => $this->listRows(), 'per_page' => $this->listRows(),
'current_page' => $this->currentPage(), 'current_page' => $this->currentPage(),
'data' => parent::toArray($allow) 'data' => parent::toArray()
]; ];
} else { } else {
return parent::toArray($allow); return parent::toArray();
} }
} }