|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace avadim\FastExcelLaravel; |
| 4 | + |
| 5 | +use avadim\FastExcelWriter\Sheet; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | + |
| 8 | +class SheetWriter extends Sheet |
| 9 | +{ |
| 10 | + private ?array $headers = null; |
| 11 | + private int $dataRowCount = 0; |
| 12 | + |
| 13 | + protected function _toArray($record) |
| 14 | + { |
| 15 | + if (is_object($record)) { |
| 16 | + if (method_exists($record, 'toArray')) { |
| 17 | + $result = $record->toArray(); |
| 18 | + } |
| 19 | + else { |
| 20 | + $result = json_decode(json_encode($record), true); |
| 21 | + } |
| 22 | + } |
| 23 | + else { |
| 24 | + $result = (array)$record; |
| 25 | + } |
| 26 | + |
| 27 | + return $result; |
| 28 | + } |
| 29 | + |
| 30 | + protected function _writeHeader($record) |
| 31 | + { |
| 32 | + if (!$this->headers['header_keys']) { |
| 33 | + $this->headers['header_keys'] = array_keys($this->_toArray($record)); |
| 34 | + } |
| 35 | + if (!$this->headers['header_values']) { |
| 36 | + $this->headers['header_values'] = $this->headers['header_keys']; |
| 37 | + } |
| 38 | + |
| 39 | + $row = array_combine($this->headers['header_keys'], $this->headers['header_values']); |
| 40 | + $row = $this->headers['header_values']; |
| 41 | + $this->writeHeader($row, $this->headers['rowStyle'], $this->headers['colStyles']); |
| 42 | + ++$this->dataRowCount; |
| 43 | + } |
| 44 | + |
| 45 | + public function writeRow(array $rowValues = [], array $rowStyle = null, array $cellStyles = null): Sheet |
| 46 | + { |
| 47 | + if ($this->dataRowCount > 0 && $this->headers['header_keys']) { |
| 48 | + $rowData = []; |
| 49 | + foreach ($this->headers['header_keys'] as $key) { |
| 50 | + if (isset($rowValues[$key])) { |
| 51 | + $rowData[] = $rowValues[$key]; |
| 52 | + } |
| 53 | + else { |
| 54 | + $rowData[] = null; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + $rowData = array_values($rowValues); |
| 60 | + } |
| 61 | + return parent::writeRow($rowData, $rowStyle, $cellStyles); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param $data |
| 66 | + * @param array|null $rowStyle |
| 67 | + * @param array|null $cellStyles |
| 68 | + * |
| 69 | + * @return $this |
| 70 | + */ |
| 71 | + public function writeData($data, array $rowStyle = null, array $cellStyles = null): SheetWriter |
| 72 | + { |
| 73 | + if (is_array($data) || ($data instanceof Collection)) { |
| 74 | + foreach ($data as $record) { |
| 75 | + if ($this->dataRowCount === 0 && $this->headers) { |
| 76 | + $this->_writeHeader($record); |
| 77 | + } |
| 78 | + $this->writeRow($this->_toArray($record), $rowStyle, $cellStyles); |
| 79 | + ++$this->dataRowCount; |
| 80 | + } |
| 81 | + } |
| 82 | + elseif (is_callable($data)) { |
| 83 | + foreach ($data() as $record) { |
| 84 | + if ($this->dataRowCount === 0 && $this->headers) { |
| 85 | + $this->_writeHeader($record); |
| 86 | + } |
| 87 | + $this->writeRow($this->_toArray($record), $rowStyle, $cellStyles); |
| 88 | + ++$this->dataRowCount; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param $model |
| 97 | + * @param array|null $rowStyle |
| 98 | + * @param array|null $cellStyles |
| 99 | + * |
| 100 | + * @return $this |
| 101 | + */ |
| 102 | + public function writeModel($model, array $rowStyle = null, array $cellStyles = null): SheetWriter |
| 103 | + { |
| 104 | + $this->writeData(static function() use ($model) { |
| 105 | + foreach ($model::cursor() as $user) { |
| 106 | + yield $user; |
| 107 | + } |
| 108 | + }, $rowStyle, $cellStyles); |
| 109 | + |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array $colStyles = []): SheetWriter |
| 114 | + { |
| 115 | + $headerKeys = $headerValues = []; |
| 116 | + if ($headers) { |
| 117 | + foreach ($headers as $key => $val) { |
| 118 | + if (is_string($key)) { |
| 119 | + $headerKeys[] = $key; |
| 120 | + $headerValues[] = $val; |
| 121 | + } |
| 122 | + else { |
| 123 | + $headerKeys[] = $headerValues[] = $val; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $this->headers = [ |
| 129 | + 'header_keys' => $headerKeys, |
| 130 | + 'header_values' => $headerValues, |
| 131 | + 'rowStyle' => $rowStyle, |
| 132 | + 'colStyles' => $colStyles, |
| 133 | + ]; |
| 134 | + |
| 135 | + return $this; |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments