f2/types

使用纯PHP,无需解析DocBlock注释,实现具有类型转换、验证和注解的数据类型声明。

0.1.3 2020-08-12 23:40 UTC

This package is not auto-updated.

Last update: 2024-09-20 17:35:27 UTC


README

提供了一致且简单的类型系统,使用纯PHP进行类型转换和测试,不使用DocBlock注释。

它支持原生PHP类型,并且可以与其他任何软件结合使用而不会相互干扰。

支持的类型

  • Type\BOOL
  • Type\INT
  • Type\FLOAT
  • Type\NUMBER
  • Type\STRING
  • Type\ARR
  • Type\OBJ
  • Type\RESOURCE
  • Type\TYPE
  • Type\CALLBACK
  • Type\CLASSNAME

使用场景

模式元数据

例如,在声明数据库模式时

$user = [
    // 'id' can be either an integer or null
    'id'            => Type\INT + Type\NUL,

    // These fields must be strings, and 'length' is a custom annotation
    'username'      => Type\STRING + [ 'length' => 50 ],
    'first_name'    => Type\STRING + [ 'length' => 50 ],
    'last_name'     => Type\STRING + [ 'length' => 50 ],

    // 'last_login' can be either a string or null
    'last_login'    => Type\STRING + Type\NUL,
];

适用于类的静态上下文

use F2\Type;

class User {

    // Types can be combined
    protected $id          = Type\INT + Type\NUL;

    protected $username    = Type\STRING + [
        // Custom annotations must not start with '#'
        'caption' => 'Username',
        'field_type' => 'VARCHAR(20)',

        // Built in annotations for validation and more
        '#required' => true,
        '#validator' => [ self::class, 'usernameValidator' ],
        ];

    protected $notes = Type\STRING + [
        '#required' => false,
        '#maxlen' => 1000,
        ];

    protected $first_name  = Type\STRING + [
        'caption' => 'Given Name',
        'field_type' => 'VARCHAR(30)'

        '#required' => true,
        '#minlen' => 1,
        '#maxlen' => 30,
        '#ctype' => 'alpha',
        ];

    protected $last_name   = Type\STRING + [
        'caption' => 'Family Name',
        '#minlen' => 1,
        '#maxlen' => 30,
        '#ctype' => 'alpha',
        ];
    protected $last_login  = Type\STRING + Type\NUL;

}

您可以通过声明常量来创建一个集中管理的类型和注解库

use F2\Type;

class CustomTypes {

    const REQUIRED = [
        '#NOT' => [ null, '' ],
        ];

    const EMAIL = Type\STRING + [
        '#for' => 'email'
        ];

    const NAME = Type\STRING + [
        '#minlen' => 1,
        '#maxlen' => 30,
        '#preg' => '|[A-Z][a-zA-Z\ ]+[a-z]|',
        ];

    /**
     * Added here to illustrate the use case
     */
    protected $exampleField = CustomTypes::NAME + CustomTypes::REQUIRED + [
        'caption' => 'Example Field',
        ];

}

示例

基本类型测试

use F2\Type;

Type::is( 123, Type\INT );       // true
Type::is( 123, Type\FLOAT );     // false
Type::is( 123.0, Type\INT );     // false
Type::is( 123, Type\NUMBER );    // true
Type::is( 123.0, Type\NUMBER );  // true
Type::is( 123, Type\STRING );    // false
Type::is( "123", Type\STRING );  // true

测试类型组合

Type::is( 123.0, Type\STRING + Type\FLOAT );     // true
Type::is( "123", Type\STRING + Type\FLOAT );     // true

Type::is( 123, Type\STRING + Type\FLOAT );       // false because 123 is an integer

类型转换

use F2\Type;

Type\INT( "123 " );                 // 123
Type\STRING( 123 );                 // "123"
Type\BOOL( 1 );                     // true
Type\BOOL( 0 );                     // false

Type::as( "123", Type\INT )         // 123
Type::as( "123", Type::of(10.0) )   // 123.0

获取任何值的类型

use F2\Type;

Type::of(10.00)                     // Type\FLOAT

广泛单元测试

该库经过700多次单元测试,以检测发布之间的任何不兼容性,但尚未在生产环境中使用。

如果您计划在任何框架或应用程序中使用它,请告知我。