kuria / collections
面向对象的集合结构
v2.0.0
2018-09-19 19:50 UTC
Requires
- php: >=7.1
- kuria/iterable: ^1.0
Requires (Dev)
- kuria/dev-meta: ^0.5
This package is auto-updated.
Last update: 2024-09-22 17:47:50 UTC
README
面向对象的集合结构。
内容
特性
Collection
- 带有顺序整数索引的值列表Map
- 键值映射
要求
- PHP 7.1+
集合
Collection
类实现了带有顺序整数索引的值列表。
它还实现了 Countable
、ArrayAccess
和 IteratorAggregate
。
创建一个新的集合
空集合
<?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
类实现了键值映射。
它还实现了 Countable
、ArrayAccess
和 IteratorAggregate
。
创建一个新的映射
空映射
<?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