Source for file ValidatorManager.php

Documentation is available at ValidatorManager.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. /**
  19.  * フォームセットのバリデーションを実行します。
  20.  * 
  21.  * <pre>
  22.  * ■configの仕様:
  23.  *   array(
  24.  *       array(
  25.  *           'name' => 'フィールド名', // 必須
  26.  *           'label' => 'ラベル名',    // 任意
  27.  *           'validation' => array(
  28.  *               'Validator名' => array(
  29.  *                   '設定1' => '設定値1',
  30.  *                   '設定2' => '設定値2',
  31.  *                   ....
  32.  *               ),
  33.  *               ....
  34.  *           ),
  35.  *       ),
  36.  *       ....
  37.  *   )
  38.  *  
  39.  * ■エラーメッセージについて
  40.  * 
  41.  * </pre>
  42.  * 
  43.  *
  44.  * @package teeple
  45.  */
  46. {
  47.     
  48.     const DEFAULT_MESSAGE '{0}が正しく入力されていません。';
  49.  
  50.     /**
  51.      * @var Logger 
  52.      */
  53.     protected $log;
  54.     
  55.     /**
  56.      * @var Teeple_Request 
  57.      */
  58.     private $request;
  59.     public function setComponent_Teeple_Request($c{
  60.         $this->request $c;
  61.     }
  62.     
  63.     /**
  64.      * @var Teeple_Resource 
  65.      */
  66.     private $resource;
  67.     public function setComponent_Teeple_Resource($c{
  68.         $this->resource $c;
  69.     }
  70.     
  71.     /**
  72.      * @var Teeple_Container 
  73.      */
  74.     private $container;
  75.     public function setComponent_container($c{
  76.         $this->container $c;
  77.     }
  78.     
  79.     /**
  80.      * コンストラクタ
  81.      *
  82.      */
  83.     public function __construct({
  84.         $this->log = LoggerManager::getLogger(get_class($this));
  85.     }
  86.     
  87.     /**
  88.      * YAML形式の設定をパースします。
  89.      * @param $yaml YAML形式の文字列
  90.      * @return array config
  91.      */
  92.     public function parseYAML($yaml{
  93.         
  94.         $yamlConfig Horde_Yaml::load($yaml);
  95.         if (is_array($yamlConfig)) {
  96.             throw new Teeple_Exception("Validationの設定を解析できませんでした。");
  97.         }
  98.         //$this->log->debug(var_export($yamlConfig, true));
  99.         
  100.         $validationConfig array();
  101.         foreach($yamlConfig as $field => $validations{
  102.             $oneConfig array();
  103.             $fields explode('.',$field2);
  104.             if (count($fields== 2{
  105.                 $oneConfig['label'$fields[1];
  106.             }
  107.             $oneConfig['name'$fields[0];
  108.             $oneConfig['validation'$validations;
  109.             
  110.             array_push($validationConfig$oneConfig);
  111.         }
  112.         //$this->log->debug(var_export($validationConfig, true));
  113.         
  114.         return $validationConfig;
  115.     }
  116.         
  117.     /**
  118.      * 指定されたconfigでバリデーションを実行します。
  119.      * エラーがあった場合はエラーメッセージを組み立てて
  120.      * Requestにメッセージを追加します。
  121.      *
  122.      * @param object $obj 
  123.      * @param array $config 
  124.      * @return boolean 
  125.      */
  126.     public function execute($obj&$config{
  127.         
  128.         $result TRUE;
  129.         foreach($config as $fieldConfig{
  130.             $fieldName $fieldConfig['name'];
  131.             foreach($fieldConfig['validation'as $validatorName => $attr{
  132.                 
  133.                 // Validatorインスタンスを作成
  134.                 $validator $this->container->getPrototype("Teeple_Validator_"ucfirst($validatorName));
  135.                 if (is_object($validator)) {
  136.                     throw new Teeple_Exception("Validatorのインスタンスを作成できません。($validatorName)");
  137.                 }
  138.                 // 属性をセット
  139.                 foreach($attr as $key => $value{
  140.                     $validator->$key $value;
  141.                 }
  142.                 // Validatorを実行
  143.                 if ($validator->validate($obj$fieldName)) {
  144.                     $result FALSE;
  145.                     // エラーメッセージをセット
  146.                     $this->setErrorMessage($validator$validatorName$attr$fieldConfig);
  147.                     break;
  148.                 }
  149.             }
  150.         }
  151.         return $result;
  152.     }
  153.     
  154.     /**
  155.      * エラーメッセージをセットします。
  156.      *
  157.      * @param Teeple_Validator $validator 
  158.      * @param string $validatorName 
  159.      * @param array $validatorConfig 
  160.      * @param array $fieldConfig 
  161.      */
  162.     private function setErrorMessage($validator$validatorName&$validatorConfig&$fieldConfig{
  163.         
  164.         // メッセージとラベルの取得
  165.         $msg $this->getMessage($validatorName$validatorConfig);
  166.         $label $this->getLabel($fieldConfig);
  167.         
  168.         // パラメータを準備
  169.         $param array();
  170.         // {0}は必ずラベル名
  171.         array_push($param$label);
  172.         foreach($validator->args as $propName{
  173.             array_push($param$validator->$propName);
  174.         }
  175.         
  176.         // メッセージをフォーマットしてセット
  177.         $errorMessage Teeple_Util::formatErrorMessage($msg$param);
  178.         $this->request->addErrorMessage($errorMessage$fieldConfig['name']);
  179.         return;
  180.     }
  181.     
  182.     /**
  183.      * エラーメッセージを取得します。
  184.      *
  185.      * @param string $validatorName 
  186.      * @param array $validatorConfig 
  187.      * @return string 
  188.      */
  189.     private function getMessage($validatorName&$validatorConfig{
  190.         
  191.         if (isset($validatorConfig['msg']&& Teeple_Util::isBlank($validatorConfig['msg'])) {
  192.             return $validatorConfig['msg'];
  193.         }
  194.         $msg $this->resource->getResource('errors.'$validatorName);
  195.         if (Teeple_Util::isBlank($msg)) {
  196.             $msg self::DEFAULT_MESSAGE;
  197.         }
  198.         return $msg;
  199.     }
  200.     
  201.     /**
  202.      * ラベル名を取得します。
  203.      *
  204.      * @param array $fieldConfig 
  205.      * @return string 
  206.      */
  207.     private function getLabel(&$fieldConfig{
  208.         
  209.         if (isset($fieldConfig['label']&& Teeple_Util::isBlank($fieldConfig['label'])) {
  210.             return $fieldConfig['label'];
  211.         }
  212.         $label $this->resource->getResource('form.'$fieldConfig['name']);
  213.         if (Teeple_Util::isBlank($label)) {
  214.             $label $fieldConfig['name'];
  215.         }
  216.         return $label;
  217.     }
  218.     
  219. }
  220. ?>

Documentation generated on Mon, 26 Apr 2010 09:00:04 +0900 by phpDocumentor 1.4.3