ast
13 years ago
cache
13 years ago
XmlImportConfig.php
13 years ago
XmlImportCsvParse.php
13 years ago
XmlImportException.php
13 years ago
XmlImportParser.php
13 years ago
XmlImportReaderInterface.php
13 years ago
XmlImportStringReader.php
13 years ago
XmlImportTemplate.php
13 years ago
XmlImportTemplateCodeGenerator.php
13 years ago
XmlImportTemplateParser.php
13 years ago
XmlImportTemplateScanner.php
13 years ago
XmlImportToken.php
13 years ago
XmlImportParser.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com> |
| 4 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 5 | * @package General |
| 6 | */ |
| 7 | |
| 8 | require_once dirname(__FILE__) . '/XmlImportTemplate.php'; |
| 9 | |
| 10 | /** |
| 11 | * Is used to parse XML using specified template and root node |
| 12 | */ |
| 13 | class XmlImportParser { |
| 14 | /** |
| 15 | * SimpleXmlElement instance for the xml file |
| 16 | * |
| 17 | * @var SimpleXMLElement |
| 18 | */ |
| 19 | protected $xml; |
| 20 | |
| 21 | /** |
| 22 | * XPath expression for selecting the root node of the record |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $rootNodeXPath; |
| 27 | |
| 28 | /** |
| 29 | * Path to cached template |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $cachedTemplate; |
| 34 | |
| 35 | /** |
| 36 | * Creates new parser instance |
| 37 | * |
| 38 | * @param string $xml |
| 39 | * @param string $rootNodeXPath XPath for the record root node |
| 40 | * @param string $cachedTemplate path to cached template |
| 41 | * @param bool $isURL whether xml is URL or path |
| 42 | */ |
| 43 | public function __construct($xml, $rootNodeXPath, $cachedTemplate, $isURL = false) |
| 44 | { |
| 45 | if ($isURL) { |
| 46 | $xml = file_get_contents($xml); |
| 47 | } |
| 48 | |
| 49 | // FIX: remove default namespace because libxml xpath implementation doesn't handle it properly |
| 50 | $xml and $xml = preg_replace('%xmlns\s*=\s*([\'"]).*\1%sU', '', $xml); |
| 51 | |
| 52 | $this->xml = new SimpleXMLElement($xml); |
| 53 | $this->rootNodeXPath = $rootNodeXPath; |
| 54 | $this->cachedTemplate = $cachedTemplate; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets the parser results for all records or |
| 59 | * the part of them if $start and $count parameters are passed |
| 60 | * |
| 61 | * @param array[optional] $records Sequence numbers of records to import (first record corresponds to 1) |
| 62 | * @return array |
| 63 | */ |
| 64 | public function parse($records = array()) |
| 65 | { |
| 66 | empty($records) or is_array($records) or $records = array($records); |
| 67 | |
| 68 | $result = array(); |
| 69 | |
| 70 | $rootNodes = $this->xml->xpath($this->rootNodeXPath); |
| 71 | if ($rootNodes === false) |
| 72 | throw new XmlImportException('Invalid root node XPath \'' . $this->rootNodeXPath . '\' specified'); |
| 73 | |
| 74 | for ($i = 0; $i < count($rootNodes); $i++) { |
| 75 | if (empty($records) or in_array(($i + 1) + 10 * $_SESSION['pmxi_import']['step'], $records)) { |
| 76 | $rootNode = $rootNodes[$i]; |
| 77 | $template = new XmlImportTemplate($rootNode, $this->cachedTemplate); |
| 78 | $result[] = $template->parse(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $result; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates new parser instance for text template specified |
| 87 | * |
| 88 | * @param string $xml |
| 89 | * @param string $rootNodeXPath XPath for the record root node |
| 90 | * @param string $template template |
| 91 | * @param string &$file path to the cached template |
| 92 | * @return XmlImportParser |
| 93 | */ |
| 94 | public static function factory($xml, $rootNodeXPath, $template, &$file = NULL) |
| 95 | { |
| 96 | |
| 97 | $scanner = new XmlImportTemplateScanner(); |
| 98 | $tokens = $scanner->scan(new XmlImportStringReader($template)); |
| 99 | $t_parser = new XmlImportTemplateParser($tokens); |
| 100 | $tree = $t_parser->parse(); |
| 101 | $codegenerator = new XmlImportTemplateCodeGenerator($tree); |
| 102 | $file = $codegenerator->generate(); |
| 103 | |
| 104 | return new self($xml, $rootNodeXPath, $file); |
| 105 | } |
| 106 | } |