Skip to content

Instantly share code, notes, and snippets.

@tokutoku3
Created December 22, 2016 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokutoku3/3aa00a1aad8a5cda1af6fbfd7515cd56 to your computer and use it in GitHub Desktop.
Save tokutoku3/3aa00a1aad8a5cda1af6fbfd7515cd56 to your computer and use it in GitHub Desktop.
<?php
/**
* 例外をスタックするためのクラス
*/
class ExceptionHolder
{
/**
* 既存の例外に新規の例外をスタックさせて返す
*
* @access public
* @param string $message エラーメッセージ
* @param null|Exception $previous 以前に使われた例外。連結先
* @link http://php.net/manual/ja/exception.construct.php
* @return Exception
*/
public static function e($message, $previous = null)
{
return new Exception($message, 0, $previous);
}
/**
* 例外にセットされたエラーメッセージを返す
* スタックされていた場合は、その分のメッセージを配列にして返す
*
* @access public
* @param Exception $e エラーメッセージを取得したい(スタックされた)例外
* @return array エラーメッセージの配列
*/
public static function exceptionToArray(Exception $e)
{
do {
$errors[] = $e->getMessage();
} while ($e = $e->getPrevious());
return array_reverse($errors);
}
/**
* 例外にセットされたエラーメッセージを返す
* スタックされていた場合は、その文のメッセージを連結した文字列として返す
*
* @access public
* @param Exception $e エラーメッセージを取得したい(スタックされた)例外
* @return string 連結したエラーメッセージの文字列
*/
public static function exceptionToString(Exception $e)
{
$errors = self::exceptionToArray($e);
return implode("<br>\n", $errors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment