rollerworks / exception-parser
将异常信息提取到数组中
Requires
- php: >=5.3.3
Requires (Dev)
- phpspec/phpspec: ~2.1@dev
This package is not auto-updated.
Last update: 2022-02-01 12:36:48 UTC
README
此包提供了Rollerworks ExceptionParser,可以将异常数据提取到数组中。
基本上,它将多个异常捕获块封装在一个调用中,并返回解析信息。
安装
1. 使用Composer
要安装Rollerworks ExceptionParser,将rollerworks/exception-parser
添加到您的composer.json文件中
$ php composer.phar require rollerworks/exception-parser:"~1.0"
或者手动,通过将以下内容添加到您的composer.json
文件中
// composer.json { // ... require: { // ... "rollerworks/exception-parser": "~1.0" } }
然后,您可以通过从您的composer.json
文件所在的目录运行Composer的update
命令来安装新的依赖项
$ php composer update exception-parser
注意:此包是独立的,Composer仅用于生成自动加载类。
您也可以使用自己的自动加载器,但请确保它与PSR-4兼容。请参阅此包中的composer.json文件以获取映射信息。
使用方法
使用方法非常简单,您要处理的每个异常都需要一个兼容的解析器,该解析器将异常解析为数组。
ExceptionParserManager按注册顺序遍历所有注册的异常解析器,直到一个解析器接受该异常。
如果没有找到兼容的解析器,则返回空数组。
异常类
class FieldRequiredException extends \RuntimeException { private $fieldName; private $groupIdx; private $nestingLevel; /** * @param string $fieldName * @param integer $groupIdx * @param integer $nestingLevel */ public function __construct($fieldName, $groupIdx, $nestingLevel) { $this->fieldName = $fieldName; $this->groupIdx = $groupIdx; $this->nestingLevel = $nestingLevel; parent::__construct(sprintf('Field "%s" is required but is missing in group %d at nesting level %d.', $fieldName, $groupIdx, $nestingLevel)); } /** * @return string */ public function getFieldName() { return $this->fieldName; } /** * @return int */ public function getGroupIdx() { return $this->groupIdx; } /** * @return int */ public function getNestingLevel() { return $this->nestingLevel; } }
异常解析器
SearchFieldRequiredExceptionParser
异常解析器解析了上面显示的FieldRequiredException
。
use Acme\Exception\FieldRequiredException; use Rollerworks\Component\ExceptionParser\ExceptionParserInterface; class SearchFieldRequiredExceptionParser implements ExceptionParserInterface { /** * Returns whether the processor accepts the exception. * * @param \Exception $exception * * @return bool */ public function accepts(\Exception $exception) { return $exception instanceof FieldRequiredException; } /** * Returns parameters parsed from the exception. * * @param \Exception $exception * * @return array */ public function parseException(\Exception $exception) { /** @var FieldRequiredException $exception */ return array( 'message' => 'Field "{{ field }}" is required but is missing in group {{ group }} at nesting level {{ nesting }}.', 'field' => $exception->getFieldName(), 'group' => $exception->getGroupIdx(), 'nesting' => $exception->getNestingLevel(), ); } }
解析器管理器
现在在ExceptionParserManager中注册解析器。
注意:您可以注册所需数量的处理器。
use Rollerworks\Component\ExceptionParser\ExceptionParserManager; require 'vendor/autoload.php'; $exceptionParser = new ExceptionParserManager(); $exceptionParser->addExceptionParser(new SearchFieldRequiredExceptionParser()); try { throw new Acme\Exception\FieldRequiredException('name', 0, 0); } catch (Exception $exception) { $params = $exceptionParser->processException($exception); /* $params = array( 'message' => 'Field "{{ field }}" is required but is missing in group {{ group }} at nesting level {{ nesting }}.', 'field' => 'name', 'group' => 0, 'nesting' => 0, ) */ }
键转换
ExceptionParserManager还允许将数组键转换为其他格式,如'{{ name }}'。
'{var}'是占位符,将被当前键替换。
$exceptionParser = new ExceptionParserManager('{{ {var} }}');
或者您可以使用一个返回转换键的回调。如果键包含前缀或被特殊格式包裹,这将非常有用。
$keyTransformer = new function ($value) { return ltrim($value, '$'); }; $exceptionParser = new ExceptionParserManager($keyTransformer);
注意:如果回调返回null或void,则键将作为递增索引添加。只有当“回调”返回null或void时,将null作为构造函数的值是默认的,并且不会转换数组键。
require 'vendor/autoload.php'; $keyTransformer = new function ($value) { return null; }; $exceptionParser = new ExceptionParserManager($keyTransformer); $exceptionParser->addExceptionParser(new ExceptionParser\SearchFieldRequiredExceptionParser()); try { throw new Acme\Exception\FieldRequiredException('name', 0, 0); } catch (Exception $exception) { $params = $exceptionParser->processException($exception); /* $params = array( 0 => 'Field "{{ field }}" is required but is missing in group {{ group }} at nesting level {{ nesting }}.', 1 => 'name', 2 => 0, 3 => 0, ) */ }