数组

Testing Is Documentation

tests/Support/ArrTest.php数组 - 图1

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

Uses

  1. <?php
  2. use Exception;
  3. use JsonSerializable;
  4. use Leevel\Support\Arr;
  5. use Leevel\Support\IArray;
  6. use Leevel\Support\IJson;

normalize 基础格式化

  1. public function testBaseUse(): void
  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. }

normalize 格式化字符串

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

normalize 格式化分隔字符串

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

normalize 格式化数组

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

normalize 格式化数组过滤空格

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

normalize 格式化数组不过滤空格

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

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

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

only 允许特定 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. }

except 排除特定 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. }

filter 数据过滤

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

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

filter 数据过滤待规则

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

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

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

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

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

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

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

filter 数据过滤强制处理 NULL 值

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

shouldJson 数据过滤强制处理 NULL 值

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

  1. namespace Tests\Support;
  2. class ArrMyArray implements IArray
  3. {
  4. public function toArray(): array
  5. {
  6. return ['hello' => 'IArray'];
  7. }
  8. }

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

  1. namespace Tests\Support;
  2. class ArrMyJson implements IJson
  3. {
  4. public function toJson(?int $option = null): string
  5. {
  6. if (null === $option) {
  7. $option = JSON_UNESCAPED_UNICODE;
  8. }
  9. return json_encode(['hello' => 'IJson'], $option);
  10. }
  11. }

测试实现了 \JsonSerializable 的对象

  1. namespace Tests\Support;
  2. class ArrMyJsonSerializable implements JsonSerializable
  3. {
  4. public function jsonSerialize()
  5. {
  6. return ['hello' => 'JsonSerializable'];
  7. }
  8. }
  1. public function testShouldJson(): void
  2. {
  3. $this->assertTrue(Arr::shouldJson(['foo' => 'bar']));
  4. $this->assertTrue(Arr::shouldJson(new ArrMyArray()));
  5. $this->assertTrue(Arr::shouldJson(new ArrMyJson()));
  6. $this->assertTrue(Arr::shouldJson(new ArrMyJsonSerializable()));
  7. }

convertJson 转换 JSON 数据

  1. public function testConvertJson(): void
  2. {
  3. $this->assertSame('{"foo":"bar"}', Arr::convertJson(['foo' => 'bar']));
  4. $this->assertSame('{"foo":"bar"}', Arr::convertJson(['foo' => 'bar'], JSON_THROW_ON_ERROR));
  5. $this->assertSame('{"hello":"IArray"}', Arr::convertJson(new ArrMyArray()));
  6. $this->assertSame('{"hello":"IJson"}', Arr::convertJson(new ArrMyJson()));
  7. $this->assertSame('{"hello":"JsonSerializable"}', Arr::convertJson(new ArrMyJsonSerializable()));
  8. $this->assertSame('{"成":"都"}', Arr::convertJson(['成' => '都']));
  9. $this->assertSame('{"\u6210":"\u90fd"}', Arr::convertJson(['成' => '都'], 0));
  10. }

inCondition 数据库 IN 查询条件

  1. public function testInCondition(): void
  2. {
  3. $data = [
  4. ['id' => 5, 'name' => 'hello'],
  5. ['id' => 6, 'name' => 'world'],
  6. ];
  7. $dataDemo2 = [
  8. [10, 'hello'],
  9. [11, 'world'],
  10. ];
  11. $result = Arr::inCondition($data, 'id');
  12. $json = <<<'eot'
  13. [
  14. 5,
  15. 6
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $json,
  20. $this->varJson($result)
  21. );
  22. $result = Arr::inCondition($data, 'name');
  23. $json = <<<'eot'
  24. [
  25. "hello",
  26. "world"
  27. ]
  28. eot;
  29. $this->assertSame(
  30. $json,
  31. $this->varJson($result)
  32. );
  33. $result = Arr::inCondition($dataDemo2, 0);
  34. $json = <<<'eot'
  35. [
  36. 10,
  37. 11
  38. ]
  39. eot;
  40. $this->assertSame(
  41. $json,
  42. $this->varJson($result)
  43. );
  44. }

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

  1. public function testInConditionWithFilter(): void
  2. {
  3. $data = [
  4. ['id' => 5, 'name' => 5],
  5. ['id' => '9', 'name' => 'world'],
  6. ['id' => 'haha', 'name' => '0'],
  7. ];
  8. $result = Arr::inCondition($data, 'id', fn ($v): int => (int) $v);
  9. $json = <<<'eot'
  10. [
  11. 5,
  12. 9,
  13. 0
  14. ]
  15. eot;
  16. $this->assertSame(
  17. $json,
  18. $this->varJson($result)
  19. );
  20. $result = Arr::inCondition($data, 'name', fn ($v): string => (string) $v);
  21. $json = <<<'eot'
  22. [
  23. "5",
  24. "world",
  25. "0"
  26. ]
  27. eot;
  28. $this->assertSame(
  29. $json,
  30. $this->varJson($result)
  31. );
  32. }