Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.55 KB

README.md

File metadata and controls

49 lines (37 loc) · 1.55 KB

Exception Constructor Tools

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);
}