<?php

declare (strict_types=1);
namespace OCA\News\Vendor\FeedIo\Standard;

use DOMDocument;
use DOMElement;
use OCA\News\Vendor\FeedIo\Formatter\XmlFormatter;
use OCA\News\Vendor\FeedIo\FormatterInterface;
use OCA\News\Vendor\FeedIo\StandardAbstract;
use OCA\News\Vendor\FeedIo\RuleSet;
use OCA\News\Vendor\FeedIo\Rule\ModifiedSince;
use OCA\News\Vendor\FeedIo\Rule\Title;
abstract class XmlAbstract extends StandardAbstract
{
    /**
     * Name of the node containing all the feed's items
     */
    public const ITEM_NODE = 'item';
    /**
     * This is for XML Standards
     */
    public const SYNTAX_FORMAT = 'Xml';
    protected ?RuleSet $feedRuleSet = null;
    protected ?RuleSet $itemRuleSet = null;
    abstract public function format(DOMDocument $document): DOMDocument;
    abstract public function getMainElement(DOMDocument $document): DOMElement;
    abstract public function buildFeedRuleSet(): RuleSet;
    abstract public function buildItemRuleSet(): RuleSet;
    public function getItemNodeName(): string
    {
        return static::ITEM_NODE;
    }
    public function getFormatter(): FormatterInterface
    {
        return new XmlFormatter($this);
    }
    public function getFeedRuleSet(): RuleSet
    {
        if (is_null($this->feedRuleSet)) {
            $this->feedRuleSet = $this->buildFeedRuleSet();
        }
        return $this->feedRuleSet;
    }
    public function getItemRuleSet(): RuleSet
    {
        if (is_null($this->itemRuleSet)) {
            $this->itemRuleSet = $this->buildItemRuleSet();
        }
        return $this->itemRuleSet;
    }
    public function getModifiedSinceRule(string $tagName): ModifiedSince
    {
        $rule = new ModifiedSince($tagName);
        $rule->setDefaultFormat($this->getDefaultDateFormat());
        $rule->setDateTimeBuilder($this->dateTimeBuilder);
        return $rule;
    }
    protected function buildBaseRuleSet(): RuleSet
    {
        $ruleSet = $ruleSet = new RuleSet();
        $ruleSet->add(new Title());
        return $ruleSet;
    }
}
