chengkun
2025-09-04 0ea78440cb53b7ecf0ef715e3f51d88918c5821d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
 
namespace think;
 
use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use think\contract\Arrayable;
use think\contract\Jsonable;
use think\helper\Arr;
use Traversable;
 
/**
 * 数据集管理类
 *
 * @template TKey of array-key
 * @template-covariant TValue
 *
 * @implements ArrayAccess<TKey, TValue>
 * @implements IteratorAggregate<TKey, TValue>
 */
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Arrayable, Jsonable
{
    /**
     * 数据集数据
     * @var array
     */
    protected $items = [];
 
    /**
     * 构造函数
     * @param iterable<TKey, TValue>|Collection<TKey, TValue> $items 数据
     */
    public function __construct($items = [])
    {
        $this->items = $this->convertToArray($items);
    }
 
    /**
     * @param iterable<TKey, TValue>|Collection<TKey, TValue> $items
     * @return static<TKey, TValue>
     */
    public static function make($items = [])
    {
        return new static($items);
    }
 
    /**
     * 是否为空
     * @return bool
     */
    public function isEmpty(): bool
    {
        return empty($this->items);
    }
 
    public function toArray(): array
    {
        return array_map(function ($value) {
            return $value instanceof Arrayable ? $value->toArray() : $value;
        }, $this->items);
    }
 
    /**
     * @return array<TKey, TValue>
     */
    public function all(): array
    {
        return $this->items;
    }
 
    /**
     * 合并数组
     *
     * @param mixed $items 数据
     * @return static
     */
    public function merge($items)
    {
        return new static(array_merge($this->items, $this->convertToArray($items)));
    }
 
    /**
     * 按指定键整理数据
     *
     * @param mixed  $items    数据
     * @param string|null $indexKey 键名
     * @return array
     */
    public function dictionary($items = null, ?string &$indexKey = null)
    {
        if ($items instanceof self) {
            $items = $items->all();
        }
 
        $items = is_null($items) ? $this->items : $items;
 
        if ($items && empty($indexKey)) {
            $indexKey = is_array($items[0]) ? 'id' : $items[0]->getPk();
        }
 
        if (isset($indexKey) && is_string($indexKey)) {
            return array_column($items, null, $indexKey);
        }
 
        return $items;
    }
 
    /**
     * 比较数组,返回差集
     *
     * @param mixed  $items    数据
     * @param string|null $indexKey 指定比较的键名
     * @return static<TKey, TValue>
     */
    public function diff($items, ?string $indexKey = null)
    {
        if ($this->isEmpty() || is_scalar($this->items[0])) {
            return new static(array_diff($this->items, $this->convertToArray($items)));
        }
 
        $diff       = [];
        $dictionary = $this->dictionary($items, $indexKey);
 
        if (is_string($indexKey)) {
            foreach ($this->items as $item) {
                if (!isset($dictionary[$item[$indexKey]])) {
                    $diff[] = $item;
                }
            }
        }
 
        return new static($diff);
    }
 
    /**
     * 比较数组,返回交集
     *
     * @param mixed  $items    数据
     * @param string|null $indexKey 指定比较的键名
     * @return static<TKey, TValue>
     */
    public function intersect($items, ?string $indexKey = null)
    {
        if ($this->isEmpty() || is_scalar($this->items[0])) {
            return new static(array_intersect($this->items, $this->convertToArray($items)));
        }
 
        $intersect  = [];
        $dictionary = $this->dictionary($items, $indexKey);
 
        if (is_string($indexKey)) {
            foreach ($this->items as $item) {
                if (isset($dictionary[$item[$indexKey]])) {
                    $intersect[] = $item;
                }
            }
        }
 
        return new static($intersect);
    }
 
    /**
     * 交换数组中的键和值
     *
     * @return static<TValue, TKey>
     */
    public function flip()
    {
        return new static(array_flip($this->items));
    }
 
    /**
     * 返回数组中所有的键名
     *
     * @return static<TKey, TValue>
     */
    public function keys()
    {
        return new static(array_keys($this->items));
    }
 
    /**
     * 返回数组中所有的值组成的新 Collection 实例
     * @return static<int, TValue>
     */
    public function values()
    {
        return new static(array_values($this->items));
    }
 
    /**
     * 删除数组的最后一个元素(出栈)
     *
     * @return TValue
     */
    public function pop()
    {
        return array_pop($this->items);
    }
 
    /**
     * 通过使用用户自定义函数,以字符串返回数组
     *
     * @param callable $callback 调用方法
     * @param mixed    $initial
     * @return mixed
     */
    public function reduce(callable $callback, $initial = null)
    {
        return array_reduce($this->items, $callback, $initial);
    }
 
    /**
     * 以相反的顺序返回数组。
     *
     * @return static<TKey, TValue>
     */
    public function reverse()
    {
        return new static(array_reverse($this->items));
    }
 
    /**
     * 删除数组中首个元素,并返回被删除元素的值
     *
     * @return TValue
     */
    public function shift()
    {
        return array_shift($this->items);
    }
 
    /**
     * 在数组结尾插入一个元素
     *
     * @param mixed  $value 元素
     * @param string|null $key   KEY
     * @return $this
     */
    public function push($value, ?string $key = null)
    {
        if (is_null($key)) {
            $this->items[] = $value;
        } else {
            $this->items[$key] = $value;
        }
 
        return $this;
    }
 
    /**
     * 把一个数组分割为新的数组块.
     *
     * @param int  $size 块大小
     * @param bool $preserveKeys
     * @return static
     */
    public function chunk(int $size, bool $preserveKeys = false)
    {
        $chunks = [];
 
        foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
            $chunks[] = new static($chunk);
        }
 
        return new static($chunks);
    }
 
    /**
     * 在数组开头插入一个元素
     *
     * @param mixed  $value 元素
     * @param string|null $key   KEY
     * @return $this
     */
    public function unshift($value, ?string $key = null)
    {
        if (is_null($key)) {
            array_unshift($this->items, $value);
        } else {
            $this->items = [$key => $value] + $this->items;
        }
 
        return $this;
    }
 
    /**
     * 给每个元素执行个回调
     *
     *
     * @param callable $callback 回调
     * @return $this
     */
    public function each(callable $callback)
    {
        foreach ($this->items as $key => $item) {
            $result = $callback($item, $key);
 
            if (false === $result) {
                break;
            } elseif (!is_object($item)) {
                $this->items[$key] = $result;
            }
        }
 
        return $this;
    }
 
    /**
     * 用回调函数处理数组中的元素
     *
     * @param callable|null $callback 回调
     * @return static<TKey, TValue>
     */
    public function map(callable $callback)
    {
        return new static(array_map($callback, $this->items));
    }
 
    /**
     * 用回调函数过滤数组中的元素
     *
     * @param callable|null $callback 回调
     * @return static<TKey, TValue>
     */
    public function filter(?callable $callback = null)
    {
        if ($callback) {
            return new static(array_filter($this->items, $callback));
        }
 
        return new static(array_filter($this->items));
    }
 
    /**
     * 根据字段条件过滤数组中的元素
     *
     * @param string $field    字段名
     * @param mixed  $operator 操作符
     * @param mixed  $value    数据
     * @return static<TKey, TValue>
     */
    public function where(string $field, $operator, $value = null)
    {
        if (is_null($value)) {
            $value    = $operator;
            $operator = '=';
        }
 
        return $this->filter(function ($data) use ($field, $operator, $value) {
            if (strpos($field, '.')) {
                [$field, $relation] = explode('.', $field);
 
                $result = $data[$field][$relation] ?? null;
            } else {
                $result = $data[$field] ?? null;
            }
 
            switch (strtolower($operator)) {
                case '===':
                    return $result === $value;
                case '!==':
                    return $result !== $value;
                case '!=':
                case '<>':
                    return $result != $value;
                case '>':
                    return $result > $value;
                case '>=':
                    return $result >= $value;
                case '<':
                    return $result < $value;
                case '<=':
                    return $result <= $value;
                case 'like':
                    return is_string($result) && false !== strpos($result, $value);
                case 'not like':
                    return is_string($result) && false === strpos($result, $value);
                case 'in':
                    return is_scalar($result) && in_array($result, $value, true);
                case 'not in':
                    return is_scalar($result) && !in_array($result, $value, true);
                case 'between':
                    [$min, $max] = is_string($value) ? explode(',', $value) : $value;
                    return is_scalar($result) && $result >= $min && $result <= $max;
                case 'not between':
                    [$min, $max] = is_string($value) ? explode(',', $value) : $value;
                    return is_scalar($result) && $result > $max || $result < $min;
                case '==':
                case '=':
                default:
                    return $result == $value;
            }
        });
    }
 
    /**
     * LIKE过滤
     *
     * @param string $field 字段名
     * @param string $value 数据
     * @return static<TKey, TValue>
     */
    public function whereLike(string $field, string $value)
    {
        return $this->where($field, 'like', $value);
    }
 
    /**
     * NOT LIKE过滤
     *
     * @param string $field 字段名
     * @param string $value 数据
     * @return static<TKey, TValue>
     */
    public function whereNotLike(string $field, string $value)
    {
        return $this->where($field, 'not like', $value);
    }
 
    /**
     * IN过滤
     *
     * @param string $field 字段名
     * @param array  $value 数据
     * @return static<TKey, TValue>
     */
    public function whereIn(string $field, array $value)
    {
        return $this->where($field, 'in', $value);
    }
 
    /**
     * NOT IN过滤
     *
     * @param string $field 字段名
     * @param array  $value 数据
     * @return static<TKey, TValue>
     */
    public function whereNotIn(string $field, array $value)
    {
        return $this->where($field, 'not in', $value);
    }
 
    /**
     * BETWEEN 过滤
     *
     * @param string $field 字段名
     * @param mixed  $value 数据
     * @return static<TKey, TValue>
     */
    public function whereBetween(string $field, $value)
    {
        return $this->where($field, 'between', $value);
    }
 
    /**
     * NOT BETWEEN 过滤
     *
     * @param string $field 字段名
     * @param mixed  $value 数据
     * @return static<TKey, TValue>
     */
    public function whereNotBetween(string $field, $value)
    {
        return $this->where($field, 'not between', $value);
    }
 
    /**
     * 返回数据中指定的一列
     *
     * @param string|null $columnKey 键名
     * @param string|null $indexKey  作为索引值的列
     * @return array
     */
    public function column(?string $columnKey, ?string $indexKey = null)
    {
        return array_column($this->items, $columnKey, $indexKey);
    }
 
    /**
     * 对数组排序
     *
     * @param callable|null $callback 回调
     * @return static<TKey, TValue>
     */
    public function sort(?callable $callback = null)
    {
        $items = $this->items;
 
        $callback = $callback ?: function ($a, $b) {
            return $a == $b ? 0 : (($a < $b) ? -1 : 1);
        };
 
        uasort($items, $callback);
 
        return new static($items);
    }
 
    /**
     * 指定字段排序
     *
     * @param string $field 排序字段
     * @param string $order 排序
     * @return $this
     */
    public function order(string $field, string $order = 'asc')
    {
        return $this->sort(function ($a, $b) use ($field, $order) {
            $fieldA = $a[$field] ?? null;
            $fieldB = $b[$field] ?? null;
 
            return 'desc' == strtolower($order) ? ($fieldB <=> $fieldA) : ($fieldA <=> $fieldB);
        });
    }
 
    /**
     * 将数组打乱
     *
     * @return static<TKey, TValue>
     */
    public function shuffle()
    {
        $items = $this->items;
 
        shuffle($items);
 
        return new static($items);
    }
 
    /**
     * 获取第一个单元数据
     *
     * @param callable|null $callback
     * @param null          $default
     * @return TValue
     */
    public function first(?callable $callback = null, $default = null)
    {
        return Arr::first($this->items, $callback, $default);
    }
 
    /**
     * 获取最后一个单元数据
     *
     * @param callable|null $callback
     * @param null          $default
     * @return TValue
     */
    public function last(?callable $callback = null, $default = null)
    {
        return Arr::last($this->items, $callback, $default);
    }
 
    /**
     * 截取数组
     *
     * @param int  $offset       起始位置
     * @param int|null $length       截取长度
     * @param bool $preserveKeys preserveKeys
     * @return static<TKey, TValue>
     */
    public function slice(int $offset, ?int $length = null, bool $preserveKeys = false)
    {
        return new static(array_slice($this->items, $offset, $length, $preserveKeys));
    }
 
    /**
     * @param TKey $key
     * @return bool
     */
    #[\ReturnTypeWillChange]
    public function offsetExists($offset) : bool
    {
        return array_key_exists($offset, $this->items);
    }
 
    /**
     * @param TKey $offset
     * @return TValue
     */
    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        return $this->items[$offset];
    }
 
    /**
     * @param TKey|null $offset
     * @param TValue $value
     * @return void
     */
    #[\ReturnTypeWillChange]
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->items[] = $value;
        } else {
            $this->items[$offset] = $value;
        }
    }
 
    /**
     * @param TKey $offset
     * @return void
     */
    #[\ReturnTypeWillChange]
    public function offsetUnset($offset)
    {
        unset($this->items[$offset]);
    }
 
    //Countable
    public function count(): int
    {
        return count($this->items);
    }
 
    /**
     * @return ArrayIterator<TKey, TValue>
     */
    #[\ReturnTypeWillChange]
    public function getIterator(): Traversable
    {
        return new ArrayIterator($this->items);
    }
 
    //JsonSerializable
    #[\ReturnTypeWillChange]
    public function jsonSerialize()
    {
        return $this->toArray();
    }
 
    /**
     * 转换当前数据集为JSON字符串
     *
     * @param integer $options json参数
     * @return string
     */
    public function toJson(int $options = JSON_UNESCAPED_UNICODE): string
    {
        return json_encode($this->toArray(), $options);
    }
 
    public function __toString()
    {
        return $this->toJson();
    }
 
    /**
     * 转换成数组
     *
     * @param mixed $items 数据
     * @return array
     */
    protected function convertToArray($items): array
    {
        if ($items instanceof self) {
            return $items->all();
        }
 
        return (array) $items;
    }
}