<?
use SimpleXMLElementExtended as GlobalSimpleXMLElementExtended;

error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
$time_start = microtime(true);
class SimpleXMLElementExtended extends SimpleXMLElement
{
  /**
   * Add value as CData to a given XML node
   *
   * @param SimpleXMLElement $node SimpleXMLElement object representing the child XML node
   * @param string $value A text to add as CData
   * @return void
   */
  private function addCDataToNode(SimpleXMLElement $node, $value = '')
  {
    if ($domElement = dom_import_simplexml($node))
    {
      $domOwner = $domElement->ownerDocument;
      $domElement->appendChild($domOwner->createCDATASection("{$value}"));
    }
  }

  /**
   * Add child node with value as CData
   *
   * @param string $name The child XML node name to add
   * @param string $value A text to add as CData
   * @return SimpleXMLElement
   */
  public function addChildWithCData($name = '', $value = '')
  {
    $newChild = parent::addChild($name);
    if ($value) $this->addCDataToNode($newChild, "{$value}");
    return $newChild;
  }



  /**
   * Add value as CData to the current XML node
   *
   * @param string $value A text to add as CData
   * @return void
   */
  public function addCData($value = '')
  {
    $this->addCDataToNode($this, "{$value}");
  }
}

function xml_adopt($root, $new) {
  $node = $root->addChild($new->getName(), (string) $new);
  foreach($new->attributes() as $attr => $value) {
      $node->addAttribute($attr, $value);
  }
  foreach($new->children() as $ch) {
      xml_adopt($node, $ch);
  }
}

// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL,'https://www.strefakierowcy.pl/tools/market/google2.xml');
// $fp = fopen( 'google-feed.xml', 'w+' );
// curl_setopt( $ch, CURLOPT_FILE, $fp );
// curl_exec ( $ch );
// curl_close ( $ch );
// fclose( $fp );

unlink('google-feed-parsed.xml');

$xml_feed = new GlobalSimpleXMLElementExtended('<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"/>');
$xml_feed->addChild('title', 'strefakierowcy.pl');
$xml_feed->addChild('updated', date('Y-m-d'));

$xml = new XMLReader();
$xml->open('google-feed.xml');

$doc = new DOMDocument('1.0', 'utf-8');
$root = $doc->createElementNS('http://www.w3.org/2005/Atom', 'feed');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:g', 'http://base.google.com/ns/1.0');
$doc->appendChild($root);

$counter = 0;
$allowedNodeNames = [
    'title', 'g:id', 'link', 'g:image_link', 'g:google_product_category',
    'g:brand', 'g:gtin', 'g:mpn'
];

while ($xml->read() && $counter < 90000) {
    if ($xml->name == 'item') {
        $node = $xml->expand();
        $sourceItem = $doc->importNode($node, true);

        $item = $doc->createElement('item');

        foreach ($sourceItem->childNodes as $child) {
            $nodeName = ($child->prefix ? $child->prefix . ':' : '') . $child->localName;
            if (in_array($nodeName, $allowedNodeNames)) {
              $newChild = $doc->createElement($child->localName, $child->textContent);
              $item->appendChild($newChild);
            }
        }

        if ( $item->hasChildNodes())
          $root->appendChild($item);

        $counter++;
    }
}

$doc->formatOutput = true;
$xml_string = $doc->saveXML();
file_put_contents('google-feed-parsed.xml', $xml_string);

$xml->close();
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo '<p><b>Total Execution Time:</b> '.$execution_time.' s</p>';