<?php

namespace Planaday\Html\Tags\Form\GenericInput\InputOption;

use Planaday\Html\Tags\Form\SelectableDataInputFieldTag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class InputOption extends SelectableDataInputFieldTag {
	const TAG = 'option';
	const DEFAULT_TEMPLATE = 'option';
	const SELECTED_ATTRIBUTE = self::ATTRIBUTE_PREFIX . 'selected';

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry) {
		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$parser->setData('selected', ($this->isCurrentValueSetValue() || key_exists(self::SELECTED_ATTRIBUTE, $this->attributes)) ? 'selected' : '');
		return $parser->parse($this->getTemplate());
	}
}