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 2024-02-17 11:39 UTC

This package is auto-updated.

Last update: 2024-04-17 12:00:30 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

How to use

Define some error codes and error messages

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

abstract 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.';
}

Instantiate the ConstantsClassesTranslator

( new ConstantsClassesTranslator( ErrorCodes::class, ErrorMessages::class ) )
    ->translate( ErrorCodes::ERROR_TWO );
/**
 * Error two occured.
 */

or vice versa

( new ConstantsClassesTranslator( ErrorMessages::class, ErrorCodes::class ) )
    ->translate( ErrorMessages::ERROR_TWO );
/**
 * 2
 */

Exceptions

The ConstantsClassesTranslator throws several exceptions which inherits from ConstantsClassesTranslatorException.