chadicus/encapsules

A collection of wrapper classes that encapsulates behavior with primitive types

Installs: 0

Dependents: 0

Watchers: 1

Forks: 1

Language: PHP

v1.0.1 2014-04-03 17:35 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

A collection of small wrapper classes which encapsulates behavior and/or validation with primitive types.

Example Usage

Given the following class:

class Widget
{
    public function get($id)
    {
        if (!is_string($id) || trim($id) == '') {
            /** throw? return null/false? */
        }

        /** get widget from data storage */
    }

    public function update($id, array $data)
    {
        if (!is_string($id) || trim($id) == '') {
            /** throw? return null/false? */
        }

        /** update widget in data storage */
    }

    public function delete($id)
    {
        if (!is_string($id) || trim($id) == '') {
            /** throw? return null/false? */
        }

        /** delete widget from data storage */
    }
}

In all three cases the $id parameter must be a non-empty string. The check for the validity of $id is repeated in each method. By using an Encapsule you can wrap the validation behavior with the primitive string type resulting cleaner code and less duplication.

use Chadicus\Encapsules\String\NonEmpty;

class Widget
{
    public function get(NonEmpty $id)
    {
        /** get widget from data storage */
    }

    public function update(NonEmpty $id, array $data)
    {
        /** update widget in data storage */
    }

    public function delete(NonEmpty $id)
    {
        /** delete widget from data storage */
    }
}

Requirements

Encapsules requires PHP 5.4 (or later).

Composer

To add the library as a local, per-project dependency use Composer! Simply add a dependency on chadicus/encapsules to your project's composer.json file such as:

{
    "require": {
        "chadicus/encapsules": "~1.0"
    }
}

Documentation

PHP docs for the project can be found here.

Contact

Developers may be contacted at:

Project Build

With a checkout of the code get Composer in your PATH and run:

./build.php