codekandis/constants-classes-translator

`codekandis/constants-classes-translator` is a library to translate values from constants classes into values of another constants classes.

2.0.0 2025-09-26 08:22 UTC

This package is auto-updated.

Last update: 2025-09-26 08:22:31 UTC


README

Version License Minimum PHP Version Code Coverage

codekandis/constants-classes-translator is a library to translate values from constants classes into values of another constants classes.

Index

Installation

Install the latest version with

$ composer require codekandis/constants-classes-translator

Testing

Test the code with

$ composer run-script test

If you want to retrieve a full coverage report run

$ composer run-script test-coverage

How to use

This 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
 */

Exceptions

The ConstantsClassesTranslator throws several exceptions.