<commit_before><?php

namespace serviform;

/**
 * Base validator class
 */
abstract class ValidatorBase implements IValidator
{
	use \serviform\traits\Configurable;


	/**
	 * @var string
	 */
	public $message = 'Field "#label#" is invalid';
	/**
	 * @var bool
	 */
	public $skipOnError = false;
	/**
	 * @var callable
	 */
	public $when = null;
	/**
	 * @var \serviform\IValidateable
	 */
	protected $_parent = null;
	/**
	 * @var array
	 */
	protected $_elements = null;


	/**
	 * @param mixed $value
	 * @return bool
	 */
	abstract protected function vaidateValue($value, $element);


	/**
	 * @return bool
	 */
	public function validate(array $elements = null)
	{
		$return = true;
		$arElements = $this->getFieldsToValidate($elements);
		foreach ($arElements as $element) {
			if ($this->isValidationNeeded($element)) {
				$res = $this->validateElement($element);
				if ($res === false) $return = false;
			}
		}
		return $return;
	}

	/**
	 * @param array $fieldsToValidate
	 */
	protected function getFieldsToValidate(array $fieldsToValidate = null)
	{
		$return = [];
		$elementNames = $this->getElements();
		$parent = $this->getParent();
		foreach ($elementNames as $elementName) {
			if (
				!empty($fieldsToValidate)
				&& !in_array($elementName, $fieldsToValidate)
			){
				continue;
			}
			$element = $parent->getElement($elementName);
			if ($element === null) throw new Exception('Wrong validated field name');
			$return[] = $element;
		}
		return $return;
	}

	/**
	 * @param \serviform\IElement $element
	 * @return bool
	 */
	protected function isValidationNeeded(\serviform\IElement $element)
	{
		return
			(
				!$this->skipOnError
				|| !$element->getErrors()
			) && (
				!is_callable($this->when)
				|| call_user_func_array($this->when, [$this, $element])
			);
	}

	/**
	 * @param \serviform\IElement $element
	 * @return bool
	 */
	protected function validateElement(\serviform\IElement $element)
	{
		$value = $element->getValue();
		$res = $this->vaidateValue($value, $element);
		if ($res === false) {
			$element->addError($this->createErrorMessage($element));
		}
		return $res;
	}

	/**
	 * @param \serviform\IElement $element
	 * @return string
	 */
	protected function createErrorMessage(\serviform\IElement $element)
	{
		$replaces = [
			'#label#' => $element->getLabel(),
			'#value#' => $element->getValue(),
			'#name#' => $element->getName(),
		];<commit_msg>add skiponempty option to validator<commit_after><?php

namespace serviform;

/**
 * Base validator class
 */
abstract class ValidatorBase implements IValidator
{
	use \serviform\traits\Configurable;


	/**
	 * @var string
	 */
	public $message = 'Field "#label#" is invalid';
	/**
	 * @var bool
	 */
	public $skipOnError = false;
	/**
	 * @var bool
	 */
	public $skipOnEmpty = false;
	/**
	 * @var callable
	 */
	public $when = null;
	/**
	 * @var \serviform\IValidateable
	 */
	protected $_parent = null;
	/**
	 * @var array
	 */
	protected $_elements = null;


	/**
	 * @param mixed $value
	 * @return bool
	 */
	abstract protected function vaidateValue($value, $element);


	/**
	 * @return bool
	 */
	public function validate(array $elements = null)
	{
		$return = true;
		$arElements = $this->getFieldsToValidate($elements);
		foreach ($arElements as $element) {
			if ($this->isValidationNeeded($element)) {
				$res = $this->validateElement($element);
				if ($res === false) $return = false;
			}
		}
		return $return;
	}

	/**
	 * @param array $fieldsToValidate
	 */
	protected function getFieldsToValidate(array $fieldsToValidate = null)
	{
		$return = [];
		$elementNames = $this->getElements();
		$parent = $this->getParent();
		foreach ($elementNames as $elementName) {
			if (
				!empty($fieldsToValidate)
				&& !in_array($elementName, $fieldsToValidate)
			){
				continue;
			}
			$element = $parent->getElement($elementName);
			if ($element === null) throw new Exception('Wrong validated field name');
			$return[] = $element;
		}
		return $return;
	}

	/**
	 * @param \serviform\IElement $element
	 * @return bool
	 */
	protected function isValidationNeeded(\serviform\IElement $element)
	{
		return
			(
				!$this->skipOnError
				|| !$element->getErrors()
			) && (
				!$this->skipOnEmpty
				|| !$this->isEmpty($element->getValue())
			) && (
				!is_callable($this->when)
				|| call_user_func_array($this->when, [$this, $element])
			);
	}

	/**
	 * @param \serviform\IElement $element
	 * @return bool
	 */
	protected function validateElement(\serviform\IElement $element)
	{
		$value = $element->getValue();
		$res = $this->vaidateValue($value, $element);
		if ($res === false) {
			$element->addError($this->createErrorMessage($element));
		}
		return $res;
	}

	/**
	 * @param \serviform\IElement $element
	 * @return string
	 */
	protected function createErrorMessage(\serviform\IElement $element)
	{
		$replaces = [
			'#label#' => $element->getLabel(),
			'#value#' => $element->getValue(),
			'#name#' => $element->getName(),
		];
{'fraction_non_alphanumeric': 0.17252460333400282, 'fraction_numerical': 0.0, 'mean_word_length': 3.4623655913978495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '2446781', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1612, 'n_words': 343}