reptily/api-check

外部API快速检查

0.0.1 2023-11-10 10:50 UTC

This package is auto-updated.

Last update: 2024-09-10 14:40:19 UTC


README

安装

composer require reptily/api-check

这个助手解决了什么问题?

Laravel门面不擅长处理大数据

大型数组示例

$users = [];
$user = [
    'id' => 1,
    'name' => 'Bob',
    'is_active' => true,
    'books' => []
];
$book = [
    'id' => 1,
    'name' => 'test book',
];

for ($i=0; $i < 100; $i++) {
    $user['books'][] = $book;
}

for ($i=0; $i < 500; $i++) {
    $users[] = $user;
}

基准测试门面验证器

Validator::make($users, [
    '*.id' => ['required', 'integer'],
    '*.name' => ['required', 'string'],
    '*.is_active' => ['required', 'boolean'],
    '*.books' => ['required', 'array'],
    '*.books.*.id' => ['required', 'integer'],
    '*.books.*.name' => ['required', 'string'],
]);
End time: 84.0274 sec.

基准测试ApiCheck

ApiCheck::structure([
    ApiCheck::TYPE_ARRAYS => [
        'id' => ApiCheck::TYPE_INTEGER,
        'name' => ApiCheck::TYPE_STRING,
        'is_active' => ApiCheck::TYPE_BOOLEAN,
        'books' => [
            ApiCheck::TYPE_ARRAYS => [
                'id' => ApiCheck::TYPE_INTEGER,
                'name' => ApiCheck::TYPE_STRING,
            ]
        ]
    ]
], $users);
End time: 0.1269 sec.

谁使用过?!

示例基本响应

{
  "id": 1,
  "name": "Bob"
}

ApiCheck

$result = ApiCheck::checker($response, [
    'id' => ApiCheck::TYPE_INTEGER,
    'name' => ApiCheck::TYPE_STRING,
]);

数组示例

["bob", "sara", "john"]

ApiCheck

$result = ApiCheck::checker($response, [ApiCheck::TYPE_ARRAYS => ApiCheck::TYPE_STRING]);

数组示例

{
  "data": [
    {
       "name": ["car", "foot", "ball"]
    },
    {
       "name": ["room", "tree"]
    }
  ]
}

ApiCheck

$result = ApiCheck::checker($response, [
    'data' => [
    ApiCheck::TYPE_ARRAYS => [
            'names' => [
                ApiCheck::TYPE_ARRAYS => ApiCheck::TYPE_STRING,
            ],
        ],
    ],
]);