Source for file ActionError.php

Documentation is available at ActionError.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  ActionError.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: 614dea2d18a9f551b3b71346ac34785c6d308c70 $
  10.  */
  11.  
  12. // {{{ Ethna_ActionError
  13. /**
  14.  *  アプリケーションエラー管理クラス
  15.  *
  16.  *  @access     public
  17.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  18.  *  @package    Ethna
  19.  *  @todo   配列フォームを扱えるようにする
  20.  */
  21. {
  22.     /**#@+
  23.      *  @access private
  24.      */
  25.  
  26.     /** @protected    array   エラーオブジェクトの一覧 */
  27.     protected $error_list = array();
  28.  
  29.     /** @protected    object  Ethna_ActionForm    アクションフォームオブジェクト */
  30.     protected $action_form = null;
  31.  
  32.     /** @protected    object  Ethna_Logger        ログオブジェクト */
  33.     protected $logger = null;
  34.     /**#@-*/
  35.  
  36.     /**
  37.      *  Ethna_ActionErrorクラスのコンストラクタ
  38.      *
  39.      *  @access public
  40.      */
  41.     public function __construct()
  42.     {
  43.     }
  44.  
  45.     /**
  46.      *  エラーオブジェクトを生成/追加する
  47.      *
  48.      *  @access public
  49.      *  @param  string  $name       エラーの発生したフォーム項目名(不要ならnull)
  50.      *  @param  string  $message    エラーメッセージ
  51.      *  @param  int     $code       エラーコード
  52.      *  @return Ethna_Error エラーオブジェクト
  53.      */
  54.     public function add($name$message$code null)
  55.     {
  56.         if (func_num_args(3{
  57.             $userinfo array_slice(func_get_args()3);
  58.             $error Ethna::raiseNotice($message$code$userinfo);
  59.         else {
  60.             $error Ethna::raiseNotice($message$code);
  61.         }
  62.         $this->addObject($name$error);
  63.         return $error;
  64.     }
  65.  
  66.     /**
  67.      *  Ethna_Errorオブジェクトを追加する
  68.      *
  69.      *  @access public
  70.      *  @param  string              $name   エラーに対応するフォーム項目名(不要ならnull)
  71.      *  @param  object  Ethna_Error $error  エラーオブジェクト
  72.      */
  73.     public function addObject($name$error)
  74.     {
  75.         $elt array();
  76.         $elt['name'$name;
  77.         $elt['object'$error;
  78.         $this->error_list[$elt;
  79.  
  80.         // ログ出力(補足)
  81.         $af $this->_getActionForm();
  82.         $logger $this->_getLogger();
  83.         $logger->log(LOG_NOTICE'{form} -> [%s]'$this->action_form->getName($name));
  84.     }
  85.  
  86.     /**
  87.      *  エラーオブジェクトの数を返す
  88.      *
  89.      *  @access public
  90.      *  @return int     エラーオブジェクトの数
  91.      */
  92.     public function count()
  93.     {
  94.         return count($this->error_list);
  95.     }
  96.  
  97.     /**
  98.      *  エラーオブジェクトの数を返す(count()メソッドのエイリアス)
  99.      *
  100.      *  @access public
  101.      *  @return int     エラーオブジェクトの数
  102.      */
  103.     public function length()
  104.     {
  105.         return count($this->error_list);
  106.     }
  107.  
  108.     /**
  109.      *  登録されたエラーオブジェクトを全て削除する
  110.      *
  111.      *  @access public
  112.      */
  113.     public function clear()
  114.     {
  115.         $this->error_list = array();
  116.     }
  117.  
  118.     /**
  119.      *  指定されたフォーム項目にエラーが発生しているかどうかを返す
  120.      *
  121.      *  @access public
  122.      *  @param  string  $name   フォーム項目名
  123.      *  @return bool    true:エラーが発生している false:エラーが発生していない
  124.      */
  125.     public function isError($name)
  126.     {
  127.         foreach ($this->error_list as $error{
  128.             if (strcasecmp($error['name']$name== 0{
  129.                 return true;
  130.             }
  131.         }
  132.         return false;
  133.     }
  134.  
  135.     /**
  136.      *  指定されたフォーム項目に対応するエラーメッセージを返す
  137.      *
  138.      *  @access public
  139.      *  @param  string  $name   フォーム項目名
  140.      *  @return string  エラーメッセージ(エラーが無い場合はnull)
  141.      */
  142.     function getMessage($name)
  143.     {
  144.         foreach ($this->error_list as $error{
  145.             if (strcasecmp($error['name']$name== 0{
  146.                 return $this->_getMessage($error);
  147.             }
  148.         }
  149.         return null;
  150.     }
  151.  
  152.     /**
  153.      *  エラーオブジェクトを配列にして返す
  154.      *
  155.      *  @access public
  156.      *  @return array   エラーオブジェクトの配列
  157.      */
  158.     function getErrorList()
  159.     {
  160.         return $this->error_list;
  161.     }
  162.  
  163.     /**
  164.      *  エラーメッセージを配列にして返す
  165.      *
  166.      *  @access public
  167.      *  @return array   エラーメッセージの配列
  168.      */
  169.     function getMessageList()
  170.     {
  171.         $message_list array();
  172.  
  173.         foreach ($this->error_list as $error{
  174.             $message_list[$this->_getMessage($error);
  175.         }
  176.         return $message_list;
  177.     }
  178.  
  179.     /**
  180.      *  アプリケーションエラーメッセージを取得する
  181.      *
  182.      *  @access private
  183.      *  @param  array   エラーエントリ
  184.      *  @return string  エラーメッセージ
  185.      */
  186.     function _getMessage(&$error)
  187.     {
  188.         $af $this->_getActionForm();
  189.         $form_name $af->getName($error['name']);
  190.         return str_replace("{form}"$form_name$error['object']->getMessage());
  191.     }
  192.  
  193.     /**
  194.      *  Ethna_ActionFormオブジェクトを取得する
  195.      *
  196.      *  @access private
  197.      *  @return object  Ethna_ActionForm 
  198.      */
  199.     private function _getActionForm()
  200.     {
  201.         if (isset($this->action_form== false{
  202.             $controller Ethna_Controller::getInstance();
  203.             $this->action_form = $controller->getActionForm();
  204.         }
  205.         return $this->action_form;
  206.     }
  207.  
  208.     /**
  209.      *  Ethna_Loggerオブジェクトを取得する
  210.      *
  211.      *  @access private
  212.      *  @return object  Ethna_Logger 
  213.      */
  214.     private function _getLogger()
  215.     {
  216.         if (is_null($this->logger)) {
  217.             $controller Ethna_Controller::getInstance();
  218.             $this->logger = $controller->getLogger();
  219.         }
  220.         return $this->logger;
  221.     }
  222. }
  223. // }}}

Documentation generated on Fri, 11 Nov 2011 03:57:17 +0900 by phpDocumentor 1.4.3