数组

    • use Leevel\Support\Arr;

    格式化字符串

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

    格式化分隔字符串

    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. eot;
    9. $this->assertSame(
    10. $json,
    11. $this->varJson(
    12. $result
    13. )
    14. );
    15. }

    格式化数组不过滤空格

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

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

    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. }

    数据过滤

    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. "hello": "world",
    9. "i": "5"
    10. }
    11. eot;
    12. $this->assertSame(
    13. $json,
    14. $this->varJson(
    15. $result
    16. )
    17. );
    18. }

    数据过滤待规则

    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. }