codekandis/constants-classes-translator is a library to translate values from constants classes into values of another constants classes.
Install the latest version with
$ composer require codekandis/constants-classes-translatorTest the code with
$ composer run-script testIf you want to retrieve a full coverage report run
$ composer run-script test-coverageThis example demonstrates how to simply translate between error codes and error messages.
First create interfaces or classes containing identical named constants representing error codes and error messages.
interface ErrorCodesInterface
{
public const int ERROR_ONE = 1;
public const int ERROR_TWO = 2;
public const int ERROR_THREE = 3;
}
class ErrorMessages
{
public const string ERROR_ONE = 'Error one occurred.';
public const string ERROR_TWO = 'Error two occurred.';
public const string ERROR_THREE = 'Error three occurred.';
}Next translate error codes into error messages.
new ConstantsClassesTranslator( ErrorCodesInterface::class, ErrorMessages::class )
->translate( ErrorCodesInterface::ERROR_TWO );
/**
* Error two occured.
*/Or translate error messages into error codes.
new ConstantsClassesTranslator( ErrorMessages::class, ErrorCodesInterface::class )
->translate( ErrorMessages::ERROR_TWO );
/**
* 2
*/The ConstantsClassesTranslator throws several exceptions.
InterfaceOrClassNotFoundExceptiona passed constants interface or class does not existInterfaceOrClassConstantNotFoundExceptiona constant of a specific interface or class does not existInterfaceOrClassConstantValueNotFoundExceptiona constant with a specific value of a specific interface or class does not exist