urmaul / rich-types
另一个丰富的类型库。
0.3.4
2018-02-06 07:47 UTC
Requires
- php: >=5.4
- ramsey/array_column: ~1.1.3
Requires (Dev)
- phpunit/phpunit: ~4.0
README
另一个丰富的类型库。设计用于与基本PHP类型使用流畅接口。
没有 代码注释不足,没有测试, 没有计划发布。只有当你足够 勇敢 时才使用。
由于主版本为0,次要版本更改意味着向后不兼容的更改。因此,您应该像这样使用composer安装它
php composer.phar require urmaul/rich-types '0.3.*'
示例
use rich\collections\Strings; $words = Strings::split(' ', $text)->lower()->unique()->values();
支持的类型
集合
变量
基本集合类。可以传递给 foreach
和 count()
。
- static from($array) - 创建新的集合实例。
- map($callback) - 对集合中的每个项目执行
array_map
。 - indexBy($callback) - 使用回调将值映射到键。
- mapKeys(array $replaces, $strict = true) - 使用映射数组替换集合键。
- filter($callback = null) - 对集合中的每个项目执行
array_filter
。 - diff($array) - 使用
array_diff
从集合中删除 $array 中存在的所有项目。 - unique() - 对集合执行
array_unique
。 - merge($array) - 将另一个数组合并到集合中。
- find($callback) - 通过回调函数查找项目。返回其值。
- reduce($callback, $initial = null) - 对集合执行
array_reduce
。返回结果值。 - count() - 返回项目数量。
- value() - 返回保留键的集合数据。
- values() - 以数组形式返回集合项目,按数字索引。
- asVariables() - 将集合转换为
Variables
。可用于克隆集合。 - asNumbers() - 将集合转换为
Numbers
。可用于克隆集合。 - asStrings() - 将集合转换为
Strings
。可用于克隆集合。 - asObjects() - 将集合转换为
Objects
。可用于克隆集合。
字符串
- static split($delimiter, $string) - 通过类似于
explode
的方式分割文本来创建新的集合实例。 - static splitList($string) - 通过逗号分隔值来创建新的集合实例。
- lower() - 将集合中的每个字符串转换为小写。
- trim() - 对集合中的每个项目执行
trim
。 - replace($search, $replace) - 对集合中的每个项目执行
str_replace
。
数字
- sum() - 返回集合项的总和。
对象
- property($property) - 创建新的集合,从对象属性中选择项目。
数组
-
column($columnKey, $indexKey = null) - 返回数组中单个列的值,由列_key 标识。可选地,您还可以提供一个 index_key 来根据输入数组中索引_key 列的值对返回数组中的值进行索引。
对集合执行 array_column。如果PHP版本低于5.5,则使用 ramsey/array_column。
-
indexByColumn($columnKey) - 使用数组中单个列的值作为集合键。