Files
interblue.pl/modules/pshowimporter/classes/PShow_Xml_Parser.php
2024-10-25 14:16:28 +02:00

237 lines
5.4 KiB
PHP

<?php
/**
* File from http://PrestaShow.pl
*
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @authors PrestaShow.pl <kontakt@prestashow.pl>
* @copyright 2015 PrestaShow.pl
* @license http://PrestaShow.pl/license
*/
class PShow_Xml_Parser
{
/**
* @var SimpleXMLElement
*/
private $xml;
/**
*
* @var array of SimpleXMLElement
*/
protected static $instances = array();
/**
* Get instance of SimpleXMLElement
*
* @param string $filepath
* @return SimpleXMLElement
*/
public static function getInstance($filepath = null)
{
if ($filepath === null) {
if (count(self::$instances) == 0) {
return false;
}
return end(self::$instances);
}
$md5 = md5($filepath);
if (!array_key_exists($md5, self::$instances)) {
self::$instances[$md5] = new PShow_Xml_Parser();
self::$instances[$md5]->filepath = $filepath;
}
return self::$instances[$md5];
}
public function remove_utf8_bom($text)
{
$bom = pack('H*', 'EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
/**
* @return array
*/
public function getElementsByPath($path)
{
if (is_array($path)) {
$path = implode(',', $path) . ',';
}
$path = substr(str_replace(',', '/', $path), 0, strlen($path) - 1);
$tmp = PShow_XMLReaderToSimpleXmlObjects::getInstance($this->filepath)->xpath($path);
return $tmp;
}
/**
*
* @param string $path
* @param SimpleXMLElement $object
* @return array
*/
public function getValueByPathAndObject($path, $object)
{
if (!is_object($object)) {
return array();
}
return $object->xpath($path);
}
/**
* @return SimpleXMLElement
*/
public function countElementsByPath($path)
{
if (is_array($path)) {
$path = implode(',', $path) . ',';
}
$path = substr(str_replace(',', '/', $path), 0, strlen($path) - 1);
return PShow_XMLReaderToSimpleXmlObjects::getInstance($this->filepath)
->xpath($path, 0, null, true);
}
/**
* @return SimpleXMLElement
*/
public function getObjectsByPath($path)
{
if ($path === null) {
return array();
}
if (is_string($path)) {
$path = explode(',', $path);
}
if (!is_string($path)) {
$path = implode('/', $path);
}
return PShow_XMLReaderToSimpleXmlObjects::getInstance($this->filepath)->xpath($path);
}
/**
* @return SimpleXMLElement
*/
public function getElementByPath($path, $n = null)
{
if (is_string($path)) {
$path = explode(',', $path);
}
if (!is_string($path)) {
$path = implode('/', $path);
}
if (substr($path, -1, 1) == '/') {
$path = substr($path, 0, strlen($path) - 1);
}
$arr = PShow_XMLReaderToSimpleXmlObjects::getInstance($this->filepath)->xpath($path, (int) $n, 1);
$first = reset($arr);
if ($first) {
return new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><data>" . $first->asXML() . "</data>");
}
return new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><data>no data</data>");
}
/**
* Get all tags
*
* @return array
*/
public function getAllTags()
{
return PShow_XMLReaderToSimpleXmlObjects::getInstance($this->filepath)->getAllTags();
}
/**
* Get n object
*
* @param int $n
* @return boolean
*/
public function getObject($n = 0)
{
}
public function getObjects($objects_path, $start_from = 0, $rows_count = 3)
{
$rows = array();
$objects = $this->getElementsByPath($objects_path);
$i = 0;
foreach ($objects as $object) {
++$i;
if ($i >= $start_from) {
$rows[] = $this->getAllValuesFromObject($object);
}
if (count($rows) >= $rows_count) {
break;
}
}
return $rows;
}
/**
* Get all importable values from SimpleXMLElement
*
* @param SimpleXMLElement $object
* @param array $results
* @return array
*/
public function getAllValuesFromObject($object, $with_path = false, &$results = null, $prevPath = null, $isAttr = false)
{
if (!is_object($object)) {
return;
}
$fTime = false;
if ($results === null) {
$fTime = true;
$results = array();
foreach ($object->attributes() as $k => $v) {
$results[] = md5($prevPath . '@' . $k . $v);
}
}
foreach ($object as $k => $v) {
$this->getAllValuesFromObject($v->attributes(), true, $results, $prevPath . $k . '/', true);
if ($with_path) {
$results[] = md5($prevPath . (($isAttr) ? '@' : '') . $k . $v);
} else {
$results[] = (string) $v;
}
$this->getAllValuesFromObject($v, true, $results, $prevPath . $k . '/');
}
return $results;
}
}