<?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_ValidatorNumeric.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * 数値範囲検証クラス
 * 
 * 例）
 * $SyL_Validator =& SyL_Validator::create( 'range', '{name}は{min}から{max}の間の入力してください', array( 'min' => 1, 'max' => '10' ) );
 *
 * ○エラーメッセージ変換文字列
 *  {min} : 
 *  {max} : 
 *
 * @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_ValidatorNumeric.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_ValidatorNumeric extends SyL_Validator
{
    /**
     * コンストラクタ
     *
     * @access public
     * @param string エラーメッセージのフォーマット
     * @param array 検証パラメータ
     */
    function SyL_ValidatorNumeric($error_message, $parameters=array())
    {
        // デフォルトパラメータ
        $init_parameters = array(
          'min'       => null,
          'min_error' => null,
          'max'       => null,
          'max_error' => null
        );
        $parameters = array_merge($init_parameters, $parameters);
        parent::SyL_Validator($error_message, $parameters);
    }

    /**
     * 数値範囲検証処理を実行する
     *
     * @access public
     * @param string 検証対象値
     * @return bool true: エラー無し, false: エラーあり
     */
    function validate($value)
    {
        if (!is_numeric($value)) {
            return false;
        }

        // 最小値検証
        if ($this->parameters['min'] !== null) {
            if ($value < $this->parameters['min']) {
                if ($this->parameters['min_error'] !== null) {
                    $this->error_message = $this->parameters['min_error'];
                }
                return false;
            }
        }
  
        // 最大値検証
        if ($this->parameters['max'] !== null) {
            if ($value > $this->parameters['max']) {
                if ($this->parameters['max_error'] !== null) {
                    $this->error_message = $this->parameters['max_error'];
                }
                return false;
            }
        }

        return true;
    }

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

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

        $js = '';
        $js .= 'if (!error_flag && !validator.isNumeric(element_name, "' . $this->getErrorMessage($display_name) . '", "' . $min . '", "' . $min_error . '", "' . $max . '", "' . $max_error . '")) {' . "\r\n";
        $js .= '  error_flag = true;' . "\r\n";
        $js .= '}' . "\r\n";
        return $js;
    }
}

?>
