frayzz / sanitizer

验证传入数据的库。来自 arbuz.kz 的测试任务。

dev-main 2023-11-21 06:00 UTC

This package is not auto-updated.

Last update: 2024-09-21 07:35:33 UTC


README

验证传入数据的库。

为了安装,执行以下命令集

composer require frayzz/sanitizer:dev-main

连接库

require_once "vendor/autoload.php";
use Sanitizer\Sanitizer\Sanitizer;

使用示例 代码

$jsonGetData = '{"foo": "1223", "bar": "asd", "baz": "+7 (707) 288-56-21", "qux": "1.7", "nested_array": [1, 5], "structure": { "key1": "55", "key2": "test" } }';
$specification = [
    'foo' => [
        'type' => 'integer',
        'element' => 'integer'
    ],
    'bar' => [
        'type' => 'string',
        'element' => 'string'
    ],
    'baz' => [
        'type' => 'phoneNumber',
        'element' => 'phoneNumber'
    ],
    'qux' => [
        'type' => 'float',
        'element' => 'float'
    ],
    'nested_array' => [
        'type' => 'array',
        'element' => 'integer'
    ],
    'structure' => [
        'type' => 'structure',
        'element' => [
            'key1' => 'integer',
            'key2' => 'string',
        ]
    ],
];

$jsonEncodeData = json_decode($jsonGetData, true);
$sanitize = new Sanitizer($specification);
$dataValue = $sanitize->validate($jsonEncodeData);

echo '<pre>';
print_r($dataValue);
echo '</pre>';

响应

Array
(
    [errors] => Array
        (
            [key1] => Array
                (
                    [enterVal] => Array
                        (
                            [key1] => 55
                            [key2] => test
                        )

                    [errorType] => Массив не является одним фиксированным типом
                    [type] => string
                )

        )

)