XmlImportTemplate.php 1.64 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?php
/**
 * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com>
 * @author Pavel Kulbakin <p.kulbakin@gmail.com>
 * @package General
 */

require_once dirname(__FILE__) . '/XmlImportConfig.php';

/**
 * Represents a template
 */
class XmlImportTemplate {
	/**
	 * Root element of the record that is being parsed
	 *
	 * @var SimpleXMLElement
	 */
	protected $xml;

	/**
	 * file name of the cached template
	 *
	 * @var string
	 */
	protected $cachedTemplate;

    protected $trimValues = TRUE;

	/**
	 * Creates new instance
	 *
	 * @param SimpleXmlElement $xml
	 * @param string $cachedTemplate
	 */
	public function __construct($xml, $cachedTemplate)
	{
		$this->xml = $xml;
		$this->cachedTemplate = $cachedTemplate;
        $this->trimValues = apply_filters('wp_all_import_is_trim_parsed_data', $this->trimValues);
	}

	/**
	 * Parses a template using {@see $this->xml}
	 *
	 * @return string
	 */
	public function parse()
	{			
		
		ob_start();
		$err_lvl = error_reporting(E_ALL);
		include $this->cachedTemplate;
		error_reporting($err_lvl);
		
		return ob_get_clean();
	}

	/**
	 * Get the value by XPath expression
	 *
	 * @param SimpleXmlElement $xpath XPath result
	 * @return mixed
	 */
	protected function getValue($xpath = array())
	{
				
		if (is_array($xpath) && count($xpath) > 0) {
			$result = array();
			foreach ($xpath as $xp) { // concatenate multiple elements into 1 string
				ob_start();
				echo $xp;
				$result[] = $this->trimValues ? trim(ob_get_clean()) : ob_get_clean();
			}			
			return implode(XmlImportConfig::getInstance()->getMultiGlue(), $result);
		} else {
			// return null if nothing found
			return null;
		}
	}
}