<commit_before><?php
namespace wcf\system\form\builder\field;
use wcf\data\IStorableObject;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\form\builder\field\validation\IFormFieldValidationError;
use wcf\system\form\builder\field\validation\IFormFieldValidator;
use wcf\system\form\builder\TFormChildNode;
use wcf\system\form\builder\TFormElement;
use wcf\system\WCF;

/**
 * Abstract implementation of a form field.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Form\Builder\Field
 * @since	5.2
 */
abstract class AbstractFormField implements IFormField {
	use TFormChildNode;
	use TFormElement;
	
	/**
	 * `true` if this field is auto-focused and `false` otherwise
	 * @var	bool
	 */
	protected $__autoFocus = false;
	
	/**
	 * name of the object property this field represents
	 * @var	null|string
	 */
	protected $__objectProperty;
	
	/**
	 * `true` if this field has to be filled out and returns `false` otherwise
	 * @var	bool
	 */
	protected $__required = false;
	
	/**
	 * value of the field
	 * @var	mixed
	 */
	protected $__value;
	
	/**
	 * name of the template used to output this field
	 * @var	string
	 */
	protected $templateName;
	
	/**
	 * validation errors of this field
	 * @var	IFormFieldValidationError[]
	 */
	protected $validationErrors = [];
	
	/**
	 * field value validators of this field
	 * @var	IFormFieldValidator[]
	 */
	protected $validators = [];
	
	/**
	 * @inheritDoc
	 */
	public function addValidationError(IFormFieldValidationError $error) {
		if (empty($this->validationErrors)) {
			$this->addClass('formError');
		}
		
		$this->validationErrors[] = $error;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function addValidator(IFormFieldValidator $validator) {
		if ($this->hasValidator($validator->getId())) {
			throw new \InvalidArgumentException("Validator with id '{$validator->getId()}' already exists.");
		}
		
		$this->validators[$validator->getId()] = $validator;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function autoFocus($autoFocus = true) {
		$this->__autoFocus = $autoFocus;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function getHtml() {
		if ($this->templateName === null) {
			throw new \LogicException("\$templateName property has not been set.");
		}
		
		if ($this->requiresLabel() && $this->getLabel() === null) {
			throw new \UnexpectedValueException("Form field '{$this->getPrefixedId()}' requires a label.");
		}
		
		return WCF::getTPL()->fetch(
			$this->templateName,
			'wcf',
			array_merge($this->getHtmlVariables(), ['field' => $this]),
			true<commit_msg>Improve exception message<commit_after><?php
namespace wcf\system\form\builder\field;
use wcf\data\IStorableObject;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\form\builder\field\validation\IFormFieldValidationError;
use wcf\system\form\builder\field\validation\IFormFieldValidator;
use wcf\system\form\builder\TFormChildNode;
use wcf\system\form\builder\TFormElement;
use wcf\system\WCF;

/**
 * Abstract implementation of a form field.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Form\Builder\Field
 * @since	5.2
 */
abstract class AbstractFormField implements IFormField {
	use TFormChildNode;
	use TFormElement;
	
	/**
	 * `true` if this field is auto-focused and `false` otherwise
	 * @var	bool
	 */
	protected $__autoFocus = false;
	
	/**
	 * name of the object property this field represents
	 * @var	null|string
	 */
	protected $__objectProperty;
	
	/**
	 * `true` if this field has to be filled out and returns `false` otherwise
	 * @var	bool
	 */
	protected $__required = false;
	
	/**
	 * value of the field
	 * @var	mixed
	 */
	protected $__value;
	
	/**
	 * name of the template used to output this field
	 * @var	string
	 */
	protected $templateName;
	
	/**
	 * validation errors of this field
	 * @var	IFormFieldValidationError[]
	 */
	protected $validationErrors = [];
	
	/**
	 * field value validators of this field
	 * @var	IFormFieldValidator[]
	 */
	protected $validators = [];
	
	/**
	 * @inheritDoc
	 */
	public function addValidationError(IFormFieldValidationError $error) {
		if (empty($this->validationErrors)) {
			$this->addClass('formError');
		}
		
		$this->validationErrors[] = $error;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function addValidator(IFormFieldValidator $validator) {
		if ($this->hasValidator($validator->getId())) {
			throw new \InvalidArgumentException("Validator with id '{$validator->getId()}' already exists.");
		}
		
		$this->validators[$validator->getId()] = $validator;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function autoFocus($autoFocus = true) {
		$this->__autoFocus = $autoFocus;
		
		return $this;
	}
	
	/**
	 * @inheritDoc
	 */
	public function getHtml() {
		if ($this->templateName === null) {
			throw new \LogicException("\$templateName property has not been set for class '" . static::class . "'.");
		}
		
		if ($this->requiresLabel() && $this->getLabel() === null) {
			throw new \UnexpectedValueException("Form field '{$this->getPrefixedId()}' requires a label.");
		}
		
		return WCF::getTPL()->fetch(
			$this->templateName,
			'wcf',
			array_merge($this->getHtmlVariables(), ['field' => $this]),
			true
{'fraction_non_alphanumeric': 0.142084385142445, 'fraction_numerical': 0.003606202668589975, 'mean_word_length': 3.7736660929432015, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, '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': '6349443', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1688, 'n_words': 420}