tomphp/exception-constructor-tools

A simple PHP trait which makes creating static constructors for exceptions nicer.

v1.0.0 2016-09-09 09:30 UTC

This package is not auto-updated.

Last update: 2024-10-26 20:47:43 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

A simple PHP trait which makes creating static constructors for exceptions nicer.

Installation

$ composer require tomphp/exception-constructor-tools

Usage

Define your exception:

<?php

use TomPHP\ExceptionConstructorTools\ExceptionConstructorTools;

class MyExceptionClass extends \RuntimeException
{
    use ExceptionConstructorTools;

    public static function forEntity($entity)
    {
        return self::create(
            'There was an error with an entity of type %s with value of %s.',
            [
                self::typeToString($entity)
                self::valueToString($entity)
            ]
        );
    }
}

Throw your exception:

if ($errorOccurred) {
    throw MyExceptionClass::forEntity($entity);
}