<commit_before><?php namespace Cviebrock\LaravelResources;

use Cviebrock\LaravelResources\Contracts\ResourceDescriptor;
use Cviebrock\LaravelResources\Exceptions\FormActionNotDefined;
use Cviebrock\LaravelResources\Exceptions\FormResourcesNotSpecified;
use Illuminate\Support\Collection;
use Illuminate\Support\Contracts\MessageProviderInterface;
use Illuminate\Support\MessageBag;
use Resource;
use Validator;
use View;

class FormBuilder implements MessageProviderInterface {

	protected $template = 'resources::form';

	/**
	 * @var array
	 */
	protected $errors;

	/**
	 * @var array
	 */
	protected $validation;

	/**
	 * @var Collection
	 */
	protected $resources;

	/**
	 * @var array
	 */
	protected $attributes;

	/**
	 * @var string
	 */
	protected $method;

	/**
	 * @var array
	 */
	protected $validateCollection = [];


	/**
	 * @var string
	 */
	protected $action;

	public function __construct($resources, $formAttributes) {

		$this->errors = [];
		$this->loadResourcesWithDescriptors($resources);
		$this->loadFormAttributes($formAttributes);
		$this->arrangeFormOrder();
	}

	/**
	 * Add validation rules
	 *
	 * @param $field
	 * @param $rule
	 */
	public function addValidation($field, $rule) {

		$this->validation[$field] = $rule;
	}

	/**
	 * The error messages.
	 *
	 * @return MessageBag
	 */
	public function errors() {

		if ($this->errors instanceof MessageBag) {
			return $this->errors;
		}

		return $this->errors = new MessageBag($this->errors);
	}

	/**
	 * Get the messages for the instance.
	 *
	 * @return \Illuminate\Support\MessageBag
	 */
	public function getMessageBag() {

		return $this->errors();
	}

	/**
	 * Check if form validation passes
	 *
	 * @return bool
	 */
	public function passes() {

		return $this->runValidation();
	}

	/**
	 * Check if form validation fails
	 *
	 * @return bool
	 */
	public function fails() {

		return !$this->passes();
	}

	/**
	 * Render a form for the given resources
	 *
	 * @return mixed
	 * @throws FormResourcesNotSpecified
	 */
	public function renderForm() {

		return View::make($this->template, $this->getFormData())->render();
	}

	/**
	 * Perform validation on the values passed in.
	 *
	 * @param $resources
	 */
	public function validate($resources) {

		$this->validateCollection = $resources;
	}

	/**
	 * Render the form if outputting this class
	 *
	 * @return mixed
	 */
	public function __toString() {

		return $this->renderForm();
	}

	/**
	 * Runs current form validation
	 */
	protected function runValidation() {

		$validation = Validator::make($this->validation, $this->validateCollection);

		if ($invalid = $validation->fails()) {
			$this->errors = $validation->messages->getMessageBag();
		}

		return !$invalid;
	}

	/**
	 * Load validation rules from a resource
	 *
	 * @param $resourceKey
	 * @param ResourceDescriptor $resource
	 */
	private function loadValidationFromResource($resourceKey, ResourceDescriptor $resource) {

		if ($validation = $resource->validate()) {
			$this->addValidation($resourceKey, $validation);<commit_msg>wrong method for getting the message errors bag<commit_after><?php namespace Cviebrock\LaravelResources;

use Cviebrock\LaravelResources\Contracts\ResourceDescriptor;
use Cviebrock\LaravelResources\Exceptions\FormActionNotDefined;
use Cviebrock\LaravelResources\Exceptions\FormResourcesNotSpecified;
use Illuminate\Support\Collection;
use Illuminate\Support\Contracts\MessageProviderInterface;
use Illuminate\Support\MessageBag;
use Resource;
use Validator;
use View;

class FormBuilder implements MessageProviderInterface {

	protected $template = 'resources::form';

	/**
	 * @var array
	 */
	protected $errors;

	/**
	 * @var array
	 */
	protected $validation;

	/**
	 * @var Collection
	 */
	protected $resources;

	/**
	 * @var array
	 */
	protected $attributes;

	/**
	 * @var string
	 */
	protected $method;

	/**
	 * @var array
	 */
	protected $validateCollection = [];


	/**
	 * @var string
	 */
	protected $action;

	public function __construct($resources, $formAttributes) {

		$this->errors = [];
		$this->loadResourcesWithDescriptors($resources);
		$this->loadFormAttributes($formAttributes);
		$this->arrangeFormOrder();
	}

	/**
	 * Add validation rules
	 *
	 * @param $field
	 * @param $rule
	 */
	public function addValidation($field, $rule) {

		$this->validation[$field] = $rule;
	}

	/**
	 * The error messages.
	 *
	 * @return MessageBag
	 */
	public function errors() {

		if ($this->errors instanceof MessageBag) {
			return $this->errors;
		}

		return $this->errors = new MessageBag($this->errors);
	}

	/**
	 * Get the messages for the instance.
	 *
	 * @return \Illuminate\Support\MessageBag
	 */
	public function getMessageBag() {

		return $this->errors();
	}

	/**
	 * Check if form validation passes
	 *
	 * @return bool
	 */
	public function passes() {

		return $this->runValidation();
	}

	/**
	 * Check if form validation fails
	 *
	 * @return bool
	 */
	public function fails() {

		return !$this->passes();
	}

	/**
	 * Render a form for the given resources
	 *
	 * @return mixed
	 * @throws FormResourcesNotSpecified
	 */
	public function renderForm() {

		return View::make($this->template, $this->getFormData())->render();
	}

	/**
	 * Perform validation on the values passed in.
	 *
	 * @param $resources
	 */
	public function validate($resources) {

		$this->validateCollection = $resources;
	}

	/**
	 * Render the form if outputting this class
	 *
	 * @return mixed
	 */
	public function __toString() {

		return $this->renderForm();
	}

	/**
	 * Runs current form validation
	 */
	protected function runValidation() {

		$validation = Validator::make($this->validation, $this->validateCollection);

		if ($invalid = $validation->fails()) {
			$this->errors = $validation->errors();
		}

		return !$invalid;
	}

	/**
	 * Load validation rules from a resource
	 *
	 * @param $resourceKey
	 * @param ResourceDescriptor $resource
	 */
	private function loadValidationFromResource($resourceKey, ResourceDescriptor $resource) {

		if ($validation = $resource->validate()) {
			$this->addValidation($resourceKey, $validation);
{'fraction_non_alphanumeric': 0.13970227384263045, 'fraction_numerical': 0.0, 'mean_word_length': 3.7066974595842956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '5768364', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1761, 'n_words': 436}