| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace OneSheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use OneSheet\Style\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use OneSheet\Style\Styler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use OneSheet\Width\WidthCalculator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use OneSheet\Width\WidthCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package OneSheet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var SheetFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $sheetFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var Styler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $styler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var Sheet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $sheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Writer constructor. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 7 |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 7 |  |         $this->initialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * All cells _above_ this cell will be frozen/fixed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param int $cellId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     public function setFreezePaneCellId($cellId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         $this->sheet->setFreezePaneCellId($cellId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Set fixed column widths per cell (no ranges) and array index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * 0 being the first column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * If used alongside cell autosizing, these should be set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * after the last row has been added. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param array $columnWidths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     public function setFixedColumnWidths(array $columnWidths) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $this->sheet->setFixedColumnWidths($columnWidths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Set lower and/or upper limits for column widths. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param float|null $minWidth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param float|null $maxWidth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |     public function setColumnWidthLimits($minWidth = null, $maxWidth = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         $this->sheet->setColumnWidthLimits($minWidth, $maxWidth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Start recording row specs for column autosizing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *  @return Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |     public function enableCellAutosizing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |         $this->sheet->enableCellAutosizing(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Stop recording row specs for column autosizing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *  @return Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |     public function disableCellAutosizing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         $this->sheet->disableCellAutosizing(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Initialize writer defaults. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 7 |  |     private function initialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |         $this->sheetFile = new SheetFile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 7 |  |         $this->sheetFile->fwrite(str_repeat(' ', pow(2, 20)) . '<sheetData>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 7 |  |         $this->styler = new Styler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 7 |  |         $this->sheet = new Sheet(new CellBuilder(), new WidthCalculator(new WidthCollection())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param array $rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param Style $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |     public function addRows(array $rows, Style $style = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         if (count($rows) === count($rows, COUNT_RECURSIVE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             throw new \InvalidArgumentException('Array must contain arrays!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |         foreach ($rows as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |             $this->addRow($row, $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param array $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param Style $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |     public function addRow(array $row, Style $style = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |         if (!empty($row)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |             $style = $style instanceof Style ? $style : $this->styler->getDefaultStyle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             $this->styler->addStyle($style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |             $this->sheetFile->fwrite($this->sheet->addRow($row, $style)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * Wrap things up and write/output xlsx. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 1 |  |     public function writeToFile($fileName = 'dummy.xlsx') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |         $finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |         $finalizer->finalize($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |