ilsenem/matcher

该包已废弃,不再维护。未建议替代包。

数组模式匹配器

v1.0.0 2016-11-21 10:43 UTC

This package is not auto-updated.

Last update: 2022-08-29 05:00:35 UTC


README

简单的数组模式匹配器。

安装

作为 Composer 依赖项安装

composer require ilsenem/matcher

在您的代码中

use Matcher\Schema;

// $schema = [];
// $data   = [];

$matcher = new Schema($schema);

if (!$matcher->match($data)) {
    print_r($matcher->getErrors());
}

用法

类型匹配

支持的值类型有

  • 布尔值
  • 整数
  • 双精度浮点数
  • 字符串
$schema = [
    'id'     => 'integer',
    'email'  => 'string',
    'active' => 'boolean',
    'rating' => 'double',
];

$matcher = new Matcher\Schema($schema);

$matcher->match([
    'id'     => 1,
    'email'  => 'some@domain.zone',
    'active' => true,
    'rating' => .5,
]);

可空值

在类型声明前添加 ? 来标记值为可空。

$schema = [
    'id'       => 'integer',
    'email'    => 'string',
    'nickname' => '?string',
];

$matcher = new Schema($schema);

$matcher->match([
    'id'       => 7,
    'email'    => 'leeroy.jenkins@wow.lol',
    'nickname' => null,
]);

跳过键

在键前添加 ? 来跳过匹配,如果键在数据中不存在。

$schema = [
    'id'        => 'integer',
    'email'     => 'string',
    '?optional' => 'boolean'
];

$matcher = new Schema($schema);

$matcher->match([
    'id'    => 18,
    'email' => 'tired.to.fake@emails.zone',
]);

组合数组

如果一个数组没有严格的模式,但遵循键和值的类型定义,你可以为它设置组合 key => value 类型

$schema = [
    'rules' => 'string => boolean',
];

$matcher = new Schema($schema);

$matcher->match([
    'rules' => [
        'admin.cp'    => true,
        'admin.users' => true,
    ],
]);

数组嵌套和集合

你可以将一个模式嵌套到另一个模式中,以匹配复杂结构并匹配数组集合

$schema = [
    '*' => [ // Many users in collection
        'id'     => 'integer',
        'email'  => 'string',
        'active' => 'boolean',
        'tokens' => [ // Nest more rules
            'activation'    => '?string',
            'authorization' => '?string',
        ],
        'role' => [
            'id'        => 'integer',
            'title'     => 'string',
            'superuser' => 'boolean',
            '?rules'    => 'string => boolean',
        ],
        '?orders' => [
            '*' => [
                'id'       => 'integer',
                'quantity' => 'integer',
                'price'    => 'double',
            ],
        ],
    ],
];

$matcher = new Schema($schema);

$matcher->match([
    [
        'id'     => 1,
        'email'  => 'admin@domain.zone',
        'active' => true,
        'tokens' => [
            'activation'    => null,
            'authorization' => '0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914',
        ],
        'role' => [
            'id'    => 1,
            'title' => 'Administrator',
        ],
    ],
    [
        'id'     => 2,
        'email'  => 'moderator@domain.zone',
        'active' => true,
        'tokens' => [
            'activation'    => null,
            'authorization' => null,
        ],
        'role' => [
            'id'    => 2,
            'title' => 'Moderator',
            'rules' => [
                'admin.cp'     => false,
                'moderator.cp' => true,
            ],
        ]
    ],
    [
        'id'     => 87,
        'email'  => 'customer@domain.zone',
        'active' => true,
        'tokens' => [
            'activation'    => null,
            'authorization' => null,
        ],
        'role' => [
            'id'    => 3,
            'title' => 'Customer',
            'rules' => [
                'admin.cp'     => false,
                'moderator.cp' => false,
            ],
        ],
        'orders' => [
            [
                'id'       => 873,
                'quantity' => 7,
                'price'    => 18.99,
            ],
            [
                'id'       => 1314,
                'quantity' => 19,
                'price'    => 1.97,
            ]
        ],
    ]
]);

错误

匹配后,通过 $matcher->getErrors() 可以获取错误数组

[
    'path.to.*.array.key' => [
        'TYPE_OF_ERROR' => 'Human readable description.',
    ],
    // ...
]

错误类型

  • Schema::ERR_COLLECTION_DEFINITION - 集合的定义必须是该级别的唯一定义。
  • Schema::ERR_KEY_NOT_FOUND - 在模式中定义的键在数据中未找到。
  • Schema::ERR_TYPE_UNKNOWN - 模式中给出的未知类型。
  • Schema::ERR_TYPE_MISMATCH - 任何类型声明中的类型不匹配。

许可证

MIT.