codekandis / constants-classes-translator
`codekandis/constants-classes-translator` is a library to translate values from constants classes into values of another constants classes.
Requires
- php: >=8.4
- codekandis/toolkit: ~1.0
- codekandis/types: ~1.0
Requires (Dev)
- codekandis/phpunit: ~5.0
- rector/rector: ~1.2
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2025-09-16 05:23:55 UTC
README
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
Known coverage issues
This library is 93.10% covered by tests. But it can be assumed as 100% tested.
There are several thrown exceptions of type UnexpectedErrorException
. These exceptions are wrapping catched exceptions that could be thrown but won't because the tested code prevents it. These origin exceptions could be catched and ignored silently. But any UnexpectedErrorException
is a fallback just in case the wrapped code changes its behaviour.
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.
- [
InterfaceOrClassNotFoundException
][xtsrclink-constants-class-not-found-exception] a passed constants interface or class does not exist - [
InterfaceOrClassConstantNotFoundException
][xtsrclink-constants-class-value-not-found-exception] a constant of a specific interface or class does not exist - [
InterfaceOrClassConstantValueNotFoundException
][xtsrclink-corresponding-constants-class-value-not-found-exception] a constant with a specific value of a specific interface or class does not exist