<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2012 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flFormElementInteger Class
 *
 * HTML5 number input field for whole numbers.
 *
 * @package fusionLib
 */
class flFormElementInteger extends flFormElementText {

	/**
	 * The minimum value.
	 *
	 * @var int
	 */
	protected $min;

	/**
	 * The maximum value.
	 *
	 * @var int
	 */
	protected $max;

	/**
	 *	Construct the element.
	 *
	 * @param flForm $form The form the element will be used with or null if not using flForm
	 * @param string $name The name of the element.
	 * @param int $min The minimum value or null if not set.
	 * @param int $max The maximum value or null if not set.
	 * @param string $default The default value of the element.
	 */
	function __construct($form, $name, $min = null, $max = null, $default = null) {
		parent::__construct($form, $name, $default);
		$this->min = $min;
		$this->max = $max;
		$this->addValidator(new flFormValidatorInteger());
		if($min !== null)
			$this->addValidator(new flFormValidatorMin($min));
		if($max !== null)
			$this->addValidator(new flFormValidatorMax($max));
	}

	/**
	 * Draw the element.
	 */
	function render() {
		$html = "<input type=\"number\" id=\"$this->name\" name=\"$this->name\" value=\"" . $this->_getValueEscaped() . '"';
		if($this->maxLength !== null) $html .= " maxlength=\"$this->maxLength\"";
		if($this->min !== null) $html .= " min=\"$this->min\"";
		if($this->max !== null) $html .= " max=\"$this->max\"";
		return $html . $this->attribHTML() . " />\n";
	}
}
