toobo/hop

高阶函数谓词。

2.0.0 2022-10-14 01:57 UTC

This package is auto-updated.

Last update: 2024-09-14 06:29:55 UTC


README

license Quality Assurance codecov.io release PHP version requirement

高阶谓词库。

是什么?

“高阶函数”是一个返回函数或接收函数作为参数的函数。

“函数谓词”是一个接收一个或多个参数(主题)并返回 truefalse 的函数。

此库是一系列返回函数谓词的函数集合.

为什么?

在PHP中,有一些函数如 array_maparray_filter 等,它们接收函数谓词作为参数。

例如

$data = [
  'foo',
  1,
  true,
  'bar',
  [],
  ''
];

$strings = array_filter($data, 'is_string'); // ['foo', 'bar', '']

这要归功于is_string是一个命名函数。

但如果我们需要更复杂的操作,例如我们还想要去除空字符串,我们需要

$strings = array_filter($data, function($item) {
    return is_string($item) && $item !== '';
});

此库的一个函数是isType(),它接受一个表示类型的字符串并返回一个可以用来检查主题是否为该类型的谓词。

此库的另一个函数是isNotEmpty(),它返回一个验证非空值的谓词。

另一个函数是chain(),它接收任意数量的谓词并返回一个当所有给定的谓词都返回 true 时返回 true 的谓词。

使用这三个函数,上面的代码可以写成这样

$strings = array_filter($data, Hop\chain(Hop\isType('string'), Hop\isNotEmpty()));

此库中的所有函数都在 Hop 命名空间中。

安装

通过 Composer 使用 toobo/hop 提供。

函数列表

以下是目前库提供的所有函数列表(省略命名空间)

通用

  • always() 返回一个总是返回 true 的谓词
  • never() 返回一个总是返回 false 的谓词
  • isEmpty()
  • isNotEmpty()
  • isTrueish()
  • isFalsey()
  • isBooleanLooking()

比较

  • is($value)
  • isNot($value)
  • equals($value)
  • notEquals($value)
  • matches(string $regex)
  • notMatches(string $regex)
  • moreThan(int|float $limit)
  • moreThanOrEqual(int|float $limit)
  • lessThan(int|float $limit)
  • lessThanOrEqual(int|float $limit)
  • between(int|float $min, int|float $max)
  • betweenInner(int|float $min, int|float $max)
  • betweenLeft(int|float $min, int|float $max)
  • betweenRight(int|float $min, int|float $max)

类型检查

  • isType(string $type) 与标量类型、类和接口一起工作
  • objectIs(string $classOrInterface) 与对象一起工作

变量过滤检查

  • filterVar(int $filter, $options = null) 返回一个谓词,该谓词使用给定的过滤器选项将 filter_var() 应用于主题。
  • isEmail()
  • isUrl()
  • isIp()
  • isMac()

大小检查

  • size(int $size) 验证数组、可计数的对象和字符串的长度
  • smallerThan(int $size)
  • smallerThanOrEqual(int $size)
  • biggerThan(int $size)
  • biggerThanOrEqual(int $size)
  • sizeBetween(int $min, int $max)
  • sizeBetweenInner(int $min, int $max)
  • sizeBetweenLeft(int $min, int $max)
  • sizeBetweenRight(int $min, int $max)

元素检查(针对数组和字符串)

  • contains(string $subString) 验证字符串是否包含子字符串
  • startsWith(string $subString) 验证字符串是否以子字符串开头
  • endsWith(string $subString) 验证字符串是否以子字符串结尾
  • has($item) 验证字符串是否包含项
  • headIs(...$items) 验证数组的第一个项
  • tailIs(...$items) 验证数组的最后一个项
  • in(...$items) 验证项是否为给定项之一
  • notIn(...$items) 验证项是否不是给定项之一
  • intersect(...$items) 验证数组与给定的项有非空交集
  • notIntersect(...$items) 验证数组与给定的项没有交集

数组键

  • hasKey(string $key)
  • hasNotKey(string $key)
  • hasAllKeys(string ...$keys)
  • hasAnyOfKeys(string ...$keys)
  • hasNoneOfKeys(string ...$keys)
  • hasNotAllKeys(string ...$keys)
  • valueForKeyIs(string $key, $value)
  • valueForKeyIsNot(string $key, $value)
  • applyOnValueForKey(string $key, callable $callback)

对象方法

  • hasMethod(string $method)
  • classHasMethod(string $method)
  • methodReturns(string $method, $value, ...$params)

谓词组合

  • not(callable $predicate) 取消给定谓词的值
  • chain(callable ...$predicates) 以AND模式组合谓词
  • pool(callable ...$predicates) 以OR模式组合谓词

其他

  • applyAfter(callable $transformation, callable $predicate) 返回一个谓词,它在输入值通过 $transformation 函数转换后,返回谓词的结果。
  • applyAfterMethod(string $method, callable $predicate)

许可证

Hop 是开源的,并遵循 MIT 许可证发布。有关更多信息,请参阅 LICENSE 文件。