codekandis/code-message-interpreter

该软件包已被废弃,不再维护。作者建议使用 codekandis/constants-classes-translator 软件包代替。

该库根据抽象枚举将代码解释为消息。

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

注意: 该软件包已被废弃,不再维护。它已被 codekandis/constants-classes-translator 软件包取代。

使用 CodeMessageInterpreter 您可以将代码转换为可读的消息。例如,当第三方库抛出带错误代码但没有有意义消息的异常时很有用。

索引

安装

使用以下命令安装最新版本

$ composer require codekandis/code-message-interpreter

如何使用

定义代码和消息

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

实例化和 CodeMessageInterpreter

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

异常

CodeMessageInterpreter 在几种情况下会抛出 CodeMessageInterpreterException 异常。

  • 传入的代码或消息类名称无效
  • 要解释的代码不存在
  • 要解释的代码没有相应的消息