Skip to content

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

License

Notifications You must be signed in to change notification settings

tomphp/exception-constructor-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages