类型

Testing Is Documentation

tests/Support/TypeTest.php类型 - 图1

QueryPHP 提供了增加 PHP 自身类型的辅助方法。

Uses

  1. <?php
  2. use Leevel\Support\Type;
  3. use stdClass;

判断是否为字符串

  1. public function testTypeString(): void
  2. {
  3. $this->assertTrue(Type::type('foo', 'string'));
  4. $this->assertFalse(Type::type(1, 'string'));
  5. }

判断是否为整型

  1. public function testTypeInt(): void
  2. {
  3. $this->assertTrue(Type::type(1, 'int'));
  4. $this->assertTrue(Type::type(3, 'integer'));
  5. $this->assertFalse(Type::type(true, 'int'));
  6. }

判断是否为浮点数

  1. public function testTypeFloat(): void
  2. {
  3. $this->assertTrue(Type::type(1.1, 'float'));
  4. $this->assertTrue(Type::type(3.14, 'double'));
  5. $this->assertFalse(Type::type(true, 'double'));
  6. }

判断是否为布尔值

  1. public function testTypeBool(): void
  2. {
  3. $this->assertTrue(Type::type(true, 'bool'));
  4. $this->assertTrue(Type::type(false, 'bool'));
  5. $this->assertFalse(Type::type(4, 'bool'));
  6. }

判断是否为数字

  1. public function testTypeNumeric(): void
  2. {
  3. $this->assertTrue(Type::type(1.2, 'numeric'));
  4. $this->assertTrue(Type::type(2, 'numeric'));
  5. $this->assertTrue(Type::type('2.5', 'numeric'));
  6. $this->assertFalse(Type::type(false, 'numeric'));
  7. }

判断是否为标量

  1. public function testTypeScalar(): void
  2. {
  3. $this->assertTrue(Type::type(1, 'scalar'));
  4. $this->assertTrue(Type::type('hello world', 'scalar'));
  5. $this->assertTrue(Type::type(0, 'scalar'));
  6. $this->assertTrue(Type::type(false, 'scalar'));
  7. $this->assertTrue(Type::type(1.1, 'scalar'));
  8. $this->assertTrue(Type::type(false, 'scalar'));
  9. $this->assertFalse(Type::type([], 'scalar'));
  10. $this->assertFalse(Type::type(null, 'scalar'));
  11. }

判断是否为资源

  1. public function testTypeResource(): void
  2. {
  3. $testFile = __DIR__.'/test.txt';
  4. file_put_contents($testFile, 'foo');
  5. $resource = fopen($testFile, 'r');
  6. $this->assertTrue(Type::type($resource, 'resource'));
  7. $this->assertFalse(Type::type(4, 'resource'));
  8. fclose($resource);
  9. unlink($testFile);
  10. }

判断是否为闭包

  1. public function testTypeClosure(): void
  2. {
  3. $this->assertTrue(Type::type(function () {
  4. }, 'Closure'));
  5. $this->assertFalse(Type::type(true, 'Closure'));
  6. }

判断是否为数组

格式支持

  • 支持 PHP 内置或者自定义的 is_array,is_int,is_custom 等函数
  • 数组支持 array:int,string 格式,值类型
  • 数组支持 array:int:string,string:array 格式,键类型:值类型
  • 数组支持 array:string:array:string:array:string:int 无限层级格式,键类型:值类型:键类型:值类型…(值类型|键类型:值类型)
  1. public function testTypeArray(): void
  2. {
  3. $this->assertTrue(Type::type([], 'array'));
  4. $this->assertTrue(Type::type([1, 2], 'array:int'));
  5. $this->assertFalse(Type::type([1, 2], 'array:'));
  6. $this->assertTrue(Type::type([1, 2], 'array:int:int'));
  7. $this->assertTrue(Type::type(['foo' => 1, 'bar' => 2], 'array:string:int'));
  8. $this->assertTrue(Type::type(['foo' => [], 'bar' => []], 'array:string:array'));
  9. $this->assertTrue(Type::type(['foo' => [1, 2, 3], 'bar' => [4, 5, 6]], 'array:string:array:int'));
  10. $this->assertFalse(Type::type(['foo' => [1, 2, 3], 'bar' => [4, 5, 6]], 'array:string:array:string'));
  11. $this->assertTrue(Type::type(['foo' => ['hello' => 1], 'bar' => ['hello' => 4]], 'array:string:array:string:int'));
  12. $this->assertTrue(Type::type(['foo' => ['hello' => ['foo' => 2]], 'bar' => ['hello' => ['foo' => 2]]], 'array:string:array:string:array:string:int'));
  13. }

判断是否为对象

  1. public function testTypeObject(): void
  2. {
  3. $this->assertTrue(Type::type(new stdClass(), 'object'));
  4. $this->assertFalse(Type::type(null, 'object'));
  5. }

判断是否为 NULL

  1. public function testTypeNull(): void
  2. {
  3. $this->assertTrue(Type::type(null, 'null'));
  4. $this->assertFalse(Type::type(1, 'null'));
  5. }

判断是否为回调

\Tests\Support\Callback1 定义

  1. namespace Tests\Support;
  2. class Callback1
  3. {
  4. public function test(): void
  5. {
  6. }
  7. public static function test2()
  8. {
  9. }
  10. }
  1. public function testTypeCallback(): void
  2. {
  3. $this->assertTrue(Type::type(function () {
  4. }, 'callable'));
  5. $this->assertTrue(Type::type('md5', 'callable'));
  6. $this->assertTrue(Type::type([new Callback1(), 'test'], 'callable'));
  7. $this->assertTrue(Type::type([Callback1::class, 'test2'], 'callable'));
  8. $this->assertFalse(Type::type(1, 'callable'));
  9. }

判断是否为对象实例

\Tests\Support\Callback2 定义

  1. namespace Tests\Support;
  2. class Callback2 implements IInterface
  3. {
  4. }

\Tests\Support\IInterface 定义

  1. namespace Tests\Support;
  2. interface IInterface
  3. {
  4. }
  1. public function testTypeInstance(): void
  2. {
  3. $this->assertTrue(Type::type(new stdClass(), stdClass::class));
  4. $this->assertTrue(Type::type(new Callback1(), Callback1::class));
  5. $this->assertTrue(Type::type(new Callback2(), IInterface::class));
  6. $this->assertFalse(Type::type(1, 'callback'));
  7. }

判断是否为指定的几种类型

  1. public function testTypeThese(): void
  2. {
  3. $this->assertTrue(Type::these('foo', ['string']));
  4. $this->assertTrue(Type::these(1, ['string', 'int']));
  5. }

判断是否为数组元素类型

格式支持

  • 数组支持 int,string 格式,值类型
  • 数组支持 int:string,string:array 格式,键类型:值类型
  • 数组支持 string:array:string:array:string:int 无限层级格式,键类型:值类型:键类型:值类型…(值类型|键类型:值类型)
  1. public function testTypeStrictArray(): void
  2. {
  3. $this->assertTrue(Type::arr(['foo'], ['string']));
  4. $this->assertFalse(Type::arr([1, 2], ['string']));
  5. $this->assertTrue(Type::arr(['bar', 'foo'], ['string']));
  6. $this->assertTrue(Type::arr(['bar', 2], ['string', 'int']));
  7. $this->assertTrue(Type::arr(['hello' => 'bar', 2], ['string:string', 'int']));
  8. $this->assertTrue(Type::arr(['hello' => 'bar', 'foo' => 'bar'], ['string:string']));
  9. $this->assertFalse(Type::arr(['hello' => 'bar', 2], ['string:string']));
  10. $this->assertFalse(Type::arr(['foo' => [1, 2, 3], 'bar' => [4, 5, 6]], ['string:array:string']));
  11. $this->assertTrue(Type::arr(['foo' => ['hello' => 1], 'bar' => ['hello' => 4]], ['string:array:string:int']));
  12. $this->assertTrue(Type::arr(['foo' => ['hello' => ['foo' => 2]], 'bar' => ['hello' => ['foo' => 2]]], ['string:array:string:array:string:int']));
  13. }