lavoiesl/php-abstract-structure

此包已废弃且不再维护。未建议替代包。

具有公共属性并验证数据类型的基层数据结构。

v1.0 2014-04-08 05:48 UTC

This package is not auto-updated.

Last update: 2022-07-18 08:52:25 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

具有公共属性并验证数据类型的基层数据结构。

灵感来自https://medium.com/laravel-4/ef6b7113dd40

使用方法

通用结构

每个属性将绑定到第一个设置的值的类型。

<?php
use Lavoiesl\AbstractStructure\AbstractStructure;

class Generic extends AbstractStructure
{
    protected $foo;
}

$generic = new Generic;
$generic->foo = 1; // initial value
$generic->foo = 2; // type not changing, still good
$generic->foo = 'foo'; // Exception, wrong type
$generic->bar = 'foo'; // Exception, undefined
?>

定义的类型

你可以覆盖 getPropertyType 以获得更细粒度的控制。

<?php
use Lavoiesl\AbstractStructure\AbstractStructure;

class Article extends AbstractStructure
{
    protected $id;

    public function getPropertyType($property)
    {
        switch ($property) {
            case 'id': return 'integer|null';
        }
    }
}

$generic = new Article;
$generic->id = 1;
$generic->id = null;
$generic->id = 'foo'; // Exception
?>

待办事项

考虑添加只读属性

作者