<?php

namespace Planaday\Html\Tags\Form\FormTag;

use Planaday\Html\Tags\Tag;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class FormTag extends Tag {
	const TAG = 'form';
	const DEFAULT_TEMPLATE = 'form';
	const ACTION_ATTRIBUTE = 'data-action';
	const NAME_ATTRIBUTE = 'data-name';
	const NOVALIDATE_ATTRIBUTE = 'novalidate';

	/**
	 * @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->addStylesheet($this->getStylePath());

		$parser->setUnsafeData('action',
			array_key_exists(self::ACTION_ATTRIBUTE, $this->attributes)
				? 'action="' . $this->attributes[self::ACTION_ATTRIBUTE] . '"'
				: ''
		);
		$parser->setData('name', $this->attributes[self::NAME_ATTRIBUTE] ?? null);
		$parser->setData('novalidate', array_key_exists(self::NOVALIDATE_ATTRIBUTE, $this->attributes) ? 'novalidate' : '');

		return $parser->parse($this->getTemplate());
	}
}