<commit_before><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Fuel Development Team
 * @license		MIT License
 * @copyright	2010 - 2011 Fuel Development Team
 * @link		http://fuelphp.com
 */

namespace Fuel\Core;



// ------------------------------------------------------------------------

/**
 * Validation
 *
 * Static object to allow static usage of validation through singleton.
 *
 * @package		Fuel
 * @subpackage	Core
 * @category	Core
 * @author		<NAME>
 */
class Validation {

	public static function factory($fieldset = 'default')
	{
		if ( ! $fieldset instanceof Fieldset)
		{
			$fieldset = (string) $fieldset;
			($set = \Fieldset::instance($fieldset)) && $fieldset = $set;
		}
		return new static($fieldset);
	}

	public static function instance($name = null)
	{
		$fieldset = \Fieldset::instance($name);
		return $fieldset === false ? false : $fieldset->validation();
	}

	/**
	 * @var	Fieldset
	 */
	protected $fieldset;

	/**
	 * @var	array	available after validation started running: contains given input values
	 */
	protected $input = array();

	/**
	 * @var	array	contains values of fields that validated succesfully
	 */
	protected $validated = array();

	/**
	 * @var	array	contains Validation_Error instances of encountered errors
	 */
	protected $errors = array();

	/**
	 * @var	array	contains a list of classnames and objects that may contain validation methods
	 */
	protected $callables = array();

	/**
	 * @var	array	contains validation error messages, will overwrite those from lang files
	 */
	protected $error_messages = array();

	protected function __construct($fieldset)
	{
		if ( ! $fieldset instanceof Fieldset)
		{
			$fieldset = Fieldset::factory($fieldset, array('validation_instance' => $this));
		}

		$this->fieldset = $fieldset;
		$this->callables = array($this);
	}

	/**
	 * Returns the related fieldset
	 *
	 * @return	Fieldset
	 */
	public function fieldset()
	{
		return $this->fieldset;
	}

	/**
	 * Simpler alias for Validation->add()
	 *
	 * @param	string		Field name
	 * @param	string		Field label
	 * @param	string		Rules as a piped string
	 * @return	Validation	$this to allow chaining
	 */
	public function add_field($name, $label, $rules = array())
	{
		$field = $this->add($name, $label);

		$rules = explode('|', $rules);
		foreach ($rules as $rule)
		{
			if (($pos = strpos($rule, '[')) !== false)
			{
				preg_match('#\[(.*)\]#', $rule, $param);
				$rule = substr($rule, 0, $pos);
				call_user_func_array(array($field, 'add_rule'), array_merge(array($rule), explode(',', $param[1])));
			}
			else
			{
				$field->add_rule($rule);
			}
		}
                
		return $field;
                // allows us to use complex validation rule like so:
                //$val->add_field('username', 'Your username', 'required|min_length[3]')->add_rule('valid_string', array('alpha', 'numeric'));

	}

	/**
	 * This will overwrite lang file messages for this validation instance
	 *
	 * @param	string
	 * @param	string
	 */
	public function set_message($rule, $message)
	{
		if ($message !== null)
		{
			$this->error_messages[$rule] = $message;
		}
		else
		{
			unset($this->error_messages[$rule]);
		}
	}

	/**<commit_msg>Added fix for complex rules.<commit_after><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Fuel Development Team
 * @license		MIT License
 * @copyright	2010 - 2011 Fuel Development Team
 * @link		http://fuelphp.com
 */

namespace Fuel\Core;



// ------------------------------------------------------------------------

/**
 * Validation
 *
 * Static object to allow static usage of validation through singleton.
 *
 * @package		Fuel
 * @subpackage	Core
 * @category	Core
 * @author		<NAME>
 */
class Validation {

	public static function factory($fieldset = 'default')
	{
		if ( ! $fieldset instanceof Fieldset)
		{
			$fieldset = (string) $fieldset;
			($set = \Fieldset::instance($fieldset)) && $fieldset = $set;
		}
		return new static($fieldset);
	}

	public static function instance($name = null)
	{
		$fieldset = \Fieldset::instance($name);
		return $fieldset === false ? false : $fieldset->validation();
	}

	/**
	 * @var	Fieldset
	 */
	protected $fieldset;

	/**
	 * @var	array	available after validation started running: contains given input values
	 */
	protected $input = array();

	/**
	 * @var	array	contains values of fields that validated succesfully
	 */
	protected $validated = array();

	/**
	 * @var	array	contains Validation_Error instances of encountered errors
	 */
	protected $errors = array();

	/**
	 * @var	array	contains a list of classnames and objects that may contain validation methods
	 */
	protected $callables = array();

	/**
	 * @var	array	contains validation error messages, will overwrite those from lang files
	 */
	protected $error_messages = array();

	protected function __construct($fieldset)
	{
		if ( ! $fieldset instanceof Fieldset)
		{
			$fieldset = Fieldset::factory($fieldset, array('validation_instance' => $this));
		}

		$this->fieldset = $fieldset;
		$this->callables = array($this);
	}

	/**
	 * Returns the related fieldset
	 *
	 * @return	Fieldset
	 */
	public function fieldset()
	{
		return $this->fieldset;
	}

	/**
	 * Simpler alias for Validation->add()
	 *
	 * @param	string		Field name
	 * @param	string		Field label
	 * @param	string		Rules as a piped string
	 * @return	Validation	$this to allow chaining
	 */
	public function add_field($name, $label, $rules = array())
	{
		$field = $this->add($name, $label);

		$rules = explode('|', $rules);
		foreach ($rules as $rule)
		{
			if (($pos = strpos($rule, '[')) !== false)
			{
				preg_match('#\[(.*)\]#', $rule, $param);
				$rule = substr($rule, 0, $pos);
				call_user_func_array(array($field, 'add_rule'), array_merge(array($rule), explode(',', $param[1])));
			}
			else
			{
				$field->add_rule($rule);
			}
		}
                
		return $field;

	}

	/**
	 * This will overwrite lang file messages for this validation instance
	 *
	 * @param	string
	 * @param	string
	 */
	public function set_message($rule, $message)
	{
		if ($message !== null)
		{
			$this->error_messages[$rule] = $message;
		}
		else
		{
			unset($this->error_messages[$rule]);
		}
	}

	/**
{'fraction_non_alphanumeric': 0.16905624704584843, 'fraction_numerical': 0.004253978257444462, 'mean_word_length': 2.9974811083123427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2992319', 'n_tokens_mistral': 2211, 'n_tokens_neox': 1954, 'n_words': 557}