[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace Box\Spout\Writer\XLSX\Helper; 4 5 use Box\Spout\Common\Exception\IOException; 6 7 /** 8 * Class SharedStringsHelper 9 * This class provides helper functions to write shared strings 10 * 11 * @package Box\Spout\Writer\XLSX\Helper 12 */ 13 class SharedStringsHelper 14 { 15 const SHARED_STRINGS_FILE_NAME = 'sharedStrings.xml'; 16 17 const SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER = <<<EOD 18 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 19 <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" 20 EOD; 21 22 /** 23 * This number must be really big so that the no generated file will have more strings than that. 24 * If the strings number goes above, characters will be overwritten in an unwanted way and will corrupt the file. 25 */ 26 const DEFAULT_STRINGS_COUNT_PART = 'count="9999999999999" uniqueCount="9999999999999"'; 27 28 /** @var resource Pointer to the sharedStrings.xml file */ 29 protected $sharedStringsFilePointer; 30 31 /** @var int Number of shared strings already written */ 32 protected $numSharedStrings = 0; 33 34 /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ 35 protected $stringsEscaper; 36 37 /** 38 * @param string $xlFolder Path to the "xl" folder 39 */ 40 public function __construct($xlFolder) 41 { 42 $sharedStringsFilePath = $xlFolder . '/' . self::SHARED_STRINGS_FILE_NAME; 43 $this->sharedStringsFilePointer = fopen($sharedStringsFilePath, 'w'); 44 45 $this->throwIfSharedStringsFilePointerIsNotAvailable(); 46 47 // the headers is split into different parts so that we can fseek and put in the correct count and uniqueCount later 48 $header = self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER . ' ' . self::DEFAULT_STRINGS_COUNT_PART . '>'; 49 fwrite($this->sharedStringsFilePointer, $header); 50 51 /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ 52 $this->stringsEscaper = new \Box\Spout\Common\Escaper\XLSX(); 53 } 54 55 /** 56 * Checks if the book has been created. Throws an exception if not created yet. 57 * 58 * @return void 59 * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing 60 */ 61 protected function throwIfSharedStringsFilePointerIsNotAvailable() 62 { 63 if (!$this->sharedStringsFilePointer) { 64 throw new IOException('Unable to open shared strings file for writing.'); 65 } 66 } 67 68 /** 69 * Writes the given string into the sharedStrings.xml file. 70 * Starting and ending whitespaces are preserved. 71 * 72 * @param string $string 73 * @return int ID of the written shared string 74 */ 75 public function writeString($string) 76 { 77 fwrite($this->sharedStringsFilePointer, '<si><t xml:space="preserve">' . $this->stringsEscaper->escape($string) . '</t></si>'); 78 $this->numSharedStrings++; 79 80 // Shared string ID is zero-based 81 return ($this->numSharedStrings - 1); 82 } 83 84 /** 85 * Finishes writing the data in the sharedStrings.xml file and closes the file. 86 * 87 * @return void 88 */ 89 public function close() 90 { 91 fwrite($this->sharedStringsFilePointer, '</sst>'); 92 93 // Replace the default strings count with the actual number of shared strings in the file header 94 $firstPartHeaderLength = strlen(self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER); 95 $defaultStringsCountPartLength = strlen(self::DEFAULT_STRINGS_COUNT_PART); 96 97 // Adding 1 to take into account the space between the last xml attribute and "count" 98 fseek($this->sharedStringsFilePointer, $firstPartHeaderLength + 1); 99 fwrite($this->sharedStringsFilePointer, sprintf("%-{$defaultStringsCountPartLength}s", 'count="' . $this->numSharedStrings . '" uniqueCount="' . $this->numSharedStrings . '"')); 100 101 fclose($this->sharedStringsFilePointer); 102 } 103 }
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 |