Model类和Collection类的toarray和tojson方法增加allow参数 用于设置允许输出的属性

This commit is contained in:
thinkphp
2016-07-09 21:38:51 +08:00
parent 59dee0bdcc
commit 3e09d80cde
2 changed files with 24 additions and 22 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() public function toArray($allow = [])
{ {
return array_map(function ($value) { return array_map(function ($value) {
return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value; return ($value instanceof Model || $value instanceof self) ? $value->toArray($allow) : $value;
}, $this->items); }, $this->items);
} }
@@ -74,7 +74,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return new static(array_diff($this->items, $this->convertToArray($items))); return new static(array_diff($this->items, $this->convertToArray($items)));
} }
/** /**
* 交换数组中的键和值 * 交换数组中的键和值
* *
@@ -116,7 +115,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return array_pop($this->items); return array_pop($this->items);
} }
/** /**
* 通过使用用户自定义函数,以字符串返回数组 * 通过使用用户自定义函数,以字符串返回数组
* *
@@ -199,7 +197,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return $this; return $this;
} }
/** /**
* 用回调函数过滤数组中的元素 * 用回调函数过滤数组中的元素
* @param callable|null $callback * @param callable|null $callback
@@ -230,11 +227,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
foreach ($this->items as $row) { foreach ($this->items as $row) {
$key = $value = null; $key = $value = null;
$keySet = $valueSet = false; $keySet = $valueSet = false;
if ($index_key !== null && array_key_exists($index_key, $row)) { if (null !== $index_key && array_key_exists($index_key, $row)) {
$keySet = true; $keySet = true;
$key = (string) $row[$index_key]; $key = (string) $row[$index_key];
} }
if ($column_key === null) { if (null === $column_key) {
$valueSet = true; $valueSet = true;
$value = $row; $value = $row;
} elseif (is_array($row) && array_key_exists($column_key, $row)) { } elseif (is_array($row) && array_key_exists($column_key, $row)) {
@@ -252,7 +249,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return $result; return $result;
} }
/** /**
* 对数组排序 * 对数组排序
* *
@@ -275,7 +271,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return new static($items); return new static($items);
} }
/** /**
* 将数组打乱 * 将数组打乱
* *
@@ -349,12 +344,13 @@ 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($options = JSON_UNESCAPED_UNICODE) public function toJson($allow = [], $options = JSON_UNESCAPED_UNICODE)
{ {
return json_encode($this->toArray(), $options); return json_encode($this->toArray($allow), $options);
} }
public function __toString() public function __toString()

View File

@@ -472,9 +472,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 转换当前模型对象为数组 * 转换当前模型对象为数组
* @access public * @access public
* @param array $allow 允许输出的属性列表
* @return array * @return array
*/ */
public function toArray() public function toArray($allow = [])
{ {
$item = []; $item = [];
if (!empty($this->append)) { if (!empty($this->append)) {
@@ -482,9 +483,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$item[$name] = $this->getAttr($name); $item[$name] = $this->getAttr($name);
} }
} }
if (empty($allow)) {
$allow = array_keys($this->data);
}
$allow = array_diff($allow, $this->hidden);
foreach ($this->data as $key => $val) { foreach ($this->data as $key => $val) {
// 如果是隐藏属性不输出 // 属性过滤输出
if (in_array($key, $this->hidden)) { if (!in_array($key, $allow)) {
continue; continue;
} }
@@ -509,12 +514,13 @@ 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($options = JSON_UNESCAPED_UNICODE) public function toJson($allow = [], $options = JSON_UNESCAPED_UNICODE)
{ {
return json_encode($this->toArray(), $options); return json_encode($this->toArray($allow), $options);
} }
/** /**