Skip to content

数组

Testing Is Documentation

tests/Support/ArrTest.php

这里为系统提供的数组使用的功能文档说明。

Uses

php
<?php

use Leevel\Kernel\Utils\Api;
use Leevel\Support\Arr;
use Leevel\Support\IArray;
use Leevel\Support\IJson;

normalize 基础格式化

php
public function testBaseUse(): void
{
    self::assertTrue(Arr::normalize(true));
    self::assertSame(['a', 'b'], Arr::normalize('a,b'));
    self::assertSame(['a', 'b'], Arr::normalize(['a', 'b']));
    self::assertSame(['a'], Arr::normalize(['a', '']));
    self::assertSame(['a'], Arr::normalize(['a', ''], ',', true));
    self::assertSame(['a', ' 0 '], Arr::normalize(['a', ' 0 '], ',', true));
    self::assertSame(['a', '0'], Arr::normalize(['a', ' 0 '], ','));
}

normalize 格式化字符串

php
public function testNormalize(): void
{
    $result = Arr::normalize('hello');

    $json = <<<'eot'
        [
            "hello"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

normalize 格式化分隔字符串

php
public function testNormalizeSplitString(): void
{
    $result = Arr::normalize('hello,world');

    $json = <<<'eot'
        [
            "hello",
            "world"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

normalize 格式化数组

php
public function testNormalizeArr(): void
{
    $result = Arr::normalize(['hello', 'world']);

    $json = <<<'eot'
        [
            "hello",
            "world"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

normalize 格式化数组过滤空格

php
public function testNormalizeArrFilterEmpty(): void
{
    $result = Arr::normalize(['hello', 'world', ' ', '0']);

    $json = <<<'eot'
        [
            "hello",
            "world"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

normalize 格式化数组不过滤空格

php
public function testNormalizeArrNotFilterEmpty(): void
{
    $result = Arr::normalize(['hello', 'world', ' ', '0'], ',', true);

    $json = <<<'eot'
        [
            "hello",
            "world",
            " "
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

normalize 格式化数据即不是数组也不是字符串

php
public function testNormalizeNotArrAndNotString(): void
{
    $result = Arr::normalize(false);

    self::assertFalse($result);
}

only 允许特定 Key 通过

相当于白名单。

php
public function testOnly(): void
{
    $result = Arr::only(['input' => 'test', 'foo' => 'bar', 'hello' => 'world'], ['input', 'hello', 'notfound']);

    $json = <<<'eot'
        {
            "input": "test",
            "hello": "world"
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

except 排除特定 Key 通过

相当于黑名单。

php
public function testExcept(): void
{
    $result = Arr::except(['input' => 'test', 'foo' => 'bar', 'hello' => 'world'], ['input', 'hello', 'notfound']);

    $json = <<<'eot'
        {
            "foo": "bar"
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

filter 数据过滤

基本的字符串会执行一次清理工作。

php
public function testFilter(): void
{
    $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    $rule = [];

    $result = Arr::filter($sourceData, $rule);

    $json = <<<'eot'
        {
            "foo": "bar",
            "hello": "world",
            "i": "5"
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

filter 数据过滤待规则

php
public function testFilterWithRule(): void
{
    $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    $rule = [
        'i' => ['intval'],
        'foo' => ['md5'],
        'bar' => [static function ($v) {
            return $v.' php';
        }],
    ];

    $result = Arr::filter($sourceData, $rule);

    $json = <<<'eot'
        {
            "foo": "37b51d194a7513e45b56f6524f2d51f2",
            "hello": "world",
            "i": 5
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

filter 数据过滤待规则必须是数组

php
public function testFilterRuleIsNotArr(): void
{
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage(
        'Rule of `i` must be an array.'
    );

    $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    $rule = [
        'i' => 'intval',
    ];

    Arr::filter($sourceData, $rule);
}

filter 数据过滤待规则不是一个回调

php
public function testFilterRuleItemIsNotACallback(): void
{
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage(
        'Rule item of `i` must be a callback type.'
    );

    $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    $rule = [
        'i' => ['notcallback'],
    ];

    Arr::filter($sourceData, $rule);
}

filter 数据过滤默认不处理 NULL 值

php
public function testFilterWithoutMust(): void
{
    $sourceData = ['foo' => null];
    $rule = ['foo' => ['intval']];

    $result = Arr::filter($sourceData, $rule);

    $json = <<<'eot'
        {
            "foo": null
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

filter 数据过滤强制处理 NULL 值

php
public function testFilterWithMust(): void
{
    $sourceData = ['foo' => null];
    $rule = ['foo' => ['intval', 'must']];

    $result = Arr::filter($sourceData, $rule);

    $json = <<<'eot'
        {
            "foo": 0
        }
        eot;

    self::assertSame(
        $json,
        $this->varJson(
            $result
        )
    );
}

shouldJson 数据过滤强制处理 NULL 值

测试实现了 \Leevel\Support\IArray 的对象

php
namespace Tests\Support;

class ArrMyArray implements IArray
{
    public function toArray(): array
    {
        return ['hello' => 'IArray'];
    }
}

测试实现了 \Leevel\Support\IJson 的对象

php
namespace Tests\Support;

class ArrMyJson implements IJson
{
    public function toJson(?int $config = null): string
    {
        if (null === $config) {
            $config = JSON_UNESCAPED_UNICODE;
        }

        return json_encode(['hello' => 'IJson'], $config);
    }
}

测试实现了 \JsonSerializable 的对象

php
namespace Tests\Support;

class ArrMyJsonSerializable implements \JsonSerializable
{
    public function jsonSerialize(): mixed
    {
        return ['hello' => 'JsonSerializable'];
    }
}
php
public function testShouldJson(): void
{
    self::assertTrue(Arr::shouldJson(['foo' => 'bar']));
    self::assertTrue(Arr::shouldJson(new ArrMyArray()));
    self::assertTrue(Arr::shouldJson(new ArrMyJson()));
    self::assertTrue(Arr::shouldJson(new ArrMyJsonSerializable()));
}

convertJson 转换 JSON 数据

php
public function testConvertJson(): void
{
    self::assertSame('{"foo":"bar"}', Arr::convertJson(['foo' => 'bar']));
    self::assertSame('{"foo":"bar"}', Arr::convertJson(['foo' => 'bar'], JSON_THROW_ON_ERROR));
    self::assertSame('{"hello":"IArray"}', Arr::convertJson(new ArrMyArray()));
    self::assertSame('{"hello":"IJson"}', Arr::convertJson(new ArrMyJson()));
    self::assertSame('{"hello":"JsonSerializable"}', Arr::convertJson(new ArrMyJsonSerializable()));
    self::assertSame('{"成":"都"}', Arr::convertJson(['成' => '都']));
    self::assertSame('{"\u6210":"\u90fd"}', Arr::convertJson(['成' => '都'], 0));
}

inCondition 数据库 IN 查询条件

php
public function testInCondition(): void
{
    $data = [
        ['id' => 5, 'name' => 'hello'],
        ['id' => 6, 'name' => 'world'],
    ];

    $dataDemo2 = [
        [10, 'hello'],
        [11, 'world'],
    ];

    $result = Arr::inCondition($data, 'id');
    $json = <<<'eot'
        [
            5,
            6
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson($result)
    );

    $result = Arr::inCondition($data, 'name');
    $json = <<<'eot'
        [
            "hello",
            "world"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson($result)
    );

    $result = Arr::inCondition($dataDemo2, 0);
    $json = <<<'eot'
        [
            10,
            11
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson($result)
    );
}

inCondition 数据库 IN 查询条件支持过滤器

php
public function testInConditionWithFilter(): void
{
    $data = [
        ['id' => 5, 'name' => 5],
        ['id' => '9', 'name' => 'world'],
        ['id' => 'haha', 'name' => '0'],
    ];

    $result = Arr::inCondition($data, 'id', static fn ($v): int => (int) $v);
    $json = <<<'eot'
        [
            5,
            9,
            0
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson($result)
    );

    $result = Arr::inCondition($data, 'name', static fn ($v): string => (string) $v);
    $json = <<<'eot'
        [
            "5",
            "world",
            "0"
        ]
        eot;

    self::assertSame(
        $json,
        $this->varJson($result)
    );
}