From 8311a4e39bb6f2c704e66fad74e868f5d1b6cdfd Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 23 Jan 2017 13:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 64 ++---------------------------- library/think/Model.php | 9 ++++- library/think/model/Collection.php | 48 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 63 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index 20bbe6a2..41b42759 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -20,12 +20,6 @@ use JsonSerializable; class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { protected $items = []; - // 显示属性 - protected $visible = []; - // 隐藏属性 - protected $hidden = []; - // 追加属性 - protected $append = []; public function __construct($items = []) { @@ -46,63 +40,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return empty($this->items); } - /** - * 设置需要隐藏的输出属性 - * @access public - * @param array $hidden 属性列表 - * @param bool $override 是否覆盖 - * @return $this - */ - public function hidden($hidden = [], $override = false) - { - $this->hidden = [$hidden, $override]; - return $this; - } - - /** - * 设置需要输出的属性 - * @param array $visible - * @param bool $override 是否覆盖 - * @return $this - */ - public function visible($visible = [], $override = false) - { - $this->visible = [$visible, $override]; - return $this; - } - - /** - * 设置需要追加的输出属性 - * @access public - * @param array $append 属性列表 - * @param bool $override 是否覆盖 - * @return $this - */ - public function append($append = [], $override = false) - { - $this->append = [$append, $override]; - return $this; - } - public function toArray() { - $result = []; - foreach ($this->items as $key => $item) { - if ($item instanceof Model || $item instanceof self) { - if (!empty($this->visible)) { - $item->visible($this->visible[0], $this->visible[1]); - } elseif (!empty($this->hidden)) { - $item->hidden($this->hidden[0], $this->hidden[1]); - } - if (!empty($this->append)) { - $item->append($this->append[0], $this->append[1]); - } - $result[$key] = $item->toArray(); - } else { - $result[$key] = $item; - } - } - return $result; + return array_map(function ($value) { + return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value; + }, $this->items); } public function all() diff --git a/library/think/Model.php b/library/think/Model.php index 06d95d0e..235fb2d1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -593,8 +593,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 解析隐藏及显示属性 * @access protected - * @param array $attrs 属性 - * @param array $result 结果集 + * @param array $attrs 属性 + * @param array $result 结果集 + * @param bool $visible * @return array */ protected function parseAttr($attrs, &$result, $visible = true) @@ -622,6 +623,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 转换子模型对象 * @access protected + * @param Model|Collection $model + * @param $visible + * @param $hidden + * @param $key * @return array */ protected function subToArray($model, $visible, $hidden, $key) diff --git a/library/think/model/Collection.php b/library/think/model/Collection.php index 5dd1da7e..08e11ad8 100644 --- a/library/think/model/Collection.php +++ b/library/think/model/Collection.php @@ -12,6 +12,7 @@ namespace think\model; use think\Collection as BaseCollection; +use think\Model; class Collection extends BaseCollection { @@ -28,4 +29,51 @@ class Collection extends BaseCollection return $this; } + /** + * 设置需要隐藏的输出属性 + * @access public + * @param array $hidden 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function hidden($hidden = [], $override = false) + { + $this->each(function ($model) use ($hidden, $override) { + /** @var Model $model */ + $model->hidden($hidden, $override); + }); + return $this; + } + + /** + * 设置需要输出的属性 + * @param array $visible + * @param bool $override 是否覆盖 + * @return $this + */ + public function visible($visible = [], $override = false) + { + $this->each(function ($model) use ($visible, $override) { + /** @var Model $model */ + $model->visible($visible, $override); + }); + return $this; + } + + /** + * 设置需要追加的输出属性 + * @access public + * @param array $append 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function append($append = [], $override = false) + { + $this->each(function ($model) use ($append, $override) { + /** @var Model $model */ + $model->append($append, $override); + }); + return $this; + } + }