digitalrevolution / utils
一个用于日常使用的实用类库
v1.20.0
2024-08-01 12:24 UTC
Requires
- php: >=8.1
Requires (Dev)
- digitalrevolution/phpunit-file-coverage-inspection: ^v2.0.0
- mikey179/vfsstream: ^1.6.11
- nikic/php-parser: ^4.10
- phpmd/phpmd: ^2.13
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.2
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-01 12:40:59 UTC
README
DR实用类
一个包含日常使用类和方法的库
数组
first
- 获取数组的第一个元素,否则抛出异常。firstOrNull
- 获取数组的第一个元素,否则返回null。last
- 获取数组的最后一个元素,否则抛出异常。lastOrNull
- 获取数组的最后一个元素,否则返回null。find
- 在数组中找到第一个匹配谓词的元素,否则抛出异常。findOrNull
- 在数组中找到第一个匹配谓词的元素,否则返回null。flatten
- 递归地展平一个数组,返回包含所有元素的数组。contains
- 测试给定值是否包含在项中。支持EquatableInterface
。diff
- 获取两个数组之间的差异。支持ComparableInterface
。equals
- 测试两个数组内容是否相等,忽略顺序。支持ComparableInterface
。explode
- 将字符串或null展开成数组,展开空字符串为空数组。map
- 与array_map
类似,但支持iterable
,并在回调中接收键作为第二个参数。mapAssoc
- 使用回调将数组映射到新的数组中,保留键。reindex
- 根据回调的结果重新索引数组。groupBy
- 根据回调的返回值分组项。remove
- 根据回调从数组中移除元素。支持EquatableInterface
。removeKey
- 根据键从数组中移除元素。removeKeys
- 根据键从数组中移除多个元素。removeTypes
- 通过移除所有与提供的类型匹配的值来过滤数组。removeNull
- 通过移除所有null值来过滤数组。search
- 在数组中找到元素的键或否则返回false。支持EquatableInterface
。unique
- 从数组中移除重复值。支持EquatableInterface
。wrap
- 将值包裹在数组中,除非它已经是数组。toJson
- 将数组转换为json字符串。fromJson
- 将json字符串转换为数组。
断言
流畅的断言方法,受 webmozart/assert
启发
null
notNull
isArray
inArray
isCallable
resource
object
scalar
integer
numeric
float
string
classString
startsWith
notStartsWith
endsWith
notEndsWith
boolean
true
false
notFalse
nonEmptyArray
nonEmptyString
isInstanceOf
type
fileExists
file
directory
readable
writable
示例
$value = Assert::notNull($this->repository->find(123));
Stringify
::value(mixed $value): string
轻松将任何值转换为字符串表示形式。
true > 'true' false > 'false' null > 'null' 123 > '123' 1.23 > '1.23' 'abc' > 'abc' '' > 'empty-string', [] > 'empty-array' [1, 2, 3] > 'array-list(3)' ['foo' => 'bar'] => 'keyed-array(1)' StringableObject => 'value (StringableObject)' stdClass => 'stdClass' resource => 'resource (stream)'
安装
composer require digitalrevolution/utils
关于我们
在123inkt(Digital Revolution B.V.的一部分),每天有超过50名开发专业人士正在努力改进我们的内部ERP系统以及我们的几个商店。您想加入我们吗?我们正在寻找开发者。