jojo1981/php-types

PHP 类型库,其中包含代表 PHP 类型的值类

4.0.0 2023-02-06 09:04 UTC

This package is auto-updated.

Last update: 2024-09-10 13:53:02 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

作者:Joost Nijhuis <jnijhuis81@gmail.com>

这个库包含一些值类,它们代表 PHP 类型。这些值类可以根据 PHP 类型名称或根据值来构建。有 2 个伪类型类:`Jojo1981\PhpTypes\MixedType` 和 `Jojo1981\PhpTypes\MultiType`,它们将作为组合使用。还包括一个抽象工厂类:`Jojo1981\PhpTypes\AbstractType`。

以下类型受支持,并列出了它们的别名

  • 数组
  • 布尔型 (布尔)
  • 可调用型 (回调)
  • 浮点数 (数字,实数,双精度)
  • 整型
  • 可迭代型
  • 对象
  • 资源
  • 字符串 (文本)
  • 空值

伪类型

  • 混合型
  • 多类型

安装

git clone https://github.com/jojo1981/php-types.git

Composer

安装 PHP Composer

composer require jojo1981/php-types

基本用法

<?php

require 'vendor/autoload.php';

$integerType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('int');
$integerType->isAssignableValue('text'); // will return false
$integerType->isAssignableValue(28); // will return true

$floatType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(5.0);
$floatType1->isAssignableValue(true); // will return false
$floatType1->isAssignableValue('text'); // will return false
$floatType1->isAssignableValue(10); // will return false
$floatType1->isAssignableValue(10.0); // will return true

$floatType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('number');
$floatType2->isAssignableValue(true); // will return false
$floatType2->isAssignableValue('text'); // will return false
$floatType2->isAssignableValue(1); // will return false
$floatType2->isAssignableValue(1.0); // will return true

$stringType = \Jojo1981\PhpTypes\AbstractType::createFromValue('string');
$stringType->isAssignableValue(true); // will return false
$stringType->isAssignableValue('text'); // will return true

// Other types: array, bool, callable, iterable, null, resource, void

// Object | Class types

$objectType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('object');
$objectType->isAssignableValue(true); // will return false
$objectType->isAssignableValue(new \stdClass()); // will return true
$objectType->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true (for all object types)

$classType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity());
$classType1->isAssignableValue(new \stdClass()); // will return false
$classType1->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity::class);
$classType2->isAssignableValue(new \stdClass()); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType3 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\InterfaceTestEntity::class);
$classType3->isAssignableValue(new \stdClass()); // will return false
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return true
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

// Pseudo types
$mixedType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('mixed');
$mixedType->isAssignableValue(null); // will return true (always)
$mixedType->isPseudoType(); // will return true

$multiType = \Jojo1981\PhpTypes\AbstractType::createFromTypes([$stringType, $integerType]);
$multiType->isAssignableValue(true); // will return false
$multiType->isAssignableValue('text'); // will return true
$multiType->isAssignableValue(28); // will return true
$multiType->isPseudoType(); // will return true