codekandis/code-message-interpreter

This package is abandoned and no longer maintained. The author suggests using the codekandis/constants-classes-translator package instead.

This library interprets codes into messages based on abstract enumerators.

1.0.1 2021-01-16 10:35 UTC

This package is auto-updated.

Last update: 2021-01-16 10:36:50 UTC


README

Version License Minimum PHP Version Code Coverage

NOTE: This package is abandoned and no longer maintained. It has been superseded by the package codekandis/constants-classes-translator.

With the CodeMessageInterpreter you are able to translate codes into readable messages. E. g. is useful with third party libraries throwing exceptions with error codes but without meaningful messages.

Index

Installation

Install the latest version with

$ composer require codekandis/code-message-interpreter

How to use

Define the codes and messages

abstract class ErrorCodes
{
    public const ERROR_ONE   = 1;
    public const ERROR_TWO   = 2;
    public const ERROR_THREE = 3;
}

abstract class ErrorMessages
{
    public const ERROR_ONE   = 'Error one occurred.';
    public const ERROR_TWO   = 'Error two occurred.';
    public const ERROR_THREE = 'Error three occurred.';
}

Instantiate and the CodeMessageInterpreter

/** returns 'Error two occurred.' */
( new CodeMessageInterpreter( ErrorCodes::class, ErrorMessages::class ) )
    ->interpret( ErrorCodes::ERROR_TWO );

Exceptions

The CodeMessageInterpreter throws an CodeMessageInterpreterException in several cases.

  • the passed codes or messages class names are invalid
  • the code to interpret does not exist
  • the code to interpret does not have a corresponding message