kuria/

collections

面向对象的集合结构

v2.0.0 2018-09-19 19:50 UTC

This package is auto-updated.

Last update: 2024-09-22 17:47:50 UTC


README

面向对象的集合结构。

https://travis-ci.cn/kuria/collections.svg?branch=master

内容

特性

  • Collection - 带有顺序整数索引的值列表
  • Map - 键值映射

要求

  • PHP 7.1+

集合

Collection 类实现了带有顺序整数索引的值列表。

它还实现了 CountableArrayAccessIteratorAggregate

创建一个新的集合

空集合

<?php

use Kuria\Collections\Collection;

$collection = Collection::create();

使用现有的可迭代对象

<?php

use Kuria\Collections\Collection;

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

使用可变参数

<?php

use Kuria\Collections\Collection;

$collection = Collection::collect('foo', 'bar', 'baz');

集合方法概述

有关更多信息,请参阅相应方法的文档注释。

静态方法

  • create($values = null): self - 从可迭代对象创建集合
  • collect(...$values): self - 从传入的参数创建集合
  • fill($value, $count): self - 创建集合并用重复的值填充
  • explode($string, $delimiter, $limit = PHP_INT_MAX): self - 通过拆分字符串创建集合

实例方法

  • setValues($values): void - 使用给定的可迭代对象替换所有值
  • toArray(): array - 将所有值作为数组获取
  • isEmpty(): bool - 检查集合是否为空
  • count(): int - 计数值
  • has($index): bool - 检查给定的索引是否存在
  • contains($value, $strict = true): bool - 检查给定的值是否存在
  • find($value, $strict = true): ?int - 尝试找到值的第一个出现
  • get($index): mixed - 获取给定索引处的值
  • first(): mixed - 获取第一个值
  • last(): mixed - 获取最后一个值
  • indexes(): int[] - 获取所有索引
  • slice($index, $length = null): self - 提取集合的一部分
  • replace($index, $value): void - 在给定索引处替换值
  • push(...$values): void - 将一个或多个值推送到集合的末尾
  • pop(): mixed - 从集合末尾弹出值
  • unshift(...$values): void - 在集合开头前缀一个或多个值
  • shift(): mixed - 从集合开头弹出一个值
  • insert($index, ...$values): void - 在给定索引处插入一个或多个值
  • pad($length, $value): void - 使用值填充集合到指定长度
  • remove(...$indexes): void - 移除给定索引处的值
  • clear(): void - 移除所有值
  • splice($index, $length = null, $replacement = null): void - 移除或替换集合的一部分
  • sum(): int|float - 计算所有值的总和
  • product(): int|float - 计算所有值的乘积
  • implode($delimiter = ''): string - 使用分隔符连接所有值
  • reduce($callback, $initial = null): mixed - 将集合缩减为单个值
  • reverse(): self - 反转集合
  • chunk($size): self[] - 将集合分割为给定大小的块
  • split($number): self[] - 将集合分割为给定数量的块
  • unique(): self - 获取唯一值
  • shuffle(): self - 获取随机顺序的值
  • random($count): self - 从集合中获取 N 个随机值
  • column($key): self - 从所有对象或数组值中收集属性或数组索引的值
  • mapColumn($valueKey, $indexKey): Map - 使用所有对象或数组值的属性或数组索引构建映射
  • filter($filter): self - 使用给定的回调过滤值
  • apply($callback): self - 将回调应用于所有值
  • map($mapper): Map - 将集合转换为映射
  • merge(...$iterables): self - 将集合与给定的可迭代合并
  • intersect(...$iterables): self - 与给定的可迭代计算交集
  • uintersect($comparator, ...$iterables): self - 使用自定义比较器与给定的可迭代计算交集
  • diff(...$iterables): self - 计算此集合与给定可迭代之间的差异
  • udiff($comparator, ...$iterables): self - 使用自定义比较器计算此集合与给定可迭代之间的差异
  • sort($flags = SORT_REGULAR, $reverse = false): self - 对集合进行排序
  • usort($comparator): self - 使用自定义比较器对集合进行排序

注意

任何返回 self 的方法都返回一个包含所选或修改值的新的集合实例。原始集合不会被更改。

如果需要更新原始集合,请使用 setValues() 来执行,例如

<?php

$collection->setValues($collection->sort());

数组访问和迭代

Collection 实例可以像常规数组一样访问和迭代。

<?php

use Kuria\Collections\Collection;

$collection = Collection::create();

// push some values
$collection[] = 'foo';
$collection[] = 'bar';
$collection[] = 'baz';

// replace a value
$collection[1] = 'new bar';

// remove a value
unset($collection[2]);

// read values
echo 'Value at index 1 is ', $collection[1], "\n";
echo 'Value at index 2 ', isset($collection[2]) ? 'exists' : 'does not exist', "\n";

// count values
echo 'There are ', count($collection), ' values in total', "\n";

// iterate values
foreach ($collection as $index => $value) {
    echo $index, ': ', $value, "\n";
}

输出

Value at index 1 is new bar
Value at index 2 does not exist
There are 2 values in total
0: foo
1: new bar

映射

Map 类实现了键值映射。

它还实现了 CountableArrayAccessIteratorAggregate

创建一个新的映射

空映射

<?php

use Kuria\Collections\Map;

$map = Map::create();

使用现有的可迭代对象

<?php

use Kuria\Collections\Map;

$map = Map::create(['foo' => 'bar', 'bar' => 'baz']);

映射方法概述

有关更多信息,请参阅相应方法的文档注释。

静态方法

  • create($pairs = null): self - 从可迭代创建映射
  • map($iterable, $mapper): self - 使用回调映射给定可迭代的值
  • build($iterable, $mapper): self - 使用回调从可迭代构建映射
  • combine($keys, $values): self - 将键列表和值列表合并以创建映射

实例方法

  • setPairs($pairs): void - 使用给定的可迭代替换所有对
  • toArray(): array - 获取所有对作为数组
  • isEmpty(): bool - 检查映射是否为空
  • count(): int - 计数对
  • has($key): bool - 检查给定的键是否存在
  • contains($value, $strict = true): bool - 检查给定的值是否存在
  • find($value, $strict = true): string|int|null - 尝试查找值的第一次出现
  • get($key): mixed - 获取给定键的值
  • values(): Collection - 获取所有值
  • keys(): Collection - 获取所有键
  • set($key, $value): void - 定义对
  • add(...$iterables): void - 将其他可迭代的对添加到此映射中
  • fill($keys, $value): void - 用值填充特定的键
  • remove(...$keys): void - 删除具有给定键的对
  • clear(): void - 删除所有对
  • reduce($reducer, $initial = null): mixed - 将映射缩减为单个值
  • flip(): self - 交换键和值
  • shuffle(): self - 随机化对顺序
  • column($key, $indexBy = null): self - 从所有对象或数组值中收集属性或数组键的值
  • filter($filter): self - 使用给定的回调过滤对
  • apply($callback): self - 将回调应用于所有对
  • map($mapper): self - 使用给定的回调重新映射对
  • intersect(...$iterables): self - 与给定的可迭代计算交集
  • uintersect($comparator, ...$iterables): self - 使用自定义比较器与给定的可迭代计算交集
  • intersectKeys(...$iterables): self - 与给定的可迭代计算键交集
  • uintersectKeys($comparator, ...$iterables): self - 使用自定义比较器与给定的可迭代计算键交集
  • diff(...$iterables): self - 与给定的可迭代计算差异
  • udiff($comparator, ...$iterables): self - 使用自定义比较器计算此映射与给定可迭代对象之间的差异
  • diffKeys(...$iterables): self - 计算此映射与给定可迭代对象之间的键差异
  • udiffKeys($comparator, ...$iterables): self - 使用自定义比较器计算此映射与给定可迭代对象之间的键差异
  • sort($flags = SORT_REGULAR, $reverse = false): self - 使用映射的值对其进行排序
  • usort($comparator): self - 使用映射的值和自定义比较器对其进行排序
  • ksort($flags = SORT_REGULAR, $reverse = false): self - 使用映射的键对其进行排序
  • uksort(): self - 使用映射的键和自定义比较器对其进行排序

注意

任何返回 self 的方法都会返回一个新映射实例,其中包含所选或修改后的对。原始映射不会改变。

如果需要更新原始映射,请使用 setPairs() 来实现,例如:

<?php

$map->setPairs($map->sort());

数组访问和迭代

Map 实例可以像常规数组一样访问和迭代。

<?php

use Kuria\Collections\Map;

$map = Map::create();

// add some pairs
$map['foo'] = 'bar';
$map['baz'] = 'qux';
$map['quux'] = 'quuz';

// remove a pair
unset($map['baz']);

// read values
echo 'Value with key "foo" is ', $map['foo'], "\n";
echo 'Value with key "baz" ', isset($map['baz']) ? 'exists' : 'does not exist', "\n";

// count pairs
echo 'There are ', count($map), ' pairs in total', "\n";

// iterate pairs
foreach ($map as $key => $value) {
   echo $key, ': ', $value, "\n";
}

输出

Value with key "foo" is bar
Value with key "baz" does not exist
There are 2 pairs in total
foo: bar
quux: quuz