initial commit
[namibia] / module / Utility / src / Utility / Import / Xml.php
1 <?php
2 namespace Utility\Import;
3
4 class Xml implements ImportInterface
5 {
6         private $_file;
7         private $_specialFields = array();
8         private $_domdoc;
9         private $_rowXPath;
10         private $_elements;
11         private $_currentItem;
12
13         public function __construct($file)
14         {
15                 $this->reset();
16                 $this->_file = $file;
17                 if(!file_exists($this->_file))
18                         throw new \Exception('File ' . $this->_file . ' not found');
19                 $this->_domdoc = new \DOMDocument();
20                 $this->_domdoc->load($this->_file);
21         }
22
23         /* (non-PHPdoc)
24          * @see Nirph_DataImport_Interface::getRecord()
25         */
26         public function getRecord ()
27         {
28                 $record = array();
29                 /* @var $this->_elements DOMNodeList */
30                 if($this->_currentItem >= $this->_elements->length)
31                         return false;
32                 /* @var $node DOMNodeList */
33                 $nodes = $this->_elements->item($this->_currentItem)->childNodes;
34                 for($i = 1;$i < $nodes->length; $i+=2) { // The first item is a #text
35                         if(is_string($nodes->item($i)->nodeValue)) // Only get nodes that has a text value
36                                 $record[$nodes->item($i)->nodeName] = $nodes->item($i)->nodeValue;
37                 }
38                 $this->_currentItem++;
39                 return $record;
40         }
41
42         /* (non-PHPdoc)
43          * @see Nirph_DataImport_Interface::reset()
44         */
45         public function reset ()
46         {
47                 $this->_currentItem = 0;
48         }
49
50         /* (non-PHPdoc)
51          * @see Nirph_DataImport_Interface::setField()
52         */
53         public function setField ($fieldNr, FieldInterface $class)
54         {
55                 // TODO Auto-generated method stub
56         }
57
58         public function setRowElementXPath($xPath)
59         {
60                 // TODO: Validation
61                 $this->_rowXPath = $xPath;
62                 $xpath = new \DOMXpath($this->_domdoc);
63                 /* @var $this->_elements DOMNodeList */
64                 $this->_elements = $xpath->query($this->_rowXPath);
65                 if($this->_elements === false)
66                         throw new \Exception("Invalid xpath");
67         }
68
69 }