cd rubin/matchmaker

超新鲜PHP匹配函数

1.0 2014-06-28 08:54 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:26:32 UTC


README

NOTE!

I've found enough issues in this library to not use it. 
It remains here for historical purposes but I
would not recommend anyone expect it to work reliably as-is.

matchmaker

PHP函数,帮助您验证复杂嵌套PHP数组的结构。

$books = [
    [
        'type' => 'book',
        'title' => 'Geography book',
        'chapters' => [
            'eu' => ['title' => 'Europe', 'interesting' => true],
            'as' => ['title' => 'America', 'interesting' => false]
        ]
    ],
    [
        'type' => 'book',
        'title' => 'Foreign languages book',
        'chapters' => [
            'de' => ['title' => 'Deutsch']
        ]
    ]
];

$pattern = [
    '*' => [
        'type' => 'book',
        'title' => ':string contains(book)',
        'chapters' => [
            ':string length(2) {1,3}' => [
                'title' => ':string',
                'interesting?' => ':bool',
            ]
        ]
    ]
];

matchmaker\matches($books, $pattern); // true

可用于检查来自不同来源(JSON、XML、Post Data)的标量值、对象或数组。

匹配规则

匹配规则是以下划线开头的字符串。您可以使用多个匹配器通过空格连接。匹配器可以是任何可调用项(函数名称或闭包)。您可以添加自己的规则或替换标准规则。

  • 通用

  • empty

  • nonempty

  • required

  • in(a, b, ...)

  • mixed

  • any

  • 类型

  • array

  • bool

  • boolean

  • callable

  • double

  • float

  • int

  • integer

  • long

  • numeric

  • number

  • object

  • real

  • resource

  • scalar

  • string

  • 数字

  • gt(n)

  • gte(n)

  • lt(n)

  • lte(n)

  • negative

  • positive

  • between(a, b)

  • 字符串

  • alnum

  • alpha

  • cntrl

  • digit

  • graph

  • lower

  • print

  • punct

  • space

  • upper

  • xdigit

  • regexp(pattern)

  • email

  • url

  • ip

  • length(n)

  • min(n)

  • max(n)

  • contains(needle)

  • starts(s)

  • ends(s)

  • json

  • date

  • 数组

  • count(n)

  • keys(key1, key2, ...)

  • 对象

  • instance(class)

  • property(name, value)

  • method(name, value)

更多详细信息请参考 此处

键的量词

  • ! - 必须有一个键(默认值)
  • ? - 可选键
      • 任意数量的键
  • {3} - 严格键的数量
  • {1,5} - 范围

对于匹配器(即 ':string'),默认量词是 *

安装

  • 使用composer将matchmaker安装到您的项目中
composer require ptrofimov/matchmaker:*

许可

版权所有 (c) 2014 Petr Trofimov

MIT许可

特此授予任何获得本软件及其相关文档副本(“软件”)的人免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供方提供软件的人这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任是基于合同、侵权或其他原因,以及与软件或其使用或其它交易有关的任何损害。