PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.5
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.5
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / Spout / Writer / XLSX / Helper / SharedStringsHelper.php

SharedStringsHelper.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.5, at app/Services/Spout/Writer/XLSX/Helper/SharedStringsHelper.php

108 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = \Box\Spout\Common\Escaper\XLSX::getInstance();
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 if (!is_resource($this->sharedStringsFilePointer)) {
92 return;
93 }
94
95 fwrite($this->sharedStringsFilePointer, '</sst>');
96
97 // Replace the default strings count with the actual number of shared strings in the file header
98 $firstPartHeaderLength = strlen(self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER);
99 $defaultStringsCountPartLength = strlen(self::DEFAULT_STRINGS_COUNT_PART);
100
101 // Adding 1 to take into account the space between the last xml attribute and "count"
102 fseek($this->sharedStringsFilePointer, $firstPartHeaderLength + 1);
103 fwrite($this->sharedStringsFilePointer, sprintf("%-{$defaultStringsCountPartLength}s", 'count="' . $this->numSharedStrings . '" uniqueCount="' . $this->numSharedStrings . '"'));
104
105 fclose($this->sharedStringsFilePointer);
106 }
107 }
108