From 6349adf19d2cc2f2dcd050274698db3a8547cf22 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 11 Jul 2016 11:35:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=B1=9E=E6=80=A7=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 11 +++--- library/think/Model.php | 46 ++++++++++++++++---------- library/think/paginator/Collection.php | 6 ++-- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index 3f91b7cb..8315268a 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -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() diff --git a/library/think/Model.php b/library/think/Model.php index b3fc26ad..47d422e5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -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); } /** diff --git a/library/think/paginator/Collection.php b/library/think/paginator/Collection.php index ddea7d5a..64fbb93b 100644 --- a/library/think/paginator/Collection.php +++ b/library/think/paginator/Collection.php @@ -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(); } }