Completed
Push — 6.0 ( 046a93...4b74b3 )
by liu
03:40 queued 10s
created

Collection::update()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
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  string|array $name       字段名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 7 found
Loading history...
123
     * @param  callable     $callback   闭包获取器
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
124
     * @return $this
125
     */
126
    public function withAttr($name, $callback = null)
127
    {
128
        $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...
129
            $model->withAttribute($name, $callback);
130
        });
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...
131
132
        return $this;
133
    }
134
135
    /**
136
     * 绑定(一对一)关联属性到当前模型
137
     * @access protected
138
     * @param  string $relation 关联名称
139
     * @param  array  $attrs    绑定属性
140
     * @return $this
141
     * @throws Exception
142
     */
143
    public function bindAttr(string $relation, array $attrs = [])
144
    {
145
        $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...
146
            $model->bindAttr($relation, $attrs);
147
        });
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...
148
149
        return $this;
150
    }
151
152
    /**
153
     * 按指定键整理数据
154
     *
155
     * @access public
156
     * @param  mixed  $items    数据
157
     * @param  string $indexKey 键名
158
     * @return array
159
     */
160
    public function dictionary($items = null, string &$indexKey = null)
161
    {
162
        if ($items instanceof self || $items instanceof Paginator) {
163
            $items = $items->all();
164
        }
165
166
        $items = is_null($items) ? $this->items : $items;
167
168
        if ($items && empty($indexKey)) {
169
            $indexKey = $items[0]->getPk();
170
        }
171
172
        if (isset($indexKey) && is_string($indexKey)) {
173
            return array_column($items, null, $indexKey);
174
        }
175
176
        return $items;
177
    }
178
179
    /**
180
     * 比较数据集,返回差集
181
     *
182
     * @access public
183
     * @param  mixed  $items    数据
184
     * @param  string $indexKey 指定比较的键名
185
     * @return static
186
     */
187
    public function diff($items, string $indexKey = null)
188
    {
189
        if ($this->isEmpty()) {
190
            return new static($items);
191
        }
192
193
        $diff       = [];
194
        $dictionary = $this->dictionary($items, $indexKey);
195
196
        if (is_string($indexKey)) {
197
            foreach ($this->items as $item) {
198
                if (!isset($dictionary[$item[$indexKey]])) {
199
                    $diff[] = $item;
200
                }
201
            }
202
        }
203
204
        return new static($diff);
205
    }
206
207
    /**
208
     * 比较数据集,返回交集
209
     *
210
     * @access public
211
     * @param  mixed  $items    数据
212
     * @param  string $indexKey 指定比较的键名
213
     * @return static
214
     */
215
    public function intersect($items, string $indexKey = null)
216
    {
217
        if ($this->isEmpty()) {
218
            return new static([]);
219
        }
220
221
        $intersect  = [];
222
        $dictionary = $this->dictionary($items, $indexKey);
223
224
        if (is_string($indexKey)) {
225
            foreach ($this->items as $item) {
226
                if (isset($dictionary[$item[$indexKey]])) {
227
                    $intersect[] = $item;
228
                }
229
            }
230
        }
231
232
        return new static($intersect);
233
    }
234
}
235