EOD; /** @var bool Whether inline or shared strings should be used */ protected $shouldUseInlineStrings; /** @var StyleManager Manages styles */ private $styleManager; /** @var SharedStringsManager Helper to write shared strings */ private $sharedStringsManager; /** @var XLSXEscaper Strings escaper */ private $stringsEscaper; /** @var StringHelper String helper */ private $stringHelper; /** @var EntityFactory Factory to create entities */ private $entityFactory; /** * WorksheetManager constructor. * * @param OptionsManagerInterface $optionsManager * @param StyleManager $styleManager * @param SharedStringsManager $sharedStringsManager * @param XLSXEscaper $stringsEscaper * @param StringHelper $stringHelper * @param EntityFactory $entityFactory */ public function __construct( OptionsManagerInterface $optionsManager, StyleManager $styleManager, SharedStringsManager $sharedStringsManager, XLSXEscaper $stringsEscaper, StringHelper $stringHelper, EntityFactory $entityFactory ) { $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); $this->styleManager = $styleManager; $this->sharedStringsManager = $sharedStringsManager; $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; $this->entityFactory = $entityFactory; } /** * @return SharedStringsManager */ public function getSharedStringsManager() { return $this->sharedStringsManager; } /** * {@inheritdoc} */ public function startSheet(Worksheet $worksheet) { $sheetFilePointer = fopen($worksheet->getFilePath(), 'w'); $this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer); $worksheet->setFilePointer($sheetFilePointer); fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); fwrite($sheetFilePointer, ''); } /** * Checks if the sheet has been sucessfully created. Throws an exception if not. * * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file * @throws IOException If the sheet data file cannot be opened for writing * @return void */ private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) { if (!$sheetFilePointer) { throw new IOException('Unable to open sheet for writing.'); } } /** * {@inheritdoc} */ public function addRow(Worksheet $worksheet, Row $row) { if (!$row->isEmpty()) { $this->addNonEmptyRow($worksheet, $row); } $worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1); } /** * Adds non empty row to the worksheet. * * @param Worksheet $worksheet The worksheet to add the row to * @param Row $row The row to be written * @throws IOException If the data cannot be written * @throws InvalidArgumentException If a cell value's type is not supported * @return void */ private function addNonEmptyRow(Worksheet $worksheet, Row $row) { $cellIndex = 0; $rowStyle = $row->getStyle(); $rowIndex = $worksheet->getLastWrittenRowIndex() + 1; $numCells = count($row->getCells()); $rowXML = ''; foreach ($row->getCells() as $cell) { $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex); $cellIndex++; } $rowXML .= ''; $wasWriteSuccessful = fwrite($worksheet->getFilePointer(), $rowXML); if ($wasWriteSuccessful === false) { throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); } } /** * Applies styles to the given style, merging the cell's style with its row's style * Then builds and returns xml for the cell. * * @param Cell $cell * @param Style $rowStyle * @param int $rowIndex * @param int $cellIndex * @throws InvalidArgumentException If the given value cannot be processed * @return string */ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndex, $cellIndex) { // Apply styles - the row style is merged at this point $cell->applyStyle($rowStyle); $this->styleManager->applyExtraStylesIfNeeded($cell); $registeredStyle = $this->styleManager->registerStyle($cell->getStyle()); return $this->getCellXML($rowIndex, $cellIndex, $cell, $registeredStyle->getId()); } /** * Builds and returns xml for a single cell. * * @param int $rowIndex * @param int $cellNumber * @param Cell $cell * @param int $styleId * @throws InvalidArgumentException If the given value cannot be processed * @return string */ private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) { $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); $cellXML = 'isString()) { $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); } elseif ($cell->isBoolean()) { $cellXML .= ' t="b">' . (int) ($cell->getValue()) . ''; } elseif ($cell->isNumeric()) { $cellXML .= '>' . $cell->getValue() . ''; } elseif ($cell->isEmpty()) { if ($this->styleManager->shouldApplyStyleOnEmptyCell($styleId)) { $cellXML .= '/>'; } else { // don't write empty cells that do no need styling // NOTE: not appending to $cellXML is the right behavior!! $cellXML = ''; } } else { throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cell->getValue())); } return $cellXML; } /** * Returns the XML fragment for a cell containing a non empty string * * @param string $cellValue The cell value * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell * @return string The XML fragment representing the cell */ private function getCellXMLFragmentForNonEmptyString($cellValue) { if ($this->stringHelper->getStringLength($cellValue) > self::MAX_CHARACTERS_PER_CELL) { throw new InvalidArgumentException('Trying to add a value that exceeds the maximum number of characters allowed in a cell (32,767)'); } if ($this->shouldUseInlineStrings) { $cellXMLFragment = ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . ''; } else { $sharedStringId = $this->sharedStringsManager->writeString($cellValue); $cellXMLFragment = ' t="s">' . $sharedStringId . ''; } return $cellXMLFragment; } /** * {@inheritdoc} */ public function close(Worksheet $worksheet) { $worksheetFilePointer = $worksheet->getFilePointer(); if (!is_resource($worksheetFilePointer)) { return; } fwrite($worksheetFilePointer, ''); fwrite($worksheetFilePointer, ''); fclose($worksheetFilePointer); } }