<commit_before><?php

namespace PeskyCMF\Traits;

use PeskyCMF\Config\CmfConfig;
use PeskyCMF\HttpCode;
use Swayok\Utils\Set;

trait DataValidationHelper {

    protected function validate($data, $rules, $messages = array(), $customAttributes = array()) {
        $messages = Set::flatten($messages);
        $validator = \Validator::make($data, $rules, $messages, $customAttributes);
        if ($validator->fails()) {
            return $this->sendValidationErrorsResponse($validator->getMessageBag()->toArray());
        }
        return true;
    }

    protected function getValidationErrorsResponseMessage() {
        return CmfConfig::transBase('.error.invalid_data_received');
    }

    protected function sendValidationErrorsResponse(array $errors) {
        abort(
            HttpCode::INVALID,
            json_encode($this->prepareDataForValidationErrorsResponse($errors), JSON_UNESCAPED_UNICODE)
        );<commit_msg>DataValidationHelper trait - added validateWithoutHalt method<commit_after><?php

namespace PeskyCMF\Traits;

use PeskyCMF\Config\CmfConfig;
use PeskyCMF\HttpCode;
use Swayok\Utils\Set;

trait DataValidationHelper {

    /**
     * Validate data and send Validation Errors Response (abort(HttpCode::INVALID)) if it is invalid
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     * @return bool
     */
    protected function validate(array $data, array $rules, array $messages = array(), array $customAttributes = array()) {
        $errors = $this->validateWithoutHalt($data, $rules, $messages, $customAttributes);
        return $errors === true ? true : $this->sendValidationErrorsResponse($errors);
    }

    /**
     * Validate data and returm errors array if it is invalid
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     * @return array|bool
     */
    protected function validateWithoutHalt(array $data, array $rules, array $messages = array(), array $customAttributes = array()) {
        $messages = Set::flatten($messages);
        $validator = \Validator::make($data, $rules, $messages, $customAttributes);
        if ($validator->fails()) {
            return $validator->getMessageBag()->toArray();
        }
        return true;
    }

    protected function getValidationErrorsResponseMessage() {
        return CmfConfig::transBase('.error.invalid_data_received');
    }

    protected function sendValidationErrorsResponse(array $errors) {
        abort(
            HttpCode::INVALID,
            json_encode($this->prepareDataForValidationErrorsResponse($errors), JSON_UNESCAPED_UNICODE)
        );
{'fraction_non_alphanumeric': 0.11616722160562339, 'fraction_numerical': 0.0, 'mean_word_length': 3.4400656814449917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, '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': '7579753', 'n_tokens_mistral': 729, 'n_tokens_neox': 726, 'n_words': 170}