artoodetoo/recordset

将数组作为一组记录进行操作

v1.2 2020-03-02 13:22 UTC

This package is auto-updated.

Last update: 2024-09-10 21:31:02 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

安装

composer require artoodetoo/recordset

基本示例

按SQL方式排序记录

use R2\Helpers\ArrayRecordset;

$records = [
    ['name' => 'Alfred', 'age' => 40],
    ['name' => 'Mark',   'age' => 40],
    ['name' => 'Lue',    'age' => 45],
    ['name' => 'Ameli',  'age' => 38],
    ['name' => 'Barb',   'age' => 38],
];
$result = (new ArrayRecordset($records))
    ->orderBy('age', 'desc')
    ->orderBy('name', 'asc')
    ->get();

// will be equal to 
//    [
//        ['name' => 'Lue',    'age' => 45],
//        ['name' => 'Alfred', 'age' => 40],
//        ['name' => 'Mark',   'age' => 40],
//        ['name' => 'Ameli',  'age' => 38],
//        ['name' => 'Barb',   'age' => 38],
//    ];

仅获取一列结果

$result = (new ArrayRecordset($records))
    ->pluck('name);

// will be equal to
// ['Alfred', 'Mark', 'Lue', 'Ameli', 'Barb'] 

$result = (new ArrayRecordset($records))
    ->pluck('age', 'name);

// will be equal to
// ['Alfred' => 40, 'Mark' => 40, 'Lue' => 45, 'Ameli' => 38, 'Barb' => 38] 

当然,您可以结合orderBy和pluck。您还可以在类构造函数中指定
CASE_SENSITIVE 和/或 PRESERVE_KEYS
标志。请参阅单元测试以获取详细示例。

要按某个字段对记录进行分组,请调用groupBy()方法。
注意:groupBy之后应跟随get()或first()或value()调用。您不能对已经分组的记录集进行排序或分组。

$result = (new ArrayRecordset($this->nameAge))
    ->groupBy('age')
    ->get();

// will be equal to
// [
//     38 => [
//         ['name' => 'Ameli',  'age' => 38],
//         ['name' => 'Barb',   'age' => 38],
//     ],
//     40 => [
//         ['name' => 'Alfred', 'age' => 40],
//         ['name' => 'Mark',   'age' => 40],
//     ],
//     45 => [
//         ['name' => 'Lue',    'age' => 45],
//     ],
// ]

高级示例

您可以使用任意的比较函数,并将其与其他函数混合使用。

$comparator = function ($a, $b) {
    return strlen($a['name']) - strlen($b['name']);
};
$result = (new ArrayRecordset($record))
    ->orderBy('age')
    ->orderByCallable($comparator)
    ->get();

// will be equal to
// [
//     ['name' => 'Barb',   'age' => 38],
//     ['name' => 'Ameli',  'age' => 38],
// 
//     ['name' => 'Mark',   'age' => 40],
//     ['name' => 'Alfred', 'age' => 40],
//     
//     ['name' => 'Lue',    'age' => 45],
// ]

您可以通过自己的宏扩展此类。感谢令人惊叹的 spatie trait

ArrayRecordset::macro(
    'orderByKeepOnTop',
    function (string $field, string $onTop, string $direction = 'asc') {
        $sign = strtolower($direction) === 'asc' ? 1 : -1;
        $this->comparators[] = function ($a, $b) use ($field, $onTop, $sign) {
            $r = ($b[$field] == $onTop) <=> ($a[$field] == $onTop);
            return $r == 0 ? strnatcasecmp($a[$field], $b[$field]) * $sign : $r;
        };
        return $this;
    }
);

// Whenever you sort by ascending or descending order, 
// Barb will be on top of the list!
$result = (new ArrayRecordset($this->nameAge))
    ->orderByKeepOnTop('name', 'Barb', 'asc')
    ->get();

// will be equal to 
// [
//     ['name' => 'Barb',   'age' => 38],
//     ['name' => 'Alfred', 'age' => 40],
//     ['name' => 'Ameli',  'age' => 38],
//     ['name' => 'Lue',    'age' => 45],
//     ['name' => 'Mark',   'age' => 40],
// ]

贡献

该项目是开源软件。欢迎提交问题报告和PR。

许可证

该项目是开源软件,许可协议为MIT许可证