gmazzap/pentothal

高阶函数谓词。

1.1.0 2016-01-15 19:21 UTC

This package is auto-updated.

Last update: 2024-08-26 14:10:25 UTC


README

travis-ci status codecov.io license release

高阶谓词库。

是什么?

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

一个“函数谓词”是一个接收一个或多个参数(主题)并返回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(),它返回一个谓词,用于验证非空值。

另一个函数是combine(),它接受任意数量的谓词并返回一个谓词,当所有组合的谓词都返回真时,它返回真。

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

use Pentothal as P;

$strings = array_filter($data, P\combine(P\isType('string'), P\isNotEmpty()));

这个库中的所有函数都在Pentothal命名空间中。

安装

由Composer通过gmazzap/pentothal提供。

函数列表

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

通用

  • always() 返回一个始终返回true的谓词
  • never() 返回一个始终返回false的谓词
  • isEmpty()
  • isNotEmpty()

比较

  • isSame($value)
  • isNotSame($value)
  • isEqual($value)
  • isNotEqual($value)
  • match(string $regex)
  • notMatch(string $regex)

类型检查

  • isType(string $type) 与标量类型、类和接口一起工作
  • isNotType(string $type)
  • isInt()
  • isNotInt()
  • isFloat()
  • isNotFloat()
  • isNumber()
  • isNotNumber()
  • isString()
  • isNotString()
  • isBool()
  • isNotBool()
  • isNull()
  • isNotNull()
  • isObject()
  • isNotObject()
  • isArray()
  • isNotArray()

变量过滤检查

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

大小检查

  • size(int $size) 验证数组、可计数对象和字符串的元素计数
  • sizeMax(int $size)
  • sizeMaxStrict(int $size)
  • sizeMin(int $size)
  • sizeMinStrict(int $size)
  • between(int $min, int $max)
  • notBetween(int $min, int $max)
  • betweenInner(int $min, int $max)
  • notBetweenInner(int $min, int $max)
  • betweenLeft(int $min, int $max)
  • notBetweenLeft(int $min, int $max)
  • betweenRight(int $min, int $max)
  • notBetweenRight(int $min, int $max)

元素检查(用于数组和字符串)

  • contain($item) 验证$item是否存在于数组中,或者如果$item和主题都是字符串,则检查主题是否包含$item
  • notContain($item)
  • startWith($item) 验证数组的第一个元素,或者如果$item和主题都是字符串,则检查主题字符串是否以$item开头
  • notStartWith($item) 验证数组的第一个元素,或者如果$item和主题都是字符串,则检查主题字符串是否以$item开头
  • endWith($item)
  • notEndWith($item)

仅对数组进行元素检查

  • anyOfValues(array $values) 验证数组主题是否与给定的值相交
  • notAnyOfValues(array $values)
  • anyOf(...$values) 类似 anyOfValues 但带有可变参数
  • notAnyOf(...$values)

对数组和对象进行元素检查

  • hasValue($value) 返回一个谓词,该谓词检查数组或可遍历的对象以查找等于给定值的元素
  • hasNotValue($value)
  • hasValues(array $value)
  • hasNotValues(array $values)
  • hasAnyOfValues(array $values)
  • hasNotAnyOfValues(array $values)

对象属性检查(与关联数组一起工作)

  • hasKey(string $key)
  • hasKeys(array $keys)
  • hasNotKeys(array $keys)
  • hasAnyOfKeys(array $keys)
  • hasNotAnyOfKeys(array $keys)
  • keyIs(string $key, $value)
  • keyIsNot(string $key, $value)
  • keyIsAnyOf(string $key, array values)
  • keyIsNotAnyOf(string $key, array $values)
  • keyIsType(string $key, string $type)
  • keyInNotType(string $key, string $type)
  • keyApply(string $key, callable $predicate) 返回一个谓词,该谓词将谓词应用于主题的键

对象方法检查

  • hasMethod(string $method)
  • hasNotMethod(string $method)
  • methodReturn(string $method, $value, array $methodArgs = [])
  • methodNotReturn(string $method, $value, array $methodArgs = [])
  • methodReturnAnyOf(string $method, array $values, array $methodArgs = [])
  • methodNotReturnAnyOf(string $method, array $values, array $methodArgs = [])
  • methodReturnType(string $method, string $type, array $methodArgs = [])
  • methodNotReturnType(string $method, string $type, array $methodArgs = [])
  • methodReturnEmpty(string $method, array $methodArgs = [])
  • methodReturnNotEmpty(string $method, array $methodArgs = [])
  • methodReturnApply(string $method, callable $predicate, array $methodArgs = []) 返回一个谓词,该谓词应用于主题给定方法的谓词返回值

批量处理

  • bulk(callable $predicate) 返回一个谓词,该谓词将相同的谓词应用于值数组,并且当给定的谓词对所有值返回 true 时返回 true
  • bulkPool(callable $predicate) 类似 bulk() 但返回的谓词在给定谓词对任何值返回 true 时返回 true

谓词组合

  • negate(callable $predicate) 返回一个谓词,当给定谓词返回 false 时返回 true,反之亦然
  • combine(...$predicates) 返回一个谓词,当所有给定的谓词都返回 true 时返回 true
  • pool(...$predicates) 返回一个谓词,当任何给定的谓词返回 true 时返回 true
  • combineCallbacks(array $predicates) 类似 combine() 但接受谓词数组
  • poolCallbacks(...$predicates) 类似 pool() 但接受谓词数组
  • combineMap(array $predicates) 接受谓词映射并返回一个谓词,该谓词应用于映射值,当所有谓词都返回 true 时返回 true
  • poolMap(array $predicates) 类似 combineMap,但返回的谓词在任何谓词返回 true 时返回 true

在应用谓词之前转换值

  • applyAfter(callable $transformation, callable $predicate) 返回一个谓词,它返回谓词的结果,在将输入值应用于由 $transformation 函数转换之后。

示例

use Pentothal as P;

$data = ['yes', 'no', 'Yes', 'No', 'YES', 'NO' ];

$yes = array_filter($data, P\applyAfter('strtolower', isSame('yes'))); // ['yes', 'Yes', 'YES']

相当复杂的示例

use Pentothal as P;

// some example data
$countableOne = new \ArrayObject(['foo' => 'bar']);
$countableTwo = new \ArrayObject(['a' => 'a', 'b' => 'b']);
$plainObj = new \stdClass();
$string1 = 'a';
$string3 = 'abc';
$number1 = 1;
$number3 = 3;

$list = [
  'a' => $countableOne,
  'b' => $countableTwo,
  'c' => $plainObj,
  'd' => $string1,
  'e' => $string3,
  'f' => $number1,
  'g' => $number3
];


$predicate = P\combine(
  P\pool(P\isNotObject(), P\isType('Countable')), // filter out: ['c' => $plainObj]
  P\pool(P\isNotString(), P\size(3)), // filter out: ['d' => $string1]
  P\pool(P\isString(), P\size(1)) // filter out: ['a' => $countableTwo, 'g' => $number3]
);

$negatePredicate = negate($predicate);

$in = array_filter($list, $predicate);
$out = array_filter($list, $negatePredicate);

var_dump($in); // array('a' => $countableOne, 'e' => $string3, 'f' => $number1];
var_dump($out); // array('b' => $countableTwo, 'c' => $plainObj, 'd' => $string1, 'g' => $number3];