Skip to content

Instantly share code, notes, and snippets.

@sukhikh18
Created February 8, 2019 13:59
Show Gist options
  • Save sukhikh18/3994264f161dd700a4f951935be593d1 to your computer and use it in GitHub Desktop.
Save sukhikh18/3994264f161dd700a4f951935be593d1 to your computer and use it in GitHub Desktop.
<?php
use CommerceMLParser\Parser,
CommerceMLParser\Event;
require 'vendor/autoload.php';
class CommerceMLParser
{
public $arCategory = array();
public $arProduct = array();
public $arOffers = array();
static $parser;
private $filename = array(
'import' => array('vendor/karakum/commerceml/example/_import.xml'),
'offers' => array('vendor/karakum/commerceml/example/_offers.xml'),
);
function __construct()
{
static::$parser = Parser::getInstance();
}
static private function _parse( Array $filename )
{
if( isset($filename['import']) && is_array($filename['import']) ) {
foreach ($filename['import'] as $import) {
$parser->parse( $import );
}
}
if( isset($filename['offers']) && is_array($filename['offers']) ) {
foreach ($filename['offers'] as $offers) {
$parser->parse( $offers );
}
}
}
public function parseOwner()
{
$parser->addListener("OwnerEvent", function (Event\OwnerEvent $ownerEvent) {
// $owner = $ownerEvent->getPartner();
});
}
public function parseCategories()
{
$parser->addListener("CategoryEvent", function (Event\CategoryEvent $categoryEvent) {
/**
* @todo check this
*/
// $flatcategory = $categoryEvent->getFlatCategory()->fetch();
if( $category = $categoryEvent->getCategory() ) {
$_cat = $category->fetch();
$this->arCategory[ $category->getId() ] = current($_cat);
}
});
static::_parse( array('import' => $this->filename['import']) );
return $this->arCategory;
}
public function parseProducts()
{
$parser->addListener("ProductEvent", function (Event\ProductEvent $productEvent) {
$product = $productEvent->getProduct();
});
static::_parse( array('import' => $this->filename['import']) );
}
public function parseOffers()
{
$parser->addListener("OfferEvent", function (Event\OfferEvent $offerEvent) {
$offer = $offerEvent->getOffer();
});
static::_parse( $this->filename );
}
}
$CommerceML = new CommerceMLParser();
echo "<pre>";
var_dump( $CommerceML->parseCategories() );
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment