lavoiesl / php-abstract-structure

Basic data structure with public properties while validating data type.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP Abstract Structures

Build Status Scrutinizer Code Quality Code Coverage

Basic data structure with public properties while validating data type.

Inspired from https://medium.com/laravel-4/ef6b7113dd40

Usage

Generic structure

Each property will the bind to the type of the first value that is set.

<?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
?>

Defined types

You may override getPropertyType for more granular control.

<?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
?>

TODO

Consider adding readonly properties

Author

About

Basic data structure with public properties while validating data type.


Languages

Language:PHP 100.0%