<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_ValidatorCompare.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * 比較検証クラス
 * 
 * 例）「==」 比較
 * $SyL_Validator =& SyL_Validator::create( 'compare', '{name}は必須です', array( 'compare' => '==', 'value' => 'aaa' );
 *
 * 例）「>」 比較
 * $SyL_Validator =& SyL_Validator::create( 'compare', '{name}は必須です', array( 'compare' => '>', 'value' => '13' );
 *
 * ○エラーメッセージ変換文字列
 *  {value} : 
 *
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_ValidatorCompare.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_ValidatorCompare extends SyL_Validator
{
    /**
     * コンストラクタ
     *
     * @access public
     * @param string エラーメッセージのフォーマット
     * @param array 検証パラメータ
     */
    function SyL_ValidatorCompare($error_message, $parameters=array())
    {
        // デフォルトパラメータ
        $init_parameters = array(
          'compare' => '==',
          'value'   => null
        );
        $parameters = array_merge($init_parameters, $parameters);
        parent::SyL_Validator($error_message, $parameters);
    }

    /**
     * 比較検証処理を実行する
     *
     * @access public
     * @param string 検証対象値
     * @return bool true: エラー無し, false: エラーあり
     */
    function validate($value)
    {
        $target  = $this->parameters['value'];
        $compare = $this->parameters['compare'];
  
        if (preg_match( '/^(!=|==)$/', $compare)) {
            return eval( 'return ("' . $value . '" ' . $compare . ' "' . $target . '");' );
        } else if (preg_match( '/^(<=|>=|>|<)$/', $compare)) {
            return eval( 'return (floatval("' . $value . '") ' . $compare . ' floatval("' . $target . '"));' );
        } else {
            return false;
        }
    }

    /**
     * エラーメッセージを取得する
     * ※オーバーライド
     *
     * @access public
     * @param string 要素名
     * @return string エラーメッセージ
     */
    function getErrorMessage($name='')
    {
        if ($this->parameters['value'] !== null) {
            $this->error_message = str_replace('{value}', $this->parameters['value'], $this->error_message);
        }
        return parent::getErrorMessage($name);
    }

    /**
     * 比較検証処理のJavaScriptを取得する
     *
     * @access public
     * @param string フォーム要素表示名
     * @param array フォーム要素の部品配列（radio, select, checkboxの場合のみ）
     * @return string JavaScript処理ロジック
     */
    function getJavaScript($display_name, $parts)
    {
        $target  = $this->parameters['value'];
        $compare = $this->parameters['compare'];

        $js  = '';
        $js .= 'if (!error_flag && !validator.isCompare(element_name, "' . $this->getErrorMessage($display_name) . '", "' . $target . '", "' . $compare . '" )) {' . "\r\n";
        $js .= '  error_flag = true;' . "\r\n";
        $js .= '}' . "\r\n";
        return $js;
    }
}

?>
