chipslays/collection

用于操作数组作为集合的库。

1.2.0 2022-10-31 10:22 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:13 UTC


README

GitHub Workflow Status Packagist Version

简单库,用于将数组或对象作为集合操作。

ℹ 支持点表示法和通配符规则。

该库的目的是提供一种通用的类,用于与数据数组进行交互。方便地支持点表示法和星号键。

安装

$ composer require chipslays/collection

文档

可以通过辅助函数collection()创建集合。

get(string $key [, $default = null, string $separator = '.'])

使用点表示法从集合中获取项目。

use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$name = $collection->get('user.name'); // chipslays
$email = $collection->get('user.email', 'default@email.com'); // default@email.com

$array = $collection->user; // array('name' => 'chipslays')
$name = $collection->user['name']; // chipslays
$collection = collection([
    'foo' => [
        'bar' => ['baz' => 1],
        'bam' => ['baz' => 2],
        'boo' => ['baz' => 3],
    ],
]);

$collection->get('foo.*.baz');
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
// )

$collection->get('foo.*');
// Array
// (
//     [0] => Array
//         (
//             [baz] => 1
//         )
//     [1] => Array
//         (
//             [baz] => 2
//         )
//     [2] => Array
//         (
//             [baz] => 3
//         )
// )
$collection = collection([
    'foo' => [
        'bar' => ['baz' => 1],
    ],
]);

$collection->get('foo.*.baz');
// 1

$collection->get('foo.*');
// Array
// (
//     [baz] => 1
// )

set(string $key, $value = null [, string $separator = '.']): Collection

使用点表示法键设置/覆盖集合中的项目。

use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$collection->set('user.name', 'john doe');
$collection->set('user.email', 'john.doe@email.com');

$name = $collection->get('user.name'); // john doe
$email = $collection->get('user.email'); // john.doe@email.com

has(string $key [, string $separator = '.']): bool

检查使用点表示法键是否存在项目。

use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$hasName = $collection->has('user.name'); // true
$hasEmail = $collection->has('user.email'); // false

first(): mixed

从集合中返回第一个项目。

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->first(); // foo

last(): mixed

从集合中返回最后一个项目。

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->last(); // baz

shift(): mixed

获取第一个项目并将其从集合中删除。

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->shift(); // foo
echo $collection->count(); // 2

values(): Collection

返回不带键的值作为集合。

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->values()); // collection(green, apple)

keys(): Collection

返回不带值的键作为集合。

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->keys()); // collection(color, name)

only(): Collection

返回所选键。

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->only(['color'])); // collection(color => green)

push(): Collection

replace(): Collection

replaceRecursive(): Collection

merge(): Collection

mergeRecursive(): Collection

trim(): Collection

remove(...string $keys): Collection

limit(): Collection

chunk(int $size): Collection

each(callable $callback($item)): Collection

map(callable $callback($item)): Collection

mapWithKeys(callable $callback($item)): Collection

filter(callable $callback($item, $key) = null): Collection

where($key, $operator = null, $value = null): Collection

all(): array

collect(string $key, string $separator = '.')

reverse(bool $preserveKeys)

count(): int

获取集合中项目数。

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

echo $collection->count(); // 10
echo count($collection); // 10

clear(): Collection

清除集合中的所有项目。

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->clear();

toArray(): array

将集合项目作为数组获取。

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->toArray();

toObject(): object

将集合项目作为对象(stdClass)获取。

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->toObject();

__toString(): string

获取可打印的字符串。

use Chipslays\Collection\Collection;

$collection = new Collection(['one', 'two']);

echo (string) $collection;

/** output string */
Array
(
    [0] => one
    [1] => two
)

👀 另请参阅

许可证

MIT