Completed
Push — 6.0 ( 6faeca...d95081 )
by liu
02:54
created

Collection::hidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
use think\Paginator;
18
19
/**
20
 * 模型数据集类
21
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class Collection extends BaseCollection
23
{
24
    /**
25
     * 延迟预载入关联查询
26
     * @access public
27
     * @param  array|string $relation 关联
28
     * @return $this
29
     */
30
    public function load($relation)
31
    {
32
        if (!$this->isEmpty()) {
33
            $item = current($this->items);
34
            $item->eagerlyResultSet($this->items, (array) $relation);
35
        }
36
37
        return $this;
38
    }
39
40
    /**
41
     * 删除数据集的数据
42
     * @access public
43
     * @return bool
44
     */
45
    public function delete(): bool
46
    {
47
        $this->each(function (Model $model) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
48
            $model->delete();
49
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
50
51
        return true;
52
    }
53
54
    /**
55
     * 更新数据
56
     * @access public
57
     * @param array $data       数据数组
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @param array $allowField 允许字段
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
59
     * @return bool
60
     */
61
    public function update(array $data, array $allowField = []): bool
62
    {
63
        $this->each(function (Model $model) use ($data, $allowField) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
64
            if (!empty($allowField)) {
65
                $model->allowField($allowField);
66
            }
67
68
            $model->save($data);
69
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
70
71
        return true;
72
    }
73
74
    /**
75
     * 设置需要隐藏的输出属性
76
     * @access public
77
     * @param  array $hidden 属性列表
78
     * @return $this
79
     */
80
    public function hidden(array $hidden)
81
    {
82
        $this->each(function (Model $model) use ($hidden) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
83
            $model->hidden($hidden);
84
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
85
86
        return $this;
87
    }
88
89
    /**
90
     * 设置需要输出的属性
91
     * @access public
92
     * @param  array $visible
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @return $this
94
     */
95
    public function visible(array $visible)
96
    {
97
        $this->each(function (Model $model) use ($visible) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
98
            $model->visible($visible);
99
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
100
101
        return $this;
102
    }
103
104
    /**
105
     * 设置需要追加的输出属性
106
     * @access public
107
     * @param  array $append 属性列表
108
     * @return $this
109
     */
110
    public function append(array $append)
111
    {
112
        $this->each(function (Model $model) use ($append) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
113
            $model->append($append);
114
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
115
116
        return $this;
117
    }
118
119
    /**
120
     * 设置父模型
121
     * @access public
122
     * @param  Model $parent 父模型
123
     * @return $this
124
     */
125
    public function setParent(Model $parent)
126
    {
127
        $this->each(function (Model $model) use ($parent) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
128
            $model->setParent($parent);
129
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
130
131
        return $this;
132
    }
133
134
    /**
135
     * 设置数据字段获取器
136
     * @access public
137
     * @param  string|array $name       字段名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 7 found
Loading history...
138
     * @param  callable     $callback   闭包获取器
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
139
     * @return $this
140
     */
141
    public function withAttr($name, $callback = null)
142
    {
143
        $this->each(function (Model $model) use ($name, $callback) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
144
            $model->withAttribute($name, $callback);
145
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
146
147
        return $this;
148
    }
149
150
    /**
151
     * 绑定(一对一)关联属性到当前模型
152
     * @access protected
153
     * @param  string $relation 关联名称
154
     * @param  array  $attrs    绑定属性
155
     * @return $this
156
     * @throws Exception
157
     */
158
    public function bindAttr(string $relation, array $attrs = [])
159
    {
160
        $this->each(function (Model $model) use ($relation, $attrs) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
161
            $model->bindAttr($relation, $attrs);
162
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
163
164
        return $this;
165
    }
166
167
    /**
168
     * 按指定键整理数据
169
     *
170
     * @access public
171
     * @param  mixed  $items    数据
172
     * @param  string $indexKey 键名
173
     * @return array
174
     */
175
    public function dictionary($items = null, string &$indexKey = null)
176
    {
177
        if ($items instanceof self || $items instanceof Paginator) {
178
            $items = $items->all();
179
        }
180
181
        $items = is_null($items) ? $this->items : $items;
182
183
        if ($items && empty($indexKey)) {
184
            $indexKey = $items[0]->getPk();
185
        }
186
187
        if (isset($indexKey) && is_string($indexKey)) {
188
            return array_column($items, null, $indexKey);
189
        }
190
191
        return $items;
192
    }
193
194
    /**
195
     * 比较数据集,返回差集
196
     *
197
     * @access public
198
     * @param  mixed  $items    数据
199
     * @param  string $indexKey 指定比较的键名
200
     * @return static
201
     */
202
    public function diff($items, string $indexKey = null)
203
    {
204
        if ($this->isEmpty()) {
205
            return new static($items);
206
        }
207
208
        $diff       = [];
209
        $dictionary = $this->dictionary($items, $indexKey);
210
211
        if (is_string($indexKey)) {
212
            foreach ($this->items as $item) {
213
                if (!isset($dictionary[$item[$indexKey]])) {
214
                    $diff[] = $item;
215
                }
216
            }
217
        }
218
219
        return new static($diff);
220
    }
221
222
    /**
223
     * 比较数据集,返回交集
224
     *
225
     * @access public
226
     * @param  mixed  $items    数据
227
     * @param  string $indexKey 指定比较的键名
228
     * @return static
229
     */
230
    public function intersect($items, string $indexKey = null)
231
    {
232
        if ($this->isEmpty()) {
233
            return new static([]);
234
        }
235
236
        $intersect  = [];
237
        $dictionary = $this->dictionary($items, $indexKey);
238
239
        if (is_string($indexKey)) {
240
            foreach ($this->items as $item) {
241
                if (isset($dictionary[$item[$indexKey]])) {
242
                    $intersect[] = $item;
243
                }
244
            }
245
        }
246
247
        return new static($intersect);
248
    }
249
}
250