dealnews/typed-objects

创建具有类型/约束属性的对象的基类

v1.1.0 2019-06-30 14:23 UTC

This package is auto-updated.

Last update: 2024-09-09 01:09:51 UTC


README

一个用于创建具有类型属性的 PHP 对象的库。

它支持 dealnews/constraints 所支持的所有类型。

具有类型属性的示例对象

// A very simple example
class Foo extends \DealNews\TypedObjects\TypedProperty {
    protected $id;
    protected $name;

    const CONSTRAINTS = [
        "id" => [
            "type" => "integer"
        ],
        "name" => [
            "type" => "string"
        ],
    ];
}

$foo = new Foo();
$foo->id = "1"; // will be integer 1
$foo->id = "string"; // will throw an exception

类型数组示例

class FooSet extends \DealNews\TypedObjects\TypedArray {
    const REQUIRED_TYPE = "Foo";
}

$set = new FooSet();
$set[] = new Foo(); // allowed
$set[] = 1; // Throws an exception