[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace Box\Spout\Reader\ODS; 4 5 use Box\Spout\Common\Exception\IOException; 6 use Box\Spout\Reader\Exception\XMLProcessingException; 7 use Box\Spout\Reader\IteratorInterface; 8 use Box\Spout\Reader\Wrapper\XMLReader; 9 10 /** 11 * Class SheetIterator 12 * Iterate over ODS sheet. 13 * 14 * @package Box\Spout\Reader\ODS 15 */ 16 class SheetIterator implements IteratorInterface 17 { 18 /** Definition of XML nodes name and attribute used to parse sheet data */ 19 const XML_NODE_TABLE = 'table:table'; 20 const XML_ATTRIBUTE_TABLE_NAME = 'table:name'; 21 22 /** @var string $filePath Path of the file to be read */ 23 protected $filePath; 24 25 /** @var XMLReader The XMLReader object that will help read sheet's XML data */ 26 protected $xmlReader; 27 28 /** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */ 29 protected $escaper; 30 31 /** @var bool Whether there are still at least a sheet to be read */ 32 protected $hasFoundSheet; 33 34 /** @var int The index of the sheet being read (zero-based) */ 35 protected $currentSheetIndex; 36 37 /** 38 * @param string $filePath Path of the file to be read 39 * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file 40 */ 41 public function __construct($filePath) 42 { 43 $this->filePath = $filePath; 44 $this->xmlReader = new XMLReader(); 45 46 /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ 47 $this->escaper = new \Box\Spout\Common\Escaper\ODS(); 48 } 49 50 /** 51 * Rewind the Iterator to the first element 52 * @link http://php.net/manual/en/iterator.rewind.php 53 * 54 * @return void 55 * @throws \Box\Spout\Common\Exception\IOException If unable to open the XML file containing sheets' data 56 */ 57 public function rewind() 58 { 59 $this->xmlReader->close(); 60 61 $contentXmlFilePath = $this->filePath . '#content.xml'; 62 if ($this->xmlReader->open('zip://' . $contentXmlFilePath) === false) { 63 throw new IOException("Could not open \"{$contentXmlFilePath}\"."); 64 } 65 66 try { 67 $this->hasFoundSheet = $this->xmlReader->readUntilNodeFound(self::XML_NODE_TABLE); 68 } catch (XMLProcessingException $exception) { 69 throw new IOException("The content.xml file is invalid and cannot be read. [{$exception->getMessage()}]"); 70 } 71 72 $this->currentSheetIndex = 0; 73 } 74 75 /** 76 * Checks if current position is valid 77 * @link http://php.net/manual/en/iterator.valid.php 78 * 79 * @return boolean 80 */ 81 public function valid() 82 { 83 return $this->hasFoundSheet; 84 } 85 86 /** 87 * Move forward to next element 88 * @link http://php.net/manual/en/iterator.next.php 89 * 90 * @return void 91 */ 92 public function next() 93 { 94 $this->hasFoundSheet = $this->xmlReader->readUntilNodeFound(self::XML_NODE_TABLE); 95 96 if ($this->hasFoundSheet) { 97 $this->currentSheetIndex++; 98 } 99 } 100 101 /** 102 * Return the current element 103 * @link http://php.net/manual/en/iterator.current.php 104 * 105 * @return \Box\Spout\Reader\ODS\Sheet 106 */ 107 public function current() 108 { 109 $escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME); 110 $sheetName = $this->escaper->unescape($escapedSheetName); 111 112 return new Sheet($this->xmlReader, $sheetName, $this->currentSheetIndex); 113 } 114 115 /** 116 * Return the key of the current element 117 * @link http://php.net/manual/en/iterator.key.php 118 * 119 * @return int 120 */ 121 public function key() 122 { 123 return $this->currentSheetIndex + 1; 124 } 125 126 /** 127 * Cleans up what was created to iterate over the object. 128 * 129 * @return void 130 */ 131 public function end() 132 { 133 $this->xmlReader->close(); 134 } 135 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |