<commit_before><?php
/**
 * @file
 * Validation service.
 *
 * All DecimaERP code is copyright by the original authors and released under the GNU Aferro General Public License version 3 (AGPLv3) or later.
 * See COPYRIGHT and LICENSE.
 */

namespace App\Kwaai\System\Services\Validation;

use Illuminate\Validation\Factory;

abstract class AbstractLaravelValidator implements ValidableInterface {

	/**
	 * Validation data key => value array
	 *
	 * @var Array
	 */
	protected $data = array();

	/**
	 * Validation errors
	 *
	 * @var Array
	 */
	protected $errors = array();

	/**
	 * Validation rules
	 *
	 * @var Array
	 */
	protected $rules = array();

	/**
	 * Custom validation messages
	 *
	 * @var Array
	 */
	protected $messages = array();

	/**
	 * Database Connection Name
	 *
	 * @var string
	 */
	protected $databaseConnectionName;

	/**
	 * Set data to validate
	 *
	 * @return \Impl\Service\Validation\AbstractLaravelValidator
	 */
	public function with(array $data)
	{
		$this->data = $data;

		return $this;
	}

	/**
	 * Test if validation passes or fails
	 *
	 * @return Boolean
	 */
	public function passes()
	{
		$Validator = $this->Validator->make(
			$this->data,
			$this->rules,
			$this->messages
		);

		if( $Validator->fails() )
		{
			$this->errors = $Validator->messages();

			return false;
		}

		return true;
	}

	/**
	 * Test if validation fails or passes
	 *
	 * @return Boolean
	 */
	public function fails()
	{
		$Validator = $this->Validator->make(
			$this->data,
			$this->rules,
			$this->messages
		);

		if( $Validator->fails() )
		{
			$this->errors = $Validator->messages();

			return true;
		}

		return false;
	}

	/**
	 * Return errors, if any
	 *
	 * @return object
	 */
	public function errors()
	{
		return $this->errors;
	}

	/**
	 * Organize an array with field and its corresponding validation message
	 *
	 * @return array
	 */
	function singleMessageStringByField()
	{
		$validations = array();

		foreach ($this->errors->toArray() as $key => $messages)
		{
			$validations = array_add($validations, $key, implode($messages, '<br>'));
		}

		return $validations;
	}

	/**
	 * Open database transactions
	 *
	 * @param boolean $openTransaction
	 * @param string $databaseConnectionName
	 *
	 * @return array
	 */
	function beginTransaction($openTransaction, $databaseConnectionName)
	{
		if(!$openTransaction)
		{
			return;<commit_msg>Issue #1<commit_after><?php
/**
 * @file
 * Validation service.
 *
 * All DecimaERP code is copyright by the original authors and released under the GNU Aferro General Public License version 3 (AGPLv3) or later.
 * See COPYRIGHT and LICENSE.
 */

namespace App\Kwaai\System\Services\Validation;

use Illuminate\Validation\Factory;

abstract class AbstractLaravelValidator implements ValidableInterface {

	/**
	 * Validation data key => value array
	 *
	 * @var Array
	 */
	protected $data = array();

	/**
	 * Validation errors
	 *
	 * @var Array
	 */
	protected $errors = array();

	/**
	 * Validation rules
	 *
	 * @var Array
	 */
	protected $rules = array();

	/**
	 * Custom validation messages
	 *
	 * @var Array
	 */
	protected $messages = array();

	/**
	 * Database Connection Name
	 *
	 * @var string
	 */
	protected $databaseConnectionName;

	/**
	 * Set data to validate
	 *
	 * @return \Impl\Service\Validation\AbstractLaravelValidator
	 */
	public function with(array $data)
	{
		$this->data = $data;

		return $this;
	}

	/**
	 * Test if validation passes or fails
	 *
	 * @return Boolean
	 */
	public function passes()
	{
		$Validator = $this->Validator->make(
			$this->data,
			$this->rules,
			$this->messages
		);

		if( $Validator->fails() )
		{
			$this->errors = $Validator->messages();

			return false;
		}

		return true;
	}

	/**
	 * Test if validation fails or passes
	 *
	 * @return Boolean
	 */
	public function fails()
	{
		$Validator = $this->Validator->make(
			$this->data,
			$this->rules,
			$this->messages
		);

		if( $Validator->fails() )
		{
			$this->errors = $Validator->messages();

			return true;
		}

		return false;
	}

	/**
	 * Return errors, if any
	 *
	 * @return object
	 */
	public function errors()
	{
		return $this->errors;
	}

	/**
	 * Organize an array with field and its corresponding validation message
	 *
	 * @return array
	 */
	function singleMessageStringByField()
	{
		$validations = array();

		foreach ($this->errors->toArray() as $key => $messages)
		{
			$validations = array_add($validations, $key, implode($messages, '<br>'));
		}

		return $validations;
	}

	/**
	 * Open database transactions
	 *
	 * @param boolean $openTransaction
	 * @param string $databaseConnectionName
	 *
	 * @return array
	 */
	function beginTransaction($openTransaction, $databaseConnectionName = null)
	{
		if(!$openTransaction)
		{
			return;
{'fraction_non_alphanumeric': 0.1396905060644082, 'fraction_numerical': 0.0010455876202425764, 'mean_word_length': 3.0465313028764807, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, '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': '1462929', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1435, 'n_words': 393}