Code Monkey home page Code Monkey logo

php-strict's Introduction

php-strict

Strict-type Object base and Array-List for PHP 5! (5.4 and upper!)

License Packagist

Introduction

  • If you want PHP much clearer you'll be happy with Php-Strict!
  • If you need a way to pass packed arguments to functions you'll be happy with Php-Strict!
  • If you need a way to validate arguments and get rid of exceptions and data-misses on invalid arguments you'll be happy with Php-Strict!
  • If you want a strict-type ArrayList like Java and other strict-typed languages you'll be happy with Php-Strict!
  • Binding to HTTP request is comming soon!!

Installation

Add following code to your composer.json and run composer update:

{
  "require": {
    "sabloger/php-strict": "dev-master"
  }
}

Usage: Object

First extend one sub class from BaseObject, implement parent abstract methods, set predefines, instantiate then use! Finally run $obj->validate() for validate data!. For example: Sub-class:

use Php_Strict\BaseObject;

/**
 * Class Book
 * @package Php_Strict
 * @method Book setTitle(string $value)
 * @method Book setAuthor(string $value)
 * @method Book setYear(int $value)
 * @method Book setSome_Object(Object $value)
 * @method string getTitle()
 * @method string getAuthor()
 * @method int getYear()
 * @method Object getSome_Object()
 */
class Book extends BaseObject
{
    /**
     * @return array
     */
    public function getFieldsStub()
    {
        return [
            'title' => 'string', // All of types as you want!!
            'author' => 'string',
            'year' => 'int',
            'some_object' => Object::class
        ];
    }

    /**
     * @return array
     */
    public function getRequiredFields()
    {
        return [
            'title',
            'year'
        ];
    }

    /**
     * If you want to allow setting undefined properties, set it True!
     * @return bool
     */
    protected function isUndefinedSettingAllowed()
    {
        return false;
    }
}
  • For ease of use and get suggestions on IDE for fields setters and getters, I strongly suggesting write class level docs, as like as above sample.

Use:

$book = new Book(["Title" => "Advanced PHP Programming"]); // Its case-insensitive and auto case-correcter!! :)
$book->setAuthor("George Schlossnagle");
$book["Year"] = 2004;

$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR"; // Yessss its case-insensitive and auto case-correcter!!  ["Foo": "BAR"]

$book->set("some_object",$obj); // Several access methods!! Use these as you want!!

print $book; // It's echoable!!
//$book->toJson(); // Jsonable :)
//$book->toArray(); // Arrayable :)

foreach ($book as $key => $value) { // Iterateable :)
  echo "key: $key , value: $value \n";
}
/*
Out:
{"title":"Advanced PHP Programming","author":"George Schlossnagle","year":2004,"some_object":{"Foo":"BAR"}}
key: title , value: Advanced PHP Programming 
key: author , value: George Schlossnagle 
key: year , value: 2004 
key: some_object , value: {"Foo":"BAR"} 
*/

Usage: ArrayList

Can instantiate it directly and extend it for customization. For use only instantiate, setType and use!!

Basic use:

//Scalar:

$strArr = new ArrayList('string');
$strArr[] = "correct!";
$strArr[] = 123; //PHP Fatal error:  Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "string" given "integer"!'

print $strArr->toJson();


//Object:
$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR";

$arr = new ArrayList(Object::class);

//$arr[0] = 11; //PHP Fatal error:  Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "Php_Strict\Object" given "integer"!'

$arr[0] = $obj;
$obj->foo = "bar";
$arr[1] = $obj;
$arr[0]->baz = "bag";
$arr[2] = $obj;
print $arr . "\n";
print $obj; // Its by-reference!

Advanced use:

use Php_Strict\ArrayList;

class BookArrayList extends ArrayList
{
    /**
     * @param array $items
     * @return BookArrayList
     */
    public static function newLib(array $items = [])
    {
        return (new self($items));
    }

    /**
     * BookArrayList constructor.
     * @param array $items
     */
    public function __construct(array $items = [])
    {
        parent::__construct(Book::class, $items);
    }

    /**
     * Validate all of items using Object->validate() and ArrayList each()
     * @throws \Exception
     * @return null|true
     */
    public function validate()
    {
        parent::each(function (Book $item, $key) { // ArrayList has each(closure) method! :)
            try {
                $item->validate();
            } catch (\Exception $exception) {
                throw new \Exception(sprintf('ArrayList validation failed at offset (%s) with message: %s' , $key, $exception->getMessage()));
            }
        });
        return true;
    }
}

Use:

$book = new Book(["Title" => "Advanced PHP Programming"]);
$book->setAuthor("George Schlossnagle");

$bookArr = BookArrayList::newLib();
$bookArr[] = $book;
$bookArr->validate(); // PHP Fatal error:  Uncaught exception 'Exception' with message 'ArrayList validation failed at offset (0) with message: Required fields are not filled. unfilled required fields: (["year"])'

foreach ($bookArr as $book) { // Iterateable :)
  echo "item: $book  \n";
}

LICENSE

This library is released under the MIT license.

php-strict's People

Contributors

sabloger avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

php-strict's Issues

Define Alias for Field Names

e.g:

public function getFieldsStub()
    {
        return [
            'id' => 'string',
            'first_name' => 'string',
            'email' => 'string'
        ];
    }

public function getFieldsAlises()
    {
        return [
            'first_name' => 'first-name',
            'email' => 'mail'
        ];
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.