<commit_before><?php

namespace Brera\MagentoConnector\XmlBuilder;

require_once('XmlString.php');

class CatalogMerge
{
    /**
     * @var \XmlWriter
     */
    private $xml;

    private $started = false;

    public function __construct()
    {
        $this->xml = new \XMLWriter();
        $this->xml->openMemory();
        $this->xml->startDocument('1.0', 'UTF-8');
        $this->startXml();
    }

    /**
     * @param XmlString $product
     */
    public function addProduct(XmlString $product)
    {
        $this->xml->writeRaw($product->getXml());
    }

    /**
     * @return string
     */
    public function finish()
    {
        $this->endXml();

        return $this->getPartialXmlString();
    }

    /**
     * @return string
     */
    public function getPartialXmlString()
    {
        return $this->xml->flush();
    }

    private function startXml()
    {
        if ($this->started) {
            return;
        }
        $this->started = true;
        $this->xml->startElement('catalog');

        $attributes = [
            'xmlns'              => 'http://lizardsandpumpkins.com',
            'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
            'xsi:schemaLocation' => 'http://lizardsandpumpkins.com ../../schema/catalog.xsd',
        ];

        foreach ($attributes as $attribute => $value) {
            $this->xml->writeAttribute($attribute, $value);
        }

        $this->xml->startElement('products');
    }

    private function endXml()
    {
        $this->xml->endElement();
        $this->xml->endElement();
    }
}<commit_msg>Issue #90: Add category support for CatalogMerge and make sure the order is valid<commit_after><?php

namespace Brera\MagentoConnector\XmlBuilder;

require_once('XmlString.php');

class CatalogMerge
{
    /**
     * @var \XmlWriter
     */
    private $xml;

    private $started = false;

    private $mode = 'product';

    public function __construct()
    {
        $this->xml = new \XMLWriter();
        $this->xml->openMemory();
        $this->xml->startDocument('1.0', 'UTF-8');
        $this->startXml();
    }

    /**
     * @param XmlString $product
     */
    public function addProduct(XmlString $product)
    {
        if (!$this->isProductMode()) {
            throw new \RuntimeException(
                'Products can only be added during product mode, this means BEFORE any category is added.'
            );
        }
        $this->addXml($product);
    }

    public function addCategory(XmlString $category)
    {
        if (!$this->isCategoryMode()) {
            $this->setCategoryMode();
        }
        $this->addXml($category);
    }

    /**
     * @return string
     */
    public function finish()
    {
        $this->endXml();

        return $this->getPartialXmlString();
    }

    /**
     * @return string
     */
    public function getPartialXmlString()
    {
        return $this->xml->flush();
    }

    private function setCategoryMode()
    {
        $this->mode = 'category';
        $this->xml->endElement(); // end products
        $this->xml->startElement('listings');
    }

    public function isCategoryMode()
    {
        return $this->mode === 'category';
    }

    public function isProductMode()
    {
        return $this->mode === 'product';
    }

    private function startXml()
    {
        if ($this->started) {
            return;
        }
        $this->started = true;
        $this->xml->startElement('catalog');

        $attributes = [
            'xmlns'              => 'http://lizardsandpumpkins.com',
            'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
            'xsi:schemaLocation' => 'http://lizardsandpumpkins.com ../../schema/catalog.xsd',
        ];

        foreach ($attributes as $attribute => $value) {
            $this->xml->writeAttribute($attribute, $value);
        }

        $this->xml->startElement('products');
    }

    private function endXml()
    {
        $this->xml->endElement();
        $this->xml->endElement();
    }

    /**
     * @param XmlString $category
     */
    private function addXml(XmlString $category)
    {
        $this->xml->writeRaw($category->getXml());
    }

}
{'fraction_non_alphanumeric': 0.15693689435121722, 'fraction_numerical': 0.004254313401087214, 'mean_word_length': 1.8986301369863015, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 28}, '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': '3300695', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1215, 'n_words': 225}