<commit_before><?php

class RecordValidator
{

    // configurables
	public static $autoTrim = true;
	
	// protected properties
	protected $_record;
	protected $_errors = array();


	// magic methods
	function __construct(&$record, $autoTrim = null)
	{
		//init autoTrim option to static default
		if (!isset($autoTrim))
		{
			$autoTrim = self::$autoTrim;
		}
		
		// apply autotrim
		if ($autoTrim)
		{
			self::trimArray($record);
		}

		// store record
		$this->_record = &$record;
	}


	// public instance methods
	public function resetErrors()
	{
		$this->_errors = array();
	}
	
	public function getErrors($id = false)
	{
		if($id === false)
			return $this->_errors;
		elseif(array_key_exists($id, $this->_errors))
			return $this->_errors[$id];
		else
			return false;
	}

	
	public function hasErrors($id = false)
	{
		if($id === false)
			return ( count($this->_errors) > 0 );
		elseif(array_key_exists($id, $this->_errors))
			return true;
		else
			return false;
	}


	public function addError($id, $errorMessage)
	{
		$this->_errors[$id] = $errorMessage;
	}


	public function validate($options)
	{
		// apply default
		$options = array_merge(array(
			'validator' => 'string'
			, 'required' => true
		), $options);


		// check 'field'
		if(empty($options['field']))
			die('FormValidator: required option "field" missing');
			
		// check 'id' and default to 'field'
		if(empty($options['id']))
		{
			if(is_array($options['field']))
				throw new Exception('Option "id" is required when option "field" is an array');
			else
				$options['id'] = $options['field'];
		}	

		// check validator
		if(!is_callable('Validators::'.$options['validator']))
			die("Invalid form validator: $options[validator]");
		

		// return false if any errors are already registered under 'id'
		if(array_key_exists($options['id'], $this->_errors))
		{
			return false;
		}
		

		// parse 'field' for multiple values and array paths
		if(is_array($options['field']))
		{
			$value = array();
			foreach($options['field'] AS $field_single)
			{
				$value[] = $this->resolveValue($field_single);
			}

			// skip validation for empty fields that aren't required
			if (!$options['required'] && !count(array_filter($value)))
			{
				return true;
			}
		}
		else
		{
			$value = $this->resolveValue($options['field']);

			// skip validation for empty fields that aren't required
			if (!$options['required'] && empty($value))
				return true;
		}


		// call validator
		$isValid = call_user_func(array('validators',$options['validator']), $value, $options);

		if ($isValid == false)
		{
			if(!empty($options['errorMessage']))
			{
				$this->_errors[$options['id']] = $options['errorMessage'];
			}
			else
			{
				// default 'errorMessage' built from 'id'
				$this->_errors[$options['id']] = Inflector::spacifyCaps($options['id']) . ' is ' . ($options['required'] && empty($value) ? 'missing' : 'invalid') . '.';
			}
			return false;
		}
		else
		{
			return true;
		}
	}
	

<commit_msg>made validation errors localizable<commit_after><?php

class RecordValidator
{

    // configurables
    public static $autoTrim = true;
	
	// protected properties
	protected $_record;
	protected $_errors = array();


	// magic methods
	function __construct(&$record, $autoTrim = null)
	{
		//init autoTrim option to static default
		if (!isset($autoTrim))
		{
			$autoTrim = self::$autoTrim;
		}
		
		// apply autotrim
		if ($autoTrim)
		{
			self::trimArray($record);
		}

		// store record
		$this->_record = &$record;
	}


	// public instance methods
	public function resetErrors()
	{
		$this->_errors = array();
	}
	
	public function getErrors($id = false)
	{
		if($id === false)
			return $this->_errors;
		elseif(array_key_exists($id, $this->_errors))
			return $this->_errors[$id];
		else
			return false;
	}

	
	public function hasErrors($id = false)
	{
		if($id === false)
			return ( count($this->_errors) > 0 );
		elseif(array_key_exists($id, $this->_errors))
			return true;
		else
			return false;
	}


	public function addError($id, $errorMessage)
	{
		$this->_errors[$id] = $errorMessage;
	}


	public function validate($options)
	{
		// apply default
		$options = array_merge(array(
			'validator' => 'string'
			, 'required' => true
		), $options);


		// check 'field'
		if(empty($options['field']))
			die('FormValidator: required option "field" missing');
			
		// check 'id' and default to 'field'
		if(empty($options['id']))
		{
			if(is_array($options['field']))
				throw new Exception('Option "id" is required when option "field" is an array');
			else
				$options['id'] = $options['field'];
		}	

		// check validator
		if(!is_callable('Validators::'.$options['validator']))
			die("Invalid form validator: $options[validator]");
		

		// return false if any errors are already registered under 'id'
		if(array_key_exists($options['id'], $this->_errors))
		{
			return false;
		}
		

		// parse 'field' for multiple values and array paths
		if(is_array($options['field']))
		{
			$value = array();
			foreach($options['field'] AS $field_single)
			{
				$value[] = $this->resolveValue($field_single);
			}

			// skip validation for empty fields that aren't required
			if (!$options['required'] && !count(array_filter($value)))
			{
				return true;
			}
		}
		else
		{
			$value = $this->resolveValue($options['field']);

			// skip validation for empty fields that aren't required
			if (!$options['required'] && empty($value))
				return true;
		}


		// call validator
		$isValid = call_user_func(array('validators',$options['validator']), $value, $options);

		if ($isValid == false)
		{
			if(!empty($options['errorMessage']))
			{
				$this->_errors[$options['id']] = gettext($options['errorMessage']);
			}
			else
			{
				// default 'errorMessage' built from 'id'
    			$this->_errors[$options['id']] = sprintf($options['required'] && empty($value) ? _('%s is missing.') :  _('%s is invalid.'), Inflector::spacifyCaps($options['id']));
			}
			return false;
		}
		else
		{
			return true;
		}
	}
	


{'fraction_non_alphanumeric': 0.17131011608623548, 'fraction_numerical': 0.0003316749585406302, 'mean_word_length': 3.4806835066864785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '3853230', 'n_tokens_mistral': 2294, 'n_tokens_neox': 1951, 'n_words': 454}