danielkellyio/value-comparison

比较一个值与另一个值,以便对用户输入执行高级逻辑(比较如大于、以...开头、包含以及任意或全部以比较多个)

1.0.0 2019-10-14 19:32 UTC

This package is auto-updated.

Last update: 2024-09-15 06:56:46 UTC


README

比较一个值与另一个值,以便对用户输入执行高级逻辑(比较如大于、以...开头、包含以及任意或全部以比较多个)

为什么需要值比较库?

值比较很简单,为什么要有库呢?我的用例是从用户输入(一个HTML选择字段)中获取运算符,因此

  1. 将运算符以单词格式表示解决了两个问题
    • 不能有动态运算符
    • 提供了非编码友好的术语
  2. 能够传递"任意"或"全部"(同样,从用户输入中用逗号分隔似乎是个不二选择)
  3. 完整的测试覆盖率,以确保它每次都能正常工作
  4. 这使得ValueComparison非常适合处理这种用户界面的输入:screenshot

安装

composer require danielkellyio/value-comparison

示例

use DanielKellyIO\ValueComparison\Compare;

// Check if string contains another string
Compare::value('Value to test')->contains('test'); // true

//Check if string contains ANY other strings (pass an array or string with values comma seperated)
Compare::value('Value to test')->contains('test, not, there', 'any'); // true
//or
Compare::value('Value to test')->contains(['test', 'not', 'there'], 'any'); // true

//Check if string contains ALL other strings (pass an array or string with values comma seperated)
Compare::value('Value to test')->contains('test, not, there', 'all'); // false
//or
Compare::value('Value to test')->contains(['test', 'not', 'there'], 'all'); // false

方法

  • is - 检查值是否与比较值完全匹配(强制转换为字符串)
  • isNot - 检查值是否不与比较值完全匹配(强制转换为字符串)
  • greaterThan - 检查值是否大于比较值(强制转换为整数)
  • lessThan - 检查值是否小于比较值(强制转换为整数)
  • contains - 检查值是否包含比较值(强制转换为字符串)
  • excludes - 检查值是否排除比较值(强制转换为字符串)
  • startsWith - 检查值是否以比较值开头(强制转换为字符串)
  • endsWith - 检查值是否以比较值结尾(强制转换为字符串)

作用域

  • null - 将比较值视为一个整体来与值进行比较
  • any - 值只需匹配比较值中的一个(比较值可以是数组或逗号分隔的字符串)
  • all - 值必须匹配所有比较值(比较值可以是数组或逗号分隔的字符串)