Common and reusable contracts and exceptions.

v2.2.1 2014-08-26 12:41 UTC


Common/shared contracts and exceptions.


All of my components require PHP >= 5.4.


Use Composer to install: composer require ts/common:~2.2

EventDispatcher abstractions

Most of my components used the Symfony's EventDispatcher Component to provide runtime extension, which I found problematic when using them in projects based on other frameworks.

The adapters are trying to be as feature-complete as possible while allowing interoperability between Laravel 4, Symfony2 and Zend Framework 2.


I found myself in need of the ability to set options/flags for various libraries or classes (JSON or SplFileObject come to mind). This trait makes flagging, and abstracting flags through normal class variables and connected setters easier.

Use the trait and set flags you want to make default or abstract away somewhere, I prefer the constructor:

use TS\Common\Contract\Flag\FlagInterface;
use TS\Common\Contract\Flag\FlagTrait;

class JsonLibrary implements FlagInterface
    use FlagTrait;

    private $forceObject = false;

    private $prettyPrint = true;

    public function __construct()
        $this->addDefaultFlag(JSON_PRETTY_PRINT, 'prettyPrint');

        $this->addSetterFlag(JSON_PRETTY_PRINT, 'prettyPrint');
        $this->addSetterFlag(JSON_FORCE_OBJECT, 'forceObject', 'setForceObject' /* optional! */);

    public function createJson()
        return json_encode(/* Some data ... */, $this->getFlags());

    public function setForceObject($forceObject = true)
        $this->forceObject = $forceObject;

    public function setPrettyPrint($prettyPrint = true)
        $this->prettyPrint = $prettyPrint;

Please note that a default flag, while saved in a variable, does not need to be attached to a setter. It could come from a different source, or set by some other logic apart from manually doing so.

$this->getFlags() in JsonLibrary::createJson() would automatically get JSON_PRETTY_PRINT added to the flags.

Default flags can be disabled by setting the corresponding variable to false or by calling FlagTrait::removeDefaultFlag($flag).