<commit_before><?php

/**
 * This file is part of the Nextras community extensions of Nette Framework
 *
 * @license    MIT
 * @link       https://github.com/nextras
 * @author     <NAME> (http://davidgrudl.com)
 * @author     <NAME>
 */

namespace Nextras\Forms\Controls;

use Nette;
use Nette\Utils\Html;



/**
 * Set of options.
 *
 * @author     <NAME>
 *
 * @property   array $items
 */
class OptionList extends Nette\Forms\Controls\BaseControl
{
	/** @var array */
	protected $items = array();

	/** @var IControl */
	protected $inputPrototype;



	/**
	 * @param  string  label
	 * @param  array   options from which to choose
	 */
	public function __construct($label = NULL, array $items = NULL)
	{
		parent::__construct($label);
		$this->control->type = 'radio';
		if ($items !== NULL) {
			$this->setItems($items);
		}
	}



	/**
	 * @inheritDoc
	 */
	public function setValue($value)
	{
		$this->value = is_scalar($value) && isset($this->items[$value]) ? $value : NULL;
		return $this;
	}



	/**
	 * Returns selected radio value.
	 * @return mixed
	 */
	public function getValue()
	{
		return $this->value;
	}



	/**
	 * Has been any radio button selected?
	 * @return bool
	 */
	public function isFilled()
	{
		return $this->getValue() !== NULL;
	}



	/**
	 * Sets options from which to choose.
	 * @param  array
	 * @return RadioList  provides a fluent interface
	 */
	public function setItems(array $items)
	{
		$this->items = $items;
		return $this;
	}



	/**
	 * Returns options from which to choose.
	 * @return array
	 */
	public function getItems()
	{
		return $this->items;
	}



	/**
	 * Generates control's HTML element.
	 *
	 * @return Nette\Utils\Html
	 */
	public function getControl()
	{
		$container = Html::el();
		foreach ($this->items as $key => $val) {
			$label = $this->getLabelItem($key);

			$label->class[] = 'radio';
			$label->add($this->getControlItem($key));

			$container->add((string) $label);
		}
		return $container;
	}



	public function getControlItem($key)
	{
		$control = clone $this->getInputPrototype();
		$control->id .= '-' . $key;
		$control->checked = (string) $key === $this->getValue();
		$control->value = $key;
		return $control;
	}



	/**
	 * Generates label's HTML element.
	 * @param  string
	 * @return void
	 */
	public function getLabel($caption = NULL)
	{
		$label = parent::getLabel($caption);
		$label->for = NULL;
		return $label;
	}



	public function getLabelItem($key, $caption = NULL)
	{
		$label = parent::getLabel();
		$label->setText($caption ?: $this->translate($this->items[$key]));
		$label->for .= '-' . $key;
		return $label;
	}



	protected function getInputPrototype()
	{
		if ($this->inputPrototype) {
			return $this->inputPrototype;
		}

		return $this->inputPrototype = parent::getControl();
	}

}
<commit_msg>OptionList: fixed order of text-label and input tag<commit_after><?php

/**
 * This file is part of the Nextras community extensions of Nette Framework
 *
 * @license    MIT
 * @link       https://github.com/nextras
 * @author     <NAME> (http://davidgrudl.com)
 * @author     <NAME>
 */

namespace Nextras\Forms\Controls;

use Nette;
use Nette\Utils\Html;



/**
 * Set of options.
 *
 * @author     <NAME>
 *
 * @property   array $items
 */
class OptionList extends Nette\Forms\Controls\BaseControl
{
	/** @var array */
	protected $items = array();

	/** @var IControl */
	protected $inputPrototype;



	/**
	 * @param  string  label
	 * @param  array   options from which to choose
	 */
	public function __construct($label = NULL, array $items = NULL)
	{
		parent::__construct($label);
		$this->control->type = 'radio';
		if ($items !== NULL) {
			$this->setItems($items);
		}
	}



	/**
	 * @inheritDoc
	 */
	public function setValue($value)
	{
		$this->value = is_scalar($value) && isset($this->items[$value]) ? $value : NULL;
		return $this;
	}



	/**
	 * Returns selected radio value.
	 * @return mixed
	 */
	public function getValue()
	{
		return $this->value;
	}



	/**
	 * Has been any radio button selected?
	 * @return bool
	 */
	public function isFilled()
	{
		return $this->getValue() !== NULL;
	}



	/**
	 * Sets options from which to choose.
	 * @param  array
	 * @return RadioList  provides a fluent interface
	 */
	public function setItems(array $items)
	{
		$this->items = $items;
		return $this;
	}



	/**
	 * Returns options from which to choose.
	 * @return array
	 */
	public function getItems()
	{
		return $this->items;
	}



	/**
	 * Generates control's HTML element.
	 *
	 * @return Nette\Utils\Html
	 */
	public function getControl()
	{
		$container = Html::el();
		foreach ($this->items as $key => $val) {
			$label = $this->getLabelItem($key);

			$label->class[] = $this->control->type;
			$text = $label->getText();
			$label->setText('');
			$label->add($this->getControlItem($key));
			$label->add($text);

			$container->add((string) $label);
		}
		return $container;
	}



	public function getControlItem($key)
	{
		$control = clone $this->getInputPrototype();
		$control->id .= '-' . $key;
		$control->checked = (string) $key === $this->getValue();
		$control->value = $key;
		return $control;
	}



	/**
	 * Generates label's HTML element.
	 * @param  string
	 * @return void
	 */
	public function getLabel($caption = NULL)
	{
		$label = parent::getLabel($caption);
		$label->for = NULL;
		return $label;
	}



	public function getLabelItem($key, $caption = NULL)
	{
		$label = parent::getLabel();
		$label->setText($caption ?: $this->translate($this->items[$key]));
		$label->for .= '-' . $key;
		return $label;
	}



	protected function getInputPrototype()
	{
		if ($this->inputPrototype) {
			return $this->inputPrototype;
		}

		return $this->inputPrototype = parent::getControl();
	}

}

{'fraction_non_alphanumeric': 0.1700348432055749, 'fraction_numerical': 0.0, 'mean_word_length': 3.1124641833810887, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 72, 'https://': 2, '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': '914187', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1884, 'n_words': 463}