Skip to content

验证器.验证是否为数组

Uses

php
<?php

use Leevel\Kernel\Utils\Api;
use Leevel\Validate\Validator;

验证通过的数据

以下是通过的校验数据示例。

php
# Tests\Validate\Validator\IsArrayTest::baseUseProvider
public static function baseUseProvider(): array
{
    return [
        [['this', 'is', 'an array']],
        [['a' => 'aaa', 'b' => 1, 'c' => true]],
        [['im', 'an', 'array']],
        [['im' => 'not', 'going' => 'to be', 'an' => 'array']],
    ];
}

上面的数据是测试的数据提供者。

php
public function testBaseUse($value): void
{
    $validate = new Validator(
        [
            'name' => $value,
        ],
        [
            'name' => 'is_array',
        ]
    );

    static::assertTrue($validate->success());
}

未验证通过的数据

以下是未通过的校验数据示例。

php
# Tests\Validate\Validator\IsArrayTest::badProvider
public static function badProvider(): array
{
    return [
        ['this is a string'],
        [0.52148389816284],
        ['0.0'],
        ['-0.0'],
        ['foo'],
        ['bar'],
        ['hello'],
        ['world'],
    ];
}

上面的数据是测试的数据提供者。

php
public function testBad($value): void
{
    $validate = new Validator(
        [
            'name' => $value,
        ],
        [
            'name' => 'is_array',
        ]
    );

    static::assertFalse($validate->success());
}