Passed
Push — 5.2 ( 61517e...2a99ba )
by liu
02:49
created

Collection::bindAttr()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: zhangyajun <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\model;
14
15
use think\Collection as BaseCollection;
16
use think\Model;
17
18
class Collection extends BaseCollection
19
{
20
    /**
21
     * 延迟预载入关联查询
22
     * @access public
23
     * @param  array $relation 关联
24
     * @return $this
25
     */
26
    public function load(array $relation)
27
    {
28
        $item = current($this->items);
29
        $item->eagerlyResultSet($this->items, $relation);
30
31
        return $this;
32
    }
33
34
    /**
35
     * 设置需要隐藏的输出属性
36
     * @access public
37
     * @param  array $hidden   属性列表
38
     * @return $this
39
     */
40
    public function hidden(array $hidden)
41
    {
42
        $this->each(function (Model $model) use ($hidden) {
43
            $model->hidden($hidden);
44
        });
45
46
        return $this;
47
    }
48
49
    /**
50
     * 设置需要输出的属性
51
     * @access public
52
     * @param  array $visible
53
     * @return $this
54
     */
55
    public function visible(array $visible)
56
    {
57
        $this->each(function (Model $model) use ($visible) {
58
            $model->visible($visible);
59
        });
60
61
        return $this;
62
    }
63
64
    /**
65
     * 设置需要追加的输出属性
66
     * @access public
67
     * @param  array $append   属性列表
68
     * @return $this
69
     */
70
    public function append(array $append)
71
    {
72
        $this->each(function (Model $model) use ($append) {
73
            $model && $model->append($append);
74
        });
75
76
        return $this;
77
    }
78
79
    /**
80
     * 设置数据字段获取器
81
     * @access public
82
     * @param  string|array $name       字段名
83
     * @param  callable     $callback   闭包获取器
84
     * @return $this
85
     */
86
    public function withAttr($name, $callback = null)
87
    {
88
        $this->each(function ($model) use ($name, $callback) {
89
            /** @var Model $model */
90
            $model && $model->withAttribute($name, $callback);
91
        });
92
93
        return $this;
94
    }
95
96
    /**
97
     * 绑定(一对一)关联属性到当前模型
98
     * @access protected
99
     * @param  string   $relation    关联名称
100
     * @param  array    $attrs       绑定属性
101
     * @return $this
102
     * @throws Exception
103
     */
104
    public function bindAttr($relation, $attrs = [])
105
    {
106
        $this->each(function ($model) use ($relation, $attrs) {
107
            /** @var Model $model */
108
            $model && $model->bindAttr($relation, $attrs);
109
        });
110
111
        return $this;
112
    }
113
114
    /**
115
     * 按指定键整理数据
116
     *
117
     * @access public
118
     * @param  mixed    $items      数据
119
     * @param  string   $indexKey   键名
120
     * @return array
121
     */
122
    public function dictionary($items = null, string &$indexKey = null)
123
    {
124
        if ($items instanceof self || $items instanceof Paginator) {
0 ignored issues
show
Bug introduced by
The type think\model\Paginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
            $items = $items->all();
126
        }
127
128
        $items = is_null($items) ? $this->items : $items;
129
130
        if ($items && empty($indexKey)) {
131
            $indexKey = $items[0]->getPk();
132
        }
133
134
        if (isset($indexKey) && is_string($indexKey)) {
135
            return array_column($items, null, $indexKey);
136
        }
137
138
        return $items;
139
    }
140
141
    /**
142
     * 比较数据集,返回差集
143
     *
144
     * @access public
145
     * @param  mixed    $items      数据
146
     * @param  string   $indexKey   指定比较的键名
147
     * @return static
148
     */
149
    public function diff($items, string $indexKey = null)
150
    {
151
        if ($this->isEmpty()) {
152
            return new static($items);
153
        }
154
155
        $diff       = [];
156
        $dictionary = $this->dictionary($items, $indexKey);
157
158
        if (is_string($indexKey)) {
159
            foreach ($this->items as $item) {
160
                if (!isset($dictionary[$item[$indexKey]])) {
161
                    $diff[] = $item;
162
                }
163
            }
164
        }
165
166
        return new static($diff);
167
    }
168
169
    /**
170
     * 比较数据集,返回交集
171
     *
172
     * @access public
173
     * @param  mixed    $items      数据
174
     * @param  string   $indexKey   指定比较的键名
175
     * @return static
176
     */
177
    public function intersect($items, string $indexKey = null)
178
    {
179
        if ($this->isEmpty()) {
180
            return new static([]);
181
        }
182
183
        $intersect  = [];
184
        $dictionary = $this->dictionary($items, $indexKey);
185
186
        if (is_string($indexKey)) {
187
            foreach ($this->items as $item) {
188
                if (isset($dictionary[$item[$indexKey]])) {
189
                    $intersect[] = $item;
190
                }
191
            }
192
        }
193
194
        return new static($intersect);
195
    }
196
}
197