Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <offers>
- <offer>
- <id>{~ bind product.id ~}</id>
- <availability>{~ bind product.availability ~}</availability>
- <availability_description>{~ bind product.availability_description ~}</availability_description>
- <promotional>{~ bind product.promotional ~}</promotional>
- <name>{~ bind name ~}</name>
- <price>{~ price netto PLN ~}</price>
- <shipping>{~ bind product.shipping ~}</shipping>
- <url>{~ bind product.url ~}</url>
- <category>{~ bind product._temp.category ~}</category>
- <subcategory>{~ call product.getCategoryId($0) ~}</subcategory>
- <description>{~ call product.parseDescription($0) ~}</description>
- <descriptionShort>{~ bind product.short_desc | gej ~}</descriptionShort>
- <producer>{~ call product.getProducerId($0) ~}</producer>
- <images>
- <image>
- <url>{~ call product.parseImage($0) ~}</url>
- </image>
- </images>
- <attributes>
- <attribute>{~ call product.parseAttribute($0) ~}</attribute>
- </attributes>
- </offer>
- </offers>
- ===================================================================================
- <?xml version="1.0" encoding="UTF-8" ?>
- <offers>
- <offer>
- <id>3</id>
- <availability>1</availability>
- <availability_description>
- <![CDATA[{JEZYK_DOSTEPNOSC}: <strong><span class="red">Produkt dostępny na miejscu.</span></strong><br /><span style="color: #999;">( produkt wycofany z produkcji, ograniczona dostępność )</span>]]></availability_description>
- <promotional>1</promotional>
- <name><![CDATA[Super Oferta: Portfel Ford BP WRC]]></name>
- <price>39</price>
- <shipping>
- <![CDATA[Koszt wysyłki to 20 zł przy wpłacie na konto, 25 zł za pobraniem. Przy zakupach za min. 250 zł i płatności na konto wysyłka gratis.]]></shipping>
- <url>http://www.rallyshop.pl/index.php?p3</url>
- <category><![CDATA[Odzież i gadżety]]></category>
- <subcategory><![CDATA[pozostałe]]></subcategory>
- <description>
- <![CDATA[Wieloprzegrodowy portfel Ford BP WRC w układzie pionowym. Posiada przegrodę z zamkiem błyskawicznym oraz z przegrodę z miejscami na zdjęcia z nadrukowanym logiem BP Ford World Rally Team.<br>]]></description>
- <descriptionShort><![CDATA[Portfel Ford BP WRC.]]></descriptionShort>
- <producer><![CDATA[Ford BP]]></producer>
- <images>
- <image>
- <url>http://rallyshop.pl/galerie/p/portfel-ford-bp-wrc_2.jpg</url>
- <thumb>http://rallyshop.pl/galerie/p/portfel-ford-bp-wrc_2_m.jpg</thumb>
- </image>
- </images>
- <attributes>
- </attributes>
- </offer>
- <offer>
- <id>4</id>
- <availability>1</availability>
- <availability_description>
- <![CDATA[{JEZYK_DOSTEPNOSC}: <strong><span class="red">Produkt dostępny na miejscu.</span></strong><br /><span style="color: #999;">( produkt wycofany z produkcji, ograniczona dostępność )</span>]]></availability_description>
- <promotional>1</promotional>
- <name><![CDATA[Super Oferta: Kurtka BP Ford]]></name>
- <price>299</price>
- <shipping>
- <![CDATA[Koszt wysyłki to 20 zł przy wpłacie na konto, 25 zł za pobraniem. Przy zakupach za min. 250 zł i płatności na konto wysyłka gratis.]]></shipping>
- <url>http://www.rallyshop.pl/index.php?p4</url>
- <category><![CDATA[Odzież i gadżety]]></category>
- <subcategory><![CDATA[kurtki]]></subcategory>
- <description>
- <![CDATA[Lekka kurtka na wiosnę lub jesień. Wodoodporna. Posiada naszyte logo zespołu oraz loga sponsorów.]]></description>
- <descriptionShort><![CDATA[Lekka kurtka na wiosnę lub jesień BP Ford WRC]]></descriptionShort>
- <producer><![CDATA[Ford BP]]></producer>
- <images>
- <image>
- <url>http://rallyshop.pl/galerie/k/kurtka-bp-ford_3.jpg</url>
- <thumb>http://rallyshop.pl/galerie/k/kurtka-bp-ford_3_m.jpg</thumb>
- </image>
- </images>
- <attributes>
- <attribute name="Wymiary własne">
- <![CDATA[<table style="text-align: left;" border="0" cellpadding="2" cellspacing="0"><tbody><tr><td align="undefined" valign="undefined"></td><td style="text-align: center; font-weight: bold;" valign="undefined">Szerokość (cm)</td><td style="text-align: center; font-weight: bold;" valign="undefined">Długość (cm)</td><td style="text-align: center; font-weight: bold;" valign="undefined">Długość rękawów (cm)</td></tr><tr><td style="text-align: center;" valign="undefined">S</td><td style="text-align: center;" valign="undefined">53</td><td style="text-align: center;" valign="undefined">69</td><td style="text-align: center;" valign="undefined">48</td></tr><tr><td style="text-align: center;" valign="undefined">M</td><td style="text-align: center;" valign="undefined">55</td><td style="text-align: center;" valign="undefined">72</td><td style="text-align: center;" valign="undefined">49</td></tr><tr><td style="text-align: center;" valign="undefined">L</td><td style="text-align: center;" valign="undefined">58</td><td style="text-align: center;" valign="undefined">73</td><td style="text-align: center;" valign="undefined">51</td></tr><tr><td style="text-align: center;" valign="undefined">XL</td><td style="text-align: center;" valign="undefined">60</td><td style="text-align: center;" valign="undefined">74</td><td style="text-align: center;" valign="undefined">53</td></tr><tr><td style="text-align: center;" valign="undefined">XXL</td><td style="text-align: center;" valign="undefined">62</td><td style="text-align: center;" valign="undefined">77</td><td style="text-align: center;" valign="undefined">53</td></tr></tbody></table>]]></attribute>
- </attributes>
- </offer>
- </offers>
- ======================================================
- <?php
- class StackXMLReader extends XMLReader
- {
- private $stack = [];
- public function process(callable $callback)
- {
- while ($this->read()) {
- if ($this->nodeType === XMLReader::ELEMENT and $this->isEmptyElement === false) {
- array_push($this->stack, $this->name);
- $callback($this);
- } elseif ($this->nodeType === XMLReader::END_ELEMENT) {
- array_pop($this->stack);
- }
- }
- }
- public function getStack(): array
- {
- return $this->stack;
- }
- }
- abstract class XMLMapper
- {
- private $xmlReader;
- public function __construct()
- {
- $this->xmlReader = new ExtendedXMLReader();
- $this->registerMapping();
- }
- public function process(string $filePath)
- {
- $this->xmlReader->open($filePath);
- $this->xmlReader->parse();
- $this->xmlReader->close();
- }
- protected function registerCallbacksFromMappingFile(string $filePath)
- {
- $xml = new StackXMLReader();
- $xml->open($filePath);
- $xml->process(function (StackXMLReader $xml) {
- $template = trim((string)$xml->readInnerXml());
- if ($template and preg_match('/^{~(.*?)~}$/', $template, $matches)) {
- $xpath = '/'.implode('/', $xml->getStack());
- $parameters = array_map('trim', explode(' ', trim($matches[1])));
- $method = array_shift($parameters);
- $callback = [$this, $method];
- if (is_callable($callback) === false) {
- throw new UnexpectedValueException(sprintf(
- 'Mapping callback (%s) does not exists in XMLMapping class', $method
- ));
- }
- $this->registerCallback($xpath, XMLReader::ELEMENT, $callback, $parameters);
- }
- });
- $xml->close();
- }
- protected function registerCallback(string $xpath, int $nodeType, callable $callback, array $parameters = [])
- {
- $this->xmlReader->registerCallback($xpath, function (ExtendedXMLReader $xml) use ($callback, $parameters) {
- call_user_func($callback, $xml, ...$parameters);
- return true;
- }, $nodeType);
- }
- abstract protected function registerMapping();
- }
- class CurrentXMLMapper extends XMLMapper
- {
- public $product = null;
- protected function registerMapping()
- {
- $this->registerCallbacksFromMappingFile('template.xml');
- $this->registerCallback('/offers/offer', XMLReader::ELEMENT, function () {
- $this->product = new Product();
- });
- $this->registerCallback('/offers/offer', XMLReader::END_ELEMENT, function () {
- echo $this->product->name.PHP_EOL;
- });
- }
- protected function bind(ExtendedXMLReader $xml, string $expression)
- {
- $this->product->{$expression} = trim((string)$xml->readString());
- }
- protected function call(ExtendedXMLReader $xml, string $expression)
- {
- $this->product->{$expression} = trim((string)$xml->readString());
- }
- protected function price(ExtendedXMLReader $xml, string $type, string $currency)
- {
- $this->product->price[] = [
- 'type' => $type,
- 'currency' => $currency,
- $type => trim((string)$xml->readString())
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement