Source for file ConverterManager.old.php

Documentation is available at ConverterManager.old.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.  * Converterを管理するクラス
  20.  * TODO 修正
  21.  *
  22.  * @package     teeple
  23.  */
  24. {
  25.     /**
  26.      * @var Converterを保持する 
  27.      *
  28.      * @access  private
  29.      * @since   3.0.0
  30.      */
  31.     protected $_list;
  32.  
  33.     /**
  34.      * @var Logger 
  35.      */
  36.     protected $log;
  37.     
  38.     /**
  39.      * @var Teeple_Request 
  40.      */
  41.     private $request;
  42.     public function setComponent_Teeple_Request($c{
  43.         $this->request $c;
  44.     }
  45.     
  46.     /**
  47.      * @var Teeple_Container 
  48.      */
  49.     private $container;
  50.     public function setComponent_container($c{
  51.         $this->container $c;
  52.     }
  53.     
  54.     /**
  55.      * コンストラクタ
  56.      *
  57.      * @access  public
  58.      * @since   3.0.0
  59.      */
  60.     public function __construct()
  61.     {
  62.         $this->_list = array();
  63.         $this->log = LoggerManager::getLogger(get_class($this));
  64.     }
  65.  
  66.     /**
  67.      * Convertを行う
  68.      *
  69.      * @param   array   $params Convertする条件が入った配列
  70.      * @access  public
  71.      * @since   3.0.0
  72.      */
  73.     public function execute($params)
  74.     {
  75.         if (!is_array($params|| (count($params1)) {
  76.             return true;
  77.         }
  78.  
  79.         // ConverterのListを生成
  80.         $this->_buildConverterList($params);
  81.  
  82.         //
  83.         // Convertを実行
  84.         //
  85.         $this->_convert($params);
  86.  
  87.         return true;
  88.     }
  89.  
  90.     /**
  91.      * ConverterのListを生成
  92.      *
  93.      * @param   array   $params Convertする条件が入った配列
  94.      * @access  private
  95.      * @since   3.0.0
  96.      */
  97.     private function _buildConverterList($params)
  98.     {
  99.         foreach ($params as $key => $value{
  100.             $key   preg_replace("/\s+/"""$key);
  101.             $value preg_replace("/\s+/"""$value);
  102.  
  103.             if ($key == ""{
  104.                 throw new Teeple_Exception("Converterの設定が不正です。");
  105.             }
  106.  
  107.             //
  108.             // $key は attribute.name のパターン
  109.             //
  110.             $keyArray explode("."$key);
  111.             if (count($keyArray!= 2{
  112.                 throw new Teeple_Exception("Converterのキーが不正です。");
  113.             }
  114.             $attribute $keyArray[0];     // 属性の名前
  115.             $name      $keyArray[1];     // Converterの名前 
  116.  
  117.             $className "Converter_" ucfirst($name);
  118.             
  119.             //
  120.             // 既に同名のConverterが追加されていたら何もしない
  121.             //
  122.             if (isset($this->_list[$name]&& is_object($this->_list[$name])) {
  123.                 continue;
  124.             }
  125.  
  126.             //
  127.             // オブジェクトの生成に失敗していたらエラー
  128.             //
  129.             $converter $this->container->getComponent($className);
  130.             if (!is_object($converter)) {
  131.                 throw new Teeple_Exception("Converter {$className} の生成に失敗しました。");
  132.             }
  133.  
  134.             $this->_list[$name$converter;
  135.         }
  136.     }
  137.  
  138.     /**
  139.      * Converterを実行
  140.      *
  141.      * @param   array   $params Convertする条件が入った配列
  142.      * @access  private
  143.      * @since   3.0.0
  144.      */
  145.     private function _convert($params)
  146.     {
  147.         foreach ($params as $key => $value{
  148.             $key   preg_replace("/\s+/"""$key);
  149.             $value preg_replace("/\s+/"""$value);
  150.  
  151.             if ($key == ""{
  152.                 throw new Teeple_Exception("Converterの設定が不正です。キーがありません。");
  153.             }
  154.  
  155.             //
  156.             // $key は attribute.name のパターン
  157.             //
  158.             $keyArray explode("."$key);
  159.             if (count($keyArray!= 2{
  160.                 throw new Teeple_Exception("Converterのkeyの形式が不正です。");
  161.             }
  162.             $attribute $keyArray[0];     // 属性の名前
  163.             $name      $keyArray[1];     // Converterの名前 
  164.  
  165.             //
  166.             // $value にはConvert後の値を入れる変数名がセットできる
  167.             //
  168.             $newAttribute $value;
  169.  
  170.             //
  171.             // Converterを取得
  172.             //
  173.             $converter $this->_list[$name];
  174.  
  175.             if (!is_object($converter)) {
  176.                 throw new Teeple_Exception("Converter {$className} の生成に失敗しました。");
  177.             }
  178.  
  179.             //
  180.             // attributeに * が指定されている場合は
  181.             // リクエストパラメータ全てが変換対象となる
  182.             //
  183.             if ($attribute == '*'{
  184.                 $attribute join(","array_keys($this->request->getParameters()));
  185.             }
  186.  
  187.             if (preg_match("/,/"$attribute)) {
  188.                 $attributes array();
  189.                 foreach (explode(","$attributeas $param{
  190.                     if ($param{
  191.                        $attributes[$param$this->request->getParameter($param);
  192.                     }
  193.                 }
  194.             else {
  195.                 $attributes $this->request->getParameter($attribute);
  196.             }
  197.  
  198.             //
  199.             // Converterを適用
  200.             //
  201.             $result $converter->convert($attributes);
  202.  
  203.             if ($newAttribute != ""{
  204.                 $this->request->setParameter($newAttribute$result);
  205.             else {
  206.                 if (is_array($attributes)) {
  207.                     foreach ($result as $key => $value{
  208.                         if ($key{
  209.                             $this->request->setParameter($key$value);
  210.                         }
  211.                     }
  212.                 else {
  213.                     $this->request->setParameter($attribute$result);
  214.                 }
  215.             }
  216.         }
  217.     }
  218. }
  219. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:39 +0900 by phpDocumentor 1.4.3