<commit_before><?php
namespace Exedra\Application\Utilities;

class Form
{
	public $data;

	public function __construct(\Exedra\Application\Execution\Exec $exe)
	{
		$this->exe = $exe;

		if($flash_data = $this->exe->flash->has('form_data'))
			$this->set($this->exe->flash->get('form_data'));
	}

	private function buildParameter($name, $attr = null, $value = null, $valueOnly = false)
	{
		$attrs	= "";
		if(is_array($attr))
		{
			foreach($attr as $key=>$val)
			{
				$attrs	.= $key."='".$val."' ";
			}
		}
		else
		{
			$attrs = $attr?$attr:"";
		}

		// $value = $this->flash->get($name, $value);
		$value = $this->has($name) ? $this->get($name) : $value;
		$value = $value?($valueOnly?$value:"value='$value'"):"";

		return $attrs.$value;
	}

	/**
	 * @param mixed key
	 * @param mixed val
	 * @param boolean override
	 * @return $this
	 */
	public function set($key, $val = false, $override = false)
	{
		if(!is_string($key) && !is_array($key))
			return $this->exe->exception->create("Form.set first argument must be array, or string. ");

		// if key was intended as an array, recurse while treating val as the overriding flag.
		if(is_array($key))
		{
			foreach($key as $k=>$v)
			{
				$this->set($k, $v, $val);
			}
		}
		else
		{
			if(!$this->has($key) || $override == true)
				$this->data[$key] = $val;
		}

		return $this;
	}

	public function get($key)
	{
		return $this->data[$key];
	}

	/**
	 * @param mixed key
	 * @param mixed val
	 * @param boolean override
	 * @return $this
	 */
	public function populate($key, $val = false, $override = false)
	{
		return $this->set($key, $val, $override);
	}

	public function has($key)
	{
		return isset($this->data[$key]);
	}

	public function flash($data = array())
	{
		## re-flash with post data, if $data wasn't set.
		$data = count($data) == 0?$this->exe->request->post:$data;

		if(count($data) == 0) return $this;

		$this->exe->flash->set("form_data", $data);

		return $this;
	}

	public function text($name, $attr = null, $value = null)
	{
		return "<input name='$name' id='$name' type='text' ".$this->buildParameter($name, $attr, $value)." />";
	}

	public function textarea($name, $attr = null, $value = null)
	{
		return "<textarea name='$name' id='$name' $attr value='' ".$this->buildParameter($name, $attr, null).">".$this->buildParameter($name, null, $value, true)."</textarea>";
	}

	public function password($name,$attr = null,$value = null)
	{
		return "<input type='password' name='$name' id='$name' ".$this->buildParameter($name, $attr, $value)." />";
	}

	/**
	 * HTML 5 compliance
	 */
	public function date($name, $attr = null, $value = null)
	{
		return "<input type='date' name='$name' id='$name' ".$this->buildParameter($name, $attr, $value)." />";
	}

	/**
	 * HTML 5 compliance
	 */
	public function time($name, $attr = null, $value = null)
	{
		return "<input type='time' name='$name' id='$name' ".$this->buildParameter($name, $attr, $value)." />";
	}

	public function select($name,$array = array(),$attr = null,$value = null, $firstOpt = '[Please select]')
	{
		$array = is_array($array)?$array:array();
		$firstOpt = $firstOpt !== false?"<option value=''>$firstOpt</option>":"";

		$value = $this->buildParameter($name, null, $value, true);
		
		$sel = "<select name='$name' id='$name' ".$this->buildParameter($name, $attr)." >$firstOpt";
		$selected = "";
		foreach($array as $key=>$val)
		{
			if($value != "")
				$selected	= $value == $key?"selected":"";
			
			$sel .= "<option value='$key' $selected>$val</option>";
		}
		$sel .=	"</select>";
		
		return $sel;
	}

	public function radio($name,$array = array(),$attr = null,$value = null,$wrapper = "")
	{
		$array		= is_array($array)? $array : array();
		$result		= "";

		$attr = $this->buildParameter($name, $attr);
		$value = $this->buildParameter($name, null, $value, true);

		foreach($array as $key=>$val)
		{
			$sel	=  $value?($key == $value?"checked":""):"";
			$radio	= "<label><input type='radio' value='$key'  $sel $attr name='$name' />$val</label>";
			$result	.= $wrapper?str_replace("{content}",$radio, $wrapper):$radio;
		}

		return $result;
	}

	public function file($name,$attr = "")
	{
		return "<input type='file' name='$name' id='$name' ".$this->buildParameter($name, $attr)." />";
	}

	public function hidden($name,$attr = "",$value = "")
	{
		return "<input name='$name' id='$name' type='hidden' $message ".$this->buildParameter($name, $attr, $value)." />";
	}
}<commit_msg>Utilities/Form : Separate attribute/value building<commit_after><?php
namespace Exedra\Application\Utilities;

class Form
{
	public $data;

	public function __construct(\Exedra\Application\Execution\Exec $exe)
	{
		$this->exe = $exe;

		if($flash_data = $this->exe->flash->has('form_data'))
			$this->set($this->exe->flash->get('form_data'));
	}

	/**
	 * @param string name
	 * @param string value
	 * @param boolean valueOnly
	 * @return string
	 */
	private function buildValue($name, $value = null, $valueOnly = false)
	{
		if($value !== false)
		{
			$value = $this->has($name) ? $this->get($name) : $value;
			$value = $value?($valueOnly?$value:' value="'.$value. '"' ):"";
		}

		return $value;
	}

	/**
	 * @param mixed attr
	 * @return string
	 */
	private function buildAttr($attr = null)
	{
		$attrs	= "";
		if(is_array($attr))
		{
			foreach($attr as $key=>$val)
			{
				$attrs	.= $key.'="'.$val.'" ';
			}
		}
		else
		{
			$attrs = $attr?$attr:"";
		}

		return $attrs;
	}

	/**
	 * @param mixed key
	 * @param mixed val
	 * @param boolean override
	 * @return $this
	 */
	public function set($key, $val = false, $override = false)
	{
		if(!is_string($key) && !is_array($key))
			return $this->exe->exception->create("Form.set first argument must be array, or string. ");

		// if key was intended as an array, recurse while treating val as the overriding flag.
		if(is_array($key))
		{
			foreach($key as $k=>$v)
			{
				$this->set($k, $v, $val);
			}
		}
		else
		{
			if(!$this->has($key) || $override == true)
				$this->data[$key] = $val;
		}

		return $this;
	}

	/**
	 * @param mixed key
	 * @return mixed $this->data
	 */
	public function get($key = null)
	{
		return $key ? $this->data[$key] : $this->data;
	}

	/**
	 * aliast to set()
	 * @param mixed key
	 * @param mixed val
	 * @param boolean override
	 * @return $this
	 */
	public function populate($key, $val = false, $override = false)
	{
		return $this->set($key, $val, $override);
	}

	/**
	 * @param string key
	 * @param boolean
	 */
	public function has($key)
	{
		return isset($this->data[$key]);
	}

	/**
	 * @param array $data
	 * @return $this
	 */
	public function flash($data = array())
	{
		## re-flash with post data, if $data wasn't set.
		$data = count($data) == 0?$this->exe->request->post:$data;

		if(count($data) == 0) return $this;

		$this->exe->flash->set("form_data", $data);

		return $this;
	}

	/**
	 * @param string name
	 * @param mixed attr
	 * @param string value
	 * @return string
	 */
	public function text($name, $attr = null, $value = null)
	{
		$value = $this->buildValue($name, $value);
		$attr = $this->buildAttr($attr);
		return "<input name='$name' id='$name' type='text' $attr $value />";
	}

	/**
	 * @param string name
	 * @param mixed attr
	 * @param string value
	 * @return string
	 */
	public function textarea( $name, $attr = null, $value = null)
	{
		$attr = $this->buildAttr( $attr );
		$value = $this->buildValue( $name, $value, true );

		return '<textarea name="'.$name.'" id="'.$name.'" '.$attr.'>'.$value.'</textarea>';
	}

	/**
	 * @param string name
	 * @param mixed attr
	 * @param string value
	 * @return string
	 */
	public function password($name,$attr = null,$value = null)
	{
		$attr = $this->buildAttr($attr);
		$value = $this->buildValue($name, $value);
		return '<input type="password" name="'. $name .'" id="'.$name.'" '.$attr.' '.$value.' />';
	}

	/**
	 * HTML 5 compliance
	 */
	public function date($name, $attr = null, $value = null)
	{
		$attr = $this->buildAttr($attr);
		$value = $this->buildValue($name, $value);

		return '<input type="date" name="'. $name .'" id="'. $name .'" '.$attr.' '.$value.' />';
	}

	/**
	 * HTML 5 compliance
	 */
	public function time($name, $attr = null, $value = null)
	{
		$attr = $this->buildAttr($attr);
		$value = $this->buildValue($name, $value);

		return '<input type="time" name="'.$name.'" id="'.$name.'" '.$attr.' '.$value.' />';
	}

	/**
	 * @param string name
	 * @param array array
	 * @param mixed attr
	 * @param string value
	 * @param string firstOpt
	 * @return string
	 */
	public function select($name,$array = array(),$attr = null,$value = null, $firstOpt = '[Please select]')
	{
		$array = is_array($array)?$array:array();
		$firstOpt = $firstOpt !== false?'<option value="">'. $firstOpt .'</option>':'';

		$attr = $this->buildAttr($attr);
		$value = $this->buildValue($name, $value, true);

		$sel = '<select name="'.$name.'" id="'.$name.'" '.$attr.' >'.$firstOpt;
		$selected = '';
		foreach($array as $key=>$val)
		{
			if($value != '')
				$selected	= $value == $key?'selected':'';
			
			$sel .= '<option value="'.$key.'" '.$selected.'>'.$val.'</option>';
		}
		$sel .=	'</select>';
		
		return $sel;
	}

	/**
	 * @param string name
	 * @param array array
	 * @param mixed attr
	 * @param string value
	 * @param string wrapper
	 * @return string
	 */
	public function radio($name,$array = array(),$attr = null,$value = null,$wrapper = "")
	{
		$array		= is_array($array)? $array : array();
		$result		= "";

		$attr = $this->buildParameter($name, $attr);
		$value = $this->buildParameter($name, null, $value, true);

		foreach($array as $key=>$val)
		{
			$sel	=  $value?($key == $value?'checked':''):'';
			$radio	= '<label><input type="radio" value="'.$key.'"  '.$sel.' '.$attr.' name="'.$name.'" />'.$val.'</label>';
			$result	.= $wrapper?str_replace("{content}",$radio, $wrapper):$radio;
		}

		return $result;
	}

	/**
	 * @param string name
	 * @param mixed attr
	 * @return string
	 */
	public function file($name,$attr = null)
	{
		$attr = $this->buildAttr($attr);
		return '<input type="file" name="'.$name.'" id="'.$name.'" '.$attr.' />';
	}

	/**
	 * @param string name
	 * @param mixed attr
	 * @param string value
	 * @return string
	 */
	public function hidden($name,$attr = null,$value = null)
	{
		$attr = $this->buildAttr($attr);
		$value = $this->buildValue($name, $value);
		return '<input type="hidden" name="'.$name.'" id="'.$name.'" '.$attr.' '.$value.' />';
	}
}
{'fraction_non_alphanumeric': 0.22767259514247465, 'fraction_numerical': 0.0007649646203863071, 'mean_word_length': 3.5692442114460463, 'pattern_counts': {'":': 3, '<': 39, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7042252', 'n_tokens_mistral': 4061, 'n_tokens_neox': 3658, 'n_words': 854}