• 数组
    • 基础格式化
    • 格式化字符串
    • 格式化分隔字符串
    • 格式化数组
    • 格式化数组过滤空格
    • 格式化数组不过滤空格
    • 格式化数据即不是数组也不是字符串
    • 允许特定 Key 通过
    • 排除特定 Key 通过
    • 数据过滤
    • 数据过滤待规则
    • 数据过滤待规则必须是数组
    • 数据过滤待规则不是一个回调

    数组

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

    引入相关类

    • use Leevel\Support\Arr;

      基础格式化

    1. public function testBaseUse()
    2. {
    3. $this->assertTrue(Arr::normalize(true));
    4. $this->assertSame(['a', 'b'], Arr::normalize('a,b'));
    5. $this->assertSame(['a', 'b'], Arr::normalize(['a', 'b']));
    6. $this->assertSame(['a'], Arr::normalize(['a', '']));
    7. $this->assertSame(['a'], Arr::normalize(['a', ''], ',', true));
    8. $this->assertSame(['a', ' 0 '], Arr::normalize(['a', ' 0 '], ',', true));
    9. $this->assertSame(['a', '0'], Arr::normalize(['a', ' 0 '], ','));
    10. }

    格式化字符串

    1. public function testNormalize(): void
    2. {
    3. $result = Arr::normalize('hello');
    4. $json = <<<'eot'
    5. [
    6. "hello"
    7. ]
    8. eot;
    9. $this->assertSame(
    10. $json,
    11. $this->varJson(
    12. $result
    13. )
    14. );
    15. }

    格式化分隔字符串

    1. public function testNormalizeSplitString(): void
    2. {
    3. $result = Arr::normalize('hello,world');
    4. $json = <<<'eot'
    5. [
    6. "hello",
    7. "world"
    8. ]
    9. eot;
    10. $this->assertSame(
    11. $json,
    12. $this->varJson(
    13. $result
    14. )
    15. );
    16. }

    格式化数组

    1. public function testNormalizeArr(): void
    2. {
    3. $result = Arr::normalize(['hello', 'world']);
    4. $json = <<<'eot'
    5. [
    6. "hello",
    7. "world"
    8. ]
    9. eot;
    10. $this->assertSame(
    11. $json,
    12. $this->varJson(
    13. $result
    14. )
    15. );
    16. }

    格式化数组过滤空格

    1. public function testNormalizeArrFilterEmpty(): void
    2. {
    3. $result = Arr::normalize(['hello', 'world', ' ', '0']);
    4. $json = <<<'eot'
    5. [
    6. "hello",
    7. "world"
    8. ]
    9. eot;
    10. $this->assertSame(
    11. $json,
    12. $this->varJson(
    13. $result
    14. )
    15. );
    16. }

    格式化数组不过滤空格

    1. public function testNormalizeArrNotFilterEmpty(): void
    2. {
    3. $result = Arr::normalize(['hello', 'world', ' ', '0'], ',', true);
    4. $json = <<<'eot'
    5. [
    6. "hello",
    7. "world",
    8. " "
    9. ]
    10. eot;
    11. $this->assertSame(
    12. $json,
    13. $this->varJson(
    14. $result
    15. )
    16. );
    17. }

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

    1. public function testNormalizeNotArrAndNotString(): void
    2. {
    3. $result = Arr::normalize(false);
    4. $this->assertFalse($result);
    5. }

    允许特定 Key 通过

    相当于白名单。

    1. public function testOnly(): void
    2. {
    3. $result = Arr::only(['input' => 'test', 'foo' => 'bar', 'hello' => 'world'], ['input', 'hello', 'notfound']);
    4. $json = <<<'eot'
    5. {
    6. "input": "test",
    7. "hello": "world",
    8. "notfound": null
    9. }
    10. eot;
    11. $this->assertSame(
    12. $json,
    13. $this->varJson(
    14. $result
    15. )
    16. );
    17. }

    排除特定 Key 通过

    相当于黑名单。

    1. public function testExcept(): void
    2. {
    3. $result = Arr::except(['input' => 'test', 'foo' => 'bar', 'hello' => 'world'], ['input', 'hello', 'notfound']);
    4. $json = <<<'eot'
    5. {
    6. "foo": "bar"
    7. }
    8. eot;
    9. $this->assertSame(
    10. $json,
    11. $this->varJson(
    12. $result
    13. )
    14. );
    15. }

    数据过滤

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

    1. public function testFilter(): void
    2. {
    3. $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    4. $rule = [];
    5. $result = Arr::filter($sourceData, $rule);
    6. $json = <<<'eot'
    7. {
    8. "foo": "bar",
    9. "hello": "world",
    10. "i": "5"
    11. }
    12. eot;
    13. $this->assertSame(
    14. $json,
    15. $this->varJson(
    16. $result
    17. )
    18. );
    19. }

    数据过滤待规则

    1. public function testFilterWithRule(): void
    2. {
    3. $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    4. $rule = [
    5. 'i' => ['intval'],
    6. 'foo' => ['md5'],
    7. 'bar' => [function ($v) {
    8. return $v.' php';
    9. }],
    10. ];
    11. $result = Arr::filter($sourceData, $rule);
    12. $json = <<<'eot'
    13. {
    14. "foo": "37b51d194a7513e45b56f6524f2d51f2",
    15. "hello": "world",
    16. "i": 5
    17. }
    18. eot;
    19. $this->assertSame(
    20. $json,
    21. $this->varJson(
    22. $result
    23. )
    24. );
    25. }

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

    1. public function testFilterRuleIsNotArr(): void
    2. {
    3. $this->expectException(\InvalidArgumentException::class);
    4. $this->expectExceptionMessage(
    5. 'Rule of `i` must be an array.'
    6. );
    7. $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    8. $rule = [
    9. 'i' => 'intval',
    10. ];
    11. Arr::filter($sourceData, $rule);
    12. }

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

    1. public function testFilterRuleItemIsNotACallback(): void
    2. {
    3. $this->expectException(\InvalidArgumentException::class);
    4. $this->expectExceptionMessage(
    5. 'Rule item of `i` must be a callback type.'
    6. );
    7. $sourceData = ['foo' => 'bar', 'hello' => 'world ', 'i' => '5'];
    8. $rule = [
    9. 'i' => ['notcallback'],
    10. ];
    11. Arr::filter($sourceData, $rule);
    12. }