Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Maatwebsite\Excel; use Closure; use Illuminate\Contracts\Support\Arrayabl..

Decoded Output download

<?php
 namespace Maatwebsite\Excel; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Maatwebsite\Excel\Concerns\FromArray; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\FromGenerator; use Maatwebsite\Excel\Concerns\FromIterator; use Maatwebsite\Excel\Concerns\FromQuery; use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Concerns\OnEachRow; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\ToArray; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithCalculatedFormulas; use Maatwebsite\Excel\Concerns\WithCharts; use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithColumnFormatting; use Maatwebsite\Excel\Concerns\WithColumnLimit; use Maatwebsite\Excel\Concerns\WithColumnWidths; use Maatwebsite\Excel\Concerns\WithCustomChunkSize; use Maatwebsite\Excel\Concerns\WithCustomStartCell; use Maatwebsite\Excel\Concerns\WithCustomValueBinder; use Maatwebsite\Excel\Concerns\WithDrawings; use Maatwebsite\Excel\Concerns\WithEvents; use Maatwebsite\Excel\Concerns\WithFormatData; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMappedCells; use Maatwebsite\Excel\Concerns\WithMapping; use Maatwebsite\Excel\Concerns\WithProgressBar; use Maatwebsite\Excel\Concerns\WithStrictNullComparison; use Maatwebsite\Excel\Concerns\WithStyles; use Maatwebsite\Excel\Concerns\WithTitle; use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Events\AfterSheet; use Maatwebsite\Excel\Events\BeforeSheet; use Maatwebsite\Excel\Exceptions\ConcernConflictException; use Maatwebsite\Excel\Exceptions\RowSkippedException; use Maatwebsite\Excel\Exceptions\SheetNotFoundException; use Maatwebsite\Excel\Files\TemporaryFileFactory; use Maatwebsite\Excel\Helpers\ArrayHelper; use Maatwebsite\Excel\Helpers\CellHelper; use Maatwebsite\Excel\Imports\EndRowFinder; use Maatwebsite\Excel\Imports\HeadingRowExtractor; use Maatwebsite\Excel\Imports\ModelImporter; use Maatwebsite\Excel\Validators\RowValidator; use PhpOffice\PhpSpreadsheet\Cell\Cell as SpreadsheetCell; use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class Sheet { use DelegatedMacroable, HasEventBus; protected $chunkSize; protected $temporaryFileFactory; protected $exportable; private $worksheet; public function __construct(Worksheet $worksheet) { $this->worksheet = $worksheet; $this->chunkSize = config("excel.exports.chunk_size", 100); $this->temporaryFileFactory = app(TemporaryFileFactory::class); } public static function make(Spreadsheet $spreadsheet, $index) { if (is_numeric($index)) { return self::byIndex($spreadsheet, $index); } return self::byName($spreadsheet, $index); } public static function byIndex(Spreadsheet $spreadsheet, int $index) : Sheet { if (!isset($spreadsheet->getAllSheets()[$index])) { throw SheetNotFoundException::byIndex($index, $spreadsheet->getSheetCount()); } return new static($spreadsheet->getSheet($index)); } public static function byName(Spreadsheet $spreadsheet, string $name) : Sheet { if (!$spreadsheet->sheetNameExists($name)) { throw SheetNotFoundException::byName($name); } return new static($spreadsheet->getSheetByName($name)); } public function open($sheetExport) { $this->exportable = $sheetExport; if ($sheetExport instanceof WithCustomValueBinder) { SpreadsheetCell::setValueBinder($sheetExport); } if ($sheetExport instanceof WithEvents) { $this->registerListeners($sheetExport->registerEvents()); } $this->raise(new BeforeSheet($this, $this->exportable)); if ($sheetExport instanceof WithTitle) { $title = $sheetExport->title(); $title = str_replace(array("*", ":", "/", "\", "?", "[", "]"), '', $title); if (StringHelper::countCharacters($title) > Worksheet::SHEET_TITLE_MAXIMUM_LENGTH) { $title = StringHelper::substring($title, 0, Worksheet::SHEET_TITLE_MAXIMUM_LENGTH); } $this->worksheet->setTitle($title); } if (($sheetExport instanceof FromQuery || $sheetExport instanceof FromCollection || $sheetExport instanceof FromArray) && $sheetExport instanceof FromView) { throw ConcernConflictException::queryOrCollectionAndView(); } if (!$sheetExport instanceof FromView && $sheetExport instanceof WithHeadings) { if ($sheetExport instanceof WithCustomStartCell) { $startCell = $sheetExport->startCell(); } $this->append(ArrayHelper::ensureMultipleRows($sheetExport->headings()), $startCell ?? null, $this->hasStrictNullComparison($sheetExport)); } if ($sheetExport instanceof WithCharts) { $this->addCharts($sheetExport->charts()); } } public function export($sheetExport) { $this->open($sheetExport); if ($sheetExport instanceof FromView) { $this->fromView($sheetExport); } else { if ($sheetExport instanceof FromQuery) { $this->fromQuery($sheetExport, $this->worksheet); } if ($sheetExport instanceof FromCollection) { $this->fromCollection($sheetExport); } if ($sheetExport instanceof FromArray) { $this->fromArray($sheetExport); } if ($sheetExport instanceof FromIterator) { $this->fromIterator($sheetExport); } if ($sheetExport instanceof FromGenerator) { $this->fromGenerator($sheetExport); } } $this->close($sheetExport); } public function import($import, int $startRow = 1) { if ($import instanceof WithEvents) { $this->registerListeners($import->registerEvents()); } $this->raise(new BeforeSheet($this, $import)); if ($import instanceof WithProgressBar && !$import instanceof WithChunkReading) { $import->getConsoleOutput()->progressStart($this->worksheet->getHighestRow()); } $calculatesFormulas = $import instanceof WithCalculatedFormulas; $formatData = $import instanceof WithFormatData; if ($import instanceof WithMappedCells) { app(MappedReader::class)->map($import, $this->worksheet); } else { if ($import instanceof ToModel) { app(ModelImporter::class)->import($this->worksheet, $import, $startRow); } if ($import instanceof ToCollection) { $rows = $this->toCollection($import, $startRow, null, $calculatesFormulas, $formatData); if ($import instanceof WithValidation) { $rows = $this->validated($import, $startRow, $rows); } $import->collection($rows); } if ($import instanceof ToArray) { $rows = $this->toArray($import, $startRow, null, $calculatesFormulas, $formatData); if ($import instanceof WithValidation) { $rows = $this->validated($import, $startRow, $rows); } $import->array($rows); } } if ($import instanceof OnEachRow) { $headingRow = HeadingRowExtractor::extract($this->worksheet, $import); $headerIsGrouped = HeadingRowExtractor::extractGrouping($headingRow, $import); $endColumn = $import instanceof WithColumnLimit ? $import->endColumn() : null; $preparationCallback = $this->getPreparationCallback($import); foreach ($this->worksheet->getRowIterator()->resetStart($startRow ?? 1) as $row) { $sheetRow = new Row($row, $headingRow, $headerIsGrouped); if ($import instanceof WithValidation) { $sheetRow->setPreparationCallback($preparationCallback); } $rowArray = $sheetRow->toArray(null, $import instanceof WithCalculatedFormulas, $import instanceof WithFormatData, $endColumn); $rowIsEmptyAccordingToImport = $import instanceof SkipsEmptyRows && method_exists($import, "isEmptyWhen") && $import->isEmptyWhen($rowArray); if (!$import instanceof SkipsEmptyRows || $import instanceof SkipsEmptyRows && (!$rowIsEmptyAccordingToImport && !$sheetRow->isEmpty($calculatesFormulas))) { if ($import instanceof WithValidation) { $toValidate = array($sheetRow->getIndex() => $rowArray); try { app(RowValidator::class)->validate($toValidate, $import); $import->onRow($sheetRow); } catch (RowSkippedException $e) { } } else { $import->onRow($sheetRow); } } if ($import instanceof WithProgressBar) { $import->getConsoleOutput()->progressAdvance(); } } } $this->raise(new AfterSheet($this, $import)); if ($import instanceof WithProgressBar && !$import instanceof WithChunkReading) { $import->getConsoleOutput()->progressFinish(); } } public function toArray($import, int $startRow = null, $nullValue = null, $calculateFormulas = false, $formatData = false) { if ($startRow > $this->worksheet->getHighestRow()) { return array(); } $endRow = EndRowFinder::find($import, $startRow, $this->worksheet->getHighestRow()); $headingRow = HeadingRowExtractor::extract($this->worksheet, $import); $headerIsGrouped = HeadingRowExtractor::extractGrouping($headingRow, $import); $endColumn = $import instanceof WithColumnLimit ? $import->endColumn() : null; $rows = array(); foreach ($this->worksheet->getRowIterator($startRow, $endRow) as $index => $row) { $row = new Row($row, $headingRow, $headerIsGrouped); if ($import instanceof SkipsEmptyRows && $row->isEmpty($calculateFormulas, $endColumn)) { continue; } $row = $row->toArray($nullValue, $calculateFormulas, $formatData, $endColumn); if ($import && method_exists($import, "isEmptyWhen") && $import->isEmptyWhen($row)) { continue; } if ($import instanceof WithMapping) { $row = $import->map($row); } if ($import instanceof WithValidation && method_exists($import, "prepareForValidation")) { $row = $import->prepareForValidation($row, $index); } $rows[] = $row; if ($import instanceof WithProgressBar) { $import->getConsoleOutput()->progressAdvance(); } } return $rows; } public function toCollection($import, int $startRow = null, $nullValue = null, $calculateFormulas = false, $formatData = false) : Collection { $rows = $this->toArray($import, $startRow, $nullValue, $calculateFormulas, $formatData); return new Collection(array_map(function (array $row) { return new Collection($row); }, $rows)); } public function close($sheetExport) { if ($sheetExport instanceof WithDrawings) { $this->addDrawings($sheetExport->drawings()); } $this->exportable = $sheetExport; if ($sheetExport instanceof WithColumnFormatting) { foreach ($sheetExport->columnFormats() as $column => $format) { $this->formatColumn($column, $format); } } if ($sheetExport instanceof ShouldAutoSize) { $this->autoSize(); } if ($sheetExport instanceof WithColumnWidths) { foreach ($sheetExport->columnWidths() as $column => $width) { $this->worksheet->getColumnDimension($column)->setAutoSize(false)->setWidth($width); } } if ($sheetExport instanceof WithStyles) { $styles = $sheetExport->styles($this->worksheet); if (is_array($styles)) { foreach ($styles as $coordinate => $coordinateStyles) { if (is_numeric($coordinate)) { $coordinate = "A" . $coordinate . ":" . $this->worksheet->getHighestColumn($coordinate) . $coordinate; } $this->worksheet->getStyle($coordinate)->applyFromArray($coordinateStyles); } } } $this->raise(new AfterSheet($this, $this->exportable)); $this->clearListeners(); } public function fromView(FromView $sheetExport, $sheetIndex = null) { $temporaryFile = $this->temporaryFileFactory->makeLocal(null, "html"); $temporaryFile->put($sheetExport->view()->render()); $spreadsheet = $this->worksheet->getParent(); $reader = IOFactory::createReader("Html"); $reader->setSheetIndex($sheetIndex ?? $spreadsheet->getSheetCount() - 1); $reader->loadIntoExisting($temporaryFile->getLocalPath(), $spreadsheet); $temporaryFile->delete(); } public function fromQuery(FromQuery $sheetExport, Worksheet $worksheet) { if ($sheetExport->query() instanceof \Laravel\Scout\Builder) { $this->fromScout($sheetExport, $worksheet); return; } $sheetExport->query()->chunk($this->getChunkSize($sheetExport), function ($chunk) use($sheetExport) { $this->appendRows($chunk, $sheetExport); }); } public function fromScout(FromQuery $sheetExport, Worksheet $worksheet) { $scout = $sheetExport->query(); $chunkSize = $this->getChunkSize($sheetExport); $chunk = $scout->paginate($chunkSize); $this->appendRows($chunk->items(), $sheetExport); for ($page = 2; $page <= $chunk->lastPage(); $page++) { $this->appendRows($scout->paginate($chunkSize, "page", $page)->items(), $sheetExport); } } public function fromCollection(FromCollection $sheetExport) { $this->appendRows($sheetExport->collection()->all(), $sheetExport); } public function fromArray(FromArray $sheetExport) { $this->appendRows($sheetExport->array(), $sheetExport); } public function fromIterator(FromIterator $sheetExport) { $iterator = class_exists(LazyCollection::class) ? new LazyCollection(function () use($sheetExport) { foreach ($sheetExport->iterator() as $row) { (yield $row); } }) : $sheetExport->iterator(); $this->appendRows($iterator, $sheetExport); } public function fromGenerator(FromGenerator $sheetExport) { $generator = class_exists(LazyCollection::class) ? new LazyCollection(function () use($sheetExport) { foreach ($sheetExport->generator() as $row) { (yield $row); } }) : $sheetExport->generator(); $this->appendRows($generator, $sheetExport); } public function append(array $rows, string $startCell = null, bool $strictNullComparison = false) { if (!$startCell) { $startCell = "A1"; } if ($this->hasRows()) { $startCell = CellHelper::getColumnFromCoordinate($startCell) . ($this->worksheet->getHighestRow() + 1); } $this->worksheet->fromArray($rows, null, $startCell, $strictNullComparison); } public function autoSize() { foreach ($this->buildColumnRange("A", $this->worksheet->getHighestDataColumn()) as $col) { $dimension = $this->worksheet->getColumnDimension($col); if ($dimension->getWidth() == -1) { $dimension->setAutoSize(true); } } } public function formatColumn(string $column, string $format) { if (stripos($column, ":") !== false) { $this->worksheet->getStyle($column)->getNumberFormat()->setFormatCode($format); } else { $this->worksheet->getStyle($column . "1:" . $column . $this->worksheet->getHighestRow())->getNumberFormat()->setFormatCode($format); } } public function chunkSize(int $chunkSize) { $this->chunkSize = $chunkSize; return $this; } public function getDelegate() { return $this->worksheet; } public function addCharts($charts) { $charts = \is_array($charts) ? $charts : array($charts); foreach ($charts as $chart) { $this->worksheet->addChart($chart); } } public function addDrawings($drawings) { $drawings = \is_array($drawings) ? $drawings : array($drawings); foreach ($drawings as $drawing) { $drawing->setWorksheet($this->worksheet); } } public function hasConcern(string $concern) : string { return $this->exportable instanceof $concern; } public function appendRows($rows, $sheetExport) { if (method_exists($sheetExport, "prepareRows")) { $rows = $sheetExport->prepareRows($rows); } $rows = $rows instanceof LazyCollection ? $rows : new Collection($rows); $rows->flatMap(function ($row) use($sheetExport) { if ($sheetExport instanceof WithMapping) { $row = $sheetExport->map($row); } if ($sheetExport instanceof WithCustomValueBinder) { SpreadsheetCell::setValueBinder($sheetExport); } return ArrayHelper::ensureMultipleRows(static::mapArraybleRow($row)); })->chunk(1000)->each(function ($rows) use($sheetExport) { $this->append($rows->toArray(), $sheetExport instanceof WithCustomStartCell ? $sheetExport->startCell() : null, $this->hasStrictNullComparison($sheetExport)); }); } public static function mapArraybleRow($row) : array { if (is_object($row) && method_exists($row, "attributesToArray")) { return $row->attributesToArray(); } if ($row instanceof Arrayable) { return $row->toArray(); } if (is_object($row)) { return json_decode(json_encode($row), true); } return $row; } public function getStartRow($sheetImport) : int { return HeadingRowExtractor::determineStartRow($sheetImport); } public function disconnect() { $this->worksheet->disconnectCells(); unset($this->worksheet); } protected function validated(WithValidation $import, int $startRow, $rows) { $toValidate = (new Collection($rows))->mapWithKeys(function ($row, $index) use($startRow) { return array($startRow + $index => $row); }); try { app(RowValidator::class)->validate($toValidate->toArray(), $import); } catch (RowSkippedException $e) { foreach ($e->skippedRows() as $row) { unset($rows[$row - $startRow]); } } return $rows; } protected function buildColumnRange(string $lower, string $upper) { $upper++; for ($i = $lower; $i !== $upper; $i++) { (yield $i); } } private function hasRows() : bool { $startCell = "A1"; if ($this->exportable instanceof WithCustomStartCell) { $startCell = $this->exportable->startCell(); } return $this->worksheet->cellExists($startCell); } private function hasStrictNullComparison($sheetExport) : bool { if ($sheetExport instanceof WithStrictNullComparison) { return true; } return config("excel.exports.strict_null_comparison", false); } private function getChunkSize($export) : int { if ($export instanceof WithCustomChunkSize) { return $export->chunkSize(); } return $this->chunkSize; } private function getPreparationCallback($import) { if (!$import instanceof WithValidation || !method_exists($import, "prepareForValidation")) { return null; } return function (array $data, int $index) use($import) { return $import->prepareForValidation($data, $index); }; } } ?>

Did this file decode correctly?

Original Code

<?php
 namespace Maatwebsite\Excel; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Maatwebsite\Excel\Concerns\FromArray; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\FromGenerator; use Maatwebsite\Excel\Concerns\FromIterator; use Maatwebsite\Excel\Concerns\FromQuery; use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Concerns\OnEachRow; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\ToArray; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithCalculatedFormulas; use Maatwebsite\Excel\Concerns\WithCharts; use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithColumnFormatting; use Maatwebsite\Excel\Concerns\WithColumnLimit; use Maatwebsite\Excel\Concerns\WithColumnWidths; use Maatwebsite\Excel\Concerns\WithCustomChunkSize; use Maatwebsite\Excel\Concerns\WithCustomStartCell; use Maatwebsite\Excel\Concerns\WithCustomValueBinder; use Maatwebsite\Excel\Concerns\WithDrawings; use Maatwebsite\Excel\Concerns\WithEvents; use Maatwebsite\Excel\Concerns\WithFormatData; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMappedCells; use Maatwebsite\Excel\Concerns\WithMapping; use Maatwebsite\Excel\Concerns\WithProgressBar; use Maatwebsite\Excel\Concerns\WithStrictNullComparison; use Maatwebsite\Excel\Concerns\WithStyles; use Maatwebsite\Excel\Concerns\WithTitle; use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Events\AfterSheet; use Maatwebsite\Excel\Events\BeforeSheet; use Maatwebsite\Excel\Exceptions\ConcernConflictException; use Maatwebsite\Excel\Exceptions\RowSkippedException; use Maatwebsite\Excel\Exceptions\SheetNotFoundException; use Maatwebsite\Excel\Files\TemporaryFileFactory; use Maatwebsite\Excel\Helpers\ArrayHelper; use Maatwebsite\Excel\Helpers\CellHelper; use Maatwebsite\Excel\Imports\EndRowFinder; use Maatwebsite\Excel\Imports\HeadingRowExtractor; use Maatwebsite\Excel\Imports\ModelImporter; use Maatwebsite\Excel\Validators\RowValidator; use PhpOffice\PhpSpreadsheet\Cell\Cell as SpreadsheetCell; use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class Sheet { use DelegatedMacroable, HasEventBus; protected $chunkSize; protected $temporaryFileFactory; protected $exportable; private $worksheet; public function __construct(Worksheet $worksheet) { $this->worksheet = $worksheet; $this->chunkSize = config("\x65\170\x63\145\x6c\x2e\x65\x78\x70\157\162\x74\x73\56\x63\x68\x75\156\153\137\163\x69\x7a\x65", 100); $this->temporaryFileFactory = app(TemporaryFileFactory::class); } public static function make(Spreadsheet $spreadsheet, $index) { if (is_numeric($index)) { return self::byIndex($spreadsheet, $index); } return self::byName($spreadsheet, $index); } public static function byIndex(Spreadsheet $spreadsheet, int $index) : Sheet { if (!isset($spreadsheet->getAllSheets()[$index])) { throw SheetNotFoundException::byIndex($index, $spreadsheet->getSheetCount()); } return new static($spreadsheet->getSheet($index)); } public static function byName(Spreadsheet $spreadsheet, string $name) : Sheet { if (!$spreadsheet->sheetNameExists($name)) { throw SheetNotFoundException::byName($name); } return new static($spreadsheet->getSheetByName($name)); } public function open($sheetExport) { $this->exportable = $sheetExport; if ($sheetExport instanceof WithCustomValueBinder) { SpreadsheetCell::setValueBinder($sheetExport); } if ($sheetExport instanceof WithEvents) { $this->registerListeners($sheetExport->registerEvents()); } $this->raise(new BeforeSheet($this, $this->exportable)); if ($sheetExport instanceof WithTitle) { $title = $sheetExport->title(); $title = str_replace(array("\x2a", "\72", "\57", "\134", "\77", "\x5b", "\135"), '', $title); if (StringHelper::countCharacters($title) > Worksheet::SHEET_TITLE_MAXIMUM_LENGTH) { $title = StringHelper::substring($title, 0, Worksheet::SHEET_TITLE_MAXIMUM_LENGTH); } $this->worksheet->setTitle($title); } if (($sheetExport instanceof FromQuery || $sheetExport instanceof FromCollection || $sheetExport instanceof FromArray) && $sheetExport instanceof FromView) { throw ConcernConflictException::queryOrCollectionAndView(); } if (!$sheetExport instanceof FromView && $sheetExport instanceof WithHeadings) { if ($sheetExport instanceof WithCustomStartCell) { $startCell = $sheetExport->startCell(); } $this->append(ArrayHelper::ensureMultipleRows($sheetExport->headings()), $startCell ?? null, $this->hasStrictNullComparison($sheetExport)); } if ($sheetExport instanceof WithCharts) { $this->addCharts($sheetExport->charts()); } } public function export($sheetExport) { $this->open($sheetExport); if ($sheetExport instanceof FromView) { $this->fromView($sheetExport); } else { if ($sheetExport instanceof FromQuery) { $this->fromQuery($sheetExport, $this->worksheet); } if ($sheetExport instanceof FromCollection) { $this->fromCollection($sheetExport); } if ($sheetExport instanceof FromArray) { $this->fromArray($sheetExport); } if ($sheetExport instanceof FromIterator) { $this->fromIterator($sheetExport); } if ($sheetExport instanceof FromGenerator) { $this->fromGenerator($sheetExport); } } $this->close($sheetExport); } public function import($import, int $startRow = 1) { if ($import instanceof WithEvents) { $this->registerListeners($import->registerEvents()); } $this->raise(new BeforeSheet($this, $import)); if ($import instanceof WithProgressBar && !$import instanceof WithChunkReading) { $import->getConsoleOutput()->progressStart($this->worksheet->getHighestRow()); } $calculatesFormulas = $import instanceof WithCalculatedFormulas; $formatData = $import instanceof WithFormatData; if ($import instanceof WithMappedCells) { app(MappedReader::class)->map($import, $this->worksheet); } else { if ($import instanceof ToModel) { app(ModelImporter::class)->import($this->worksheet, $import, $startRow); } if ($import instanceof ToCollection) { $rows = $this->toCollection($import, $startRow, null, $calculatesFormulas, $formatData); if ($import instanceof WithValidation) { $rows = $this->validated($import, $startRow, $rows); } $import->collection($rows); } if ($import instanceof ToArray) { $rows = $this->toArray($import, $startRow, null, $calculatesFormulas, $formatData); if ($import instanceof WithValidation) { $rows = $this->validated($import, $startRow, $rows); } $import->array($rows); } } if ($import instanceof OnEachRow) { $headingRow = HeadingRowExtractor::extract($this->worksheet, $import); $headerIsGrouped = HeadingRowExtractor::extractGrouping($headingRow, $import); $endColumn = $import instanceof WithColumnLimit ? $import->endColumn() : null; $preparationCallback = $this->getPreparationCallback($import); foreach ($this->worksheet->getRowIterator()->resetStart($startRow ?? 1) as $row) { $sheetRow = new Row($row, $headingRow, $headerIsGrouped); if ($import instanceof WithValidation) { $sheetRow->setPreparationCallback($preparationCallback); } $rowArray = $sheetRow->toArray(null, $import instanceof WithCalculatedFormulas, $import instanceof WithFormatData, $endColumn); $rowIsEmptyAccordingToImport = $import instanceof SkipsEmptyRows && method_exists($import, "\151\163\105\155\x70\164\171\x57\x68\145\x6e") && $import->isEmptyWhen($rowArray); if (!$import instanceof SkipsEmptyRows || $import instanceof SkipsEmptyRows && (!$rowIsEmptyAccordingToImport && !$sheetRow->isEmpty($calculatesFormulas))) { if ($import instanceof WithValidation) { $toValidate = array($sheetRow->getIndex() => $rowArray); try { app(RowValidator::class)->validate($toValidate, $import); $import->onRow($sheetRow); } catch (RowSkippedException $e) { } } else { $import->onRow($sheetRow); } } if ($import instanceof WithProgressBar) { $import->getConsoleOutput()->progressAdvance(); } } } $this->raise(new AfterSheet($this, $import)); if ($import instanceof WithProgressBar && !$import instanceof WithChunkReading) { $import->getConsoleOutput()->progressFinish(); } } public function toArray($import, int $startRow = null, $nullValue = null, $calculateFormulas = false, $formatData = false) { if ($startRow > $this->worksheet->getHighestRow()) { return array(); } $endRow = EndRowFinder::find($import, $startRow, $this->worksheet->getHighestRow()); $headingRow = HeadingRowExtractor::extract($this->worksheet, $import); $headerIsGrouped = HeadingRowExtractor::extractGrouping($headingRow, $import); $endColumn = $import instanceof WithColumnLimit ? $import->endColumn() : null; $rows = array(); foreach ($this->worksheet->getRowIterator($startRow, $endRow) as $index => $row) { $row = new Row($row, $headingRow, $headerIsGrouped); if ($import instanceof SkipsEmptyRows && $row->isEmpty($calculateFormulas, $endColumn)) { continue; } $row = $row->toArray($nullValue, $calculateFormulas, $formatData, $endColumn); if ($import && method_exists($import, "\151\x73\x45\x6d\160\164\x79\x57\150\145\x6e") && $import->isEmptyWhen($row)) { continue; } if ($import instanceof WithMapping) { $row = $import->map($row); } if ($import instanceof WithValidation && method_exists($import, "\x70\x72\145\x70\x61\x72\x65\x46\157\162\126\141\x6c\151\144\141\164\151\x6f\x6e")) { $row = $import->prepareForValidation($row, $index); } $rows[] = $row; if ($import instanceof WithProgressBar) { $import->getConsoleOutput()->progressAdvance(); } } return $rows; } public function toCollection($import, int $startRow = null, $nullValue = null, $calculateFormulas = false, $formatData = false) : Collection { $rows = $this->toArray($import, $startRow, $nullValue, $calculateFormulas, $formatData); return new Collection(array_map(function (array $row) { return new Collection($row); }, $rows)); } public function close($sheetExport) { if ($sheetExport instanceof WithDrawings) { $this->addDrawings($sheetExport->drawings()); } $this->exportable = $sheetExport; if ($sheetExport instanceof WithColumnFormatting) { foreach ($sheetExport->columnFormats() as $column => $format) { $this->formatColumn($column, $format); } } if ($sheetExport instanceof ShouldAutoSize) { $this->autoSize(); } if ($sheetExport instanceof WithColumnWidths) { foreach ($sheetExport->columnWidths() as $column => $width) { $this->worksheet->getColumnDimension($column)->setAutoSize(false)->setWidth($width); } } if ($sheetExport instanceof WithStyles) { $styles = $sheetExport->styles($this->worksheet); if (is_array($styles)) { foreach ($styles as $coordinate => $coordinateStyles) { if (is_numeric($coordinate)) { $coordinate = "\x41" . $coordinate . "\72" . $this->worksheet->getHighestColumn($coordinate) . $coordinate; } $this->worksheet->getStyle($coordinate)->applyFromArray($coordinateStyles); } } } $this->raise(new AfterSheet($this, $this->exportable)); $this->clearListeners(); } public function fromView(FromView $sheetExport, $sheetIndex = null) { $temporaryFile = $this->temporaryFileFactory->makeLocal(null, "\x68\x74\155\x6c"); $temporaryFile->put($sheetExport->view()->render()); $spreadsheet = $this->worksheet->getParent(); $reader = IOFactory::createReader("\x48\164\x6d\154"); $reader->setSheetIndex($sheetIndex ?? $spreadsheet->getSheetCount() - 1); $reader->loadIntoExisting($temporaryFile->getLocalPath(), $spreadsheet); $temporaryFile->delete(); } public function fromQuery(FromQuery $sheetExport, Worksheet $worksheet) { if ($sheetExport->query() instanceof \Laravel\Scout\Builder) { $this->fromScout($sheetExport, $worksheet); return; } $sheetExport->query()->chunk($this->getChunkSize($sheetExport), function ($chunk) use($sheetExport) { $this->appendRows($chunk, $sheetExport); }); } public function fromScout(FromQuery $sheetExport, Worksheet $worksheet) { $scout = $sheetExport->query(); $chunkSize = $this->getChunkSize($sheetExport); $chunk = $scout->paginate($chunkSize); $this->appendRows($chunk->items(), $sheetExport); for ($page = 2; $page <= $chunk->lastPage(); $page++) { $this->appendRows($scout->paginate($chunkSize, "\x70\141\x67\x65", $page)->items(), $sheetExport); } } public function fromCollection(FromCollection $sheetExport) { $this->appendRows($sheetExport->collection()->all(), $sheetExport); } public function fromArray(FromArray $sheetExport) { $this->appendRows($sheetExport->array(), $sheetExport); } public function fromIterator(FromIterator $sheetExport) { $iterator = class_exists(LazyCollection::class) ? new LazyCollection(function () use($sheetExport) { foreach ($sheetExport->iterator() as $row) { (yield $row); } }) : $sheetExport->iterator(); $this->appendRows($iterator, $sheetExport); } public function fromGenerator(FromGenerator $sheetExport) { $generator = class_exists(LazyCollection::class) ? new LazyCollection(function () use($sheetExport) { foreach ($sheetExport->generator() as $row) { (yield $row); } }) : $sheetExport->generator(); $this->appendRows($generator, $sheetExport); } public function append(array $rows, string $startCell = null, bool $strictNullComparison = false) { if (!$startCell) { $startCell = "\101\61"; } if ($this->hasRows()) { $startCell = CellHelper::getColumnFromCoordinate($startCell) . ($this->worksheet->getHighestRow() + 1); } $this->worksheet->fromArray($rows, null, $startCell, $strictNullComparison); } public function autoSize() { foreach ($this->buildColumnRange("\x41", $this->worksheet->getHighestDataColumn()) as $col) { $dimension = $this->worksheet->getColumnDimension($col); if ($dimension->getWidth() == -1) { $dimension->setAutoSize(true); } } } public function formatColumn(string $column, string $format) { if (stripos($column, "\x3a") !== false) { $this->worksheet->getStyle($column)->getNumberFormat()->setFormatCode($format); } else { $this->worksheet->getStyle($column . "\61\72" . $column . $this->worksheet->getHighestRow())->getNumberFormat()->setFormatCode($format); } } public function chunkSize(int $chunkSize) { $this->chunkSize = $chunkSize; return $this; } public function getDelegate() { return $this->worksheet; } public function addCharts($charts) { $charts = \is_array($charts) ? $charts : array($charts); foreach ($charts as $chart) { $this->worksheet->addChart($chart); } } public function addDrawings($drawings) { $drawings = \is_array($drawings) ? $drawings : array($drawings); foreach ($drawings as $drawing) { $drawing->setWorksheet($this->worksheet); } } public function hasConcern(string $concern) : string { return $this->exportable instanceof $concern; } public function appendRows($rows, $sheetExport) { if (method_exists($sheetExport, "\160\162\x65\x70\x61\162\x65\x52\x6f\167\x73")) { $rows = $sheetExport->prepareRows($rows); } $rows = $rows instanceof LazyCollection ? $rows : new Collection($rows); $rows->flatMap(function ($row) use($sheetExport) { if ($sheetExport instanceof WithMapping) { $row = $sheetExport->map($row); } if ($sheetExport instanceof WithCustomValueBinder) { SpreadsheetCell::setValueBinder($sheetExport); } return ArrayHelper::ensureMultipleRows(static::mapArraybleRow($row)); })->chunk(1000)->each(function ($rows) use($sheetExport) { $this->append($rows->toArray(), $sheetExport instanceof WithCustomStartCell ? $sheetExport->startCell() : null, $this->hasStrictNullComparison($sheetExport)); }); } public static function mapArraybleRow($row) : array { if (is_object($row) && method_exists($row, "\x61\x74\164\x72\151\x62\165\x74\x65\163\124\x6f\x41\162\x72\x61\x79")) { return $row->attributesToArray(); } if ($row instanceof Arrayable) { return $row->toArray(); } if (is_object($row)) { return json_decode(json_encode($row), true); } return $row; } public function getStartRow($sheetImport) : int { return HeadingRowExtractor::determineStartRow($sheetImport); } public function disconnect() { $this->worksheet->disconnectCells(); unset($this->worksheet); } protected function validated(WithValidation $import, int $startRow, $rows) { $toValidate = (new Collection($rows))->mapWithKeys(function ($row, $index) use($startRow) { return array($startRow + $index => $row); }); try { app(RowValidator::class)->validate($toValidate->toArray(), $import); } catch (RowSkippedException $e) { foreach ($e->skippedRows() as $row) { unset($rows[$row - $startRow]); } } return $rows; } protected function buildColumnRange(string $lower, string $upper) { $upper++; for ($i = $lower; $i !== $upper; $i++) { (yield $i); } } private function hasRows() : bool { $startCell = "\x41\61"; if ($this->exportable instanceof WithCustomStartCell) { $startCell = $this->exportable->startCell(); } return $this->worksheet->cellExists($startCell); } private function hasStrictNullComparison($sheetExport) : bool { if ($sheetExport instanceof WithStrictNullComparison) { return true; } return config("\x65\x78\143\145\x6c\x2e\x65\170\x70\x6f\x72\x74\163\56\163\164\x72\x69\143\x74\137\156\x75\154\x6c\x5f\143\x6f\155\160\141\162\x69\163\157\x6e", false); } private function getChunkSize($export) : int { if ($export instanceof WithCustomChunkSize) { return $export->chunkSize(); } return $this->chunkSize; } private function getPreparationCallback($import) { if (!$import instanceof WithValidation || !method_exists($import, "\x70\162\145\x70\141\x72\145\106\x6f\162\126\x61\154\x69\x64\x61\x74\x69\x6f\x6e")) { return null; } return function (array $data, int $index) use($import) { return $import->prepareForValidation($data, $index); }; } }

Function Calls

None

Variables

None

Stats

MD5 056bf2f84a0a2db4150442a1d6d77dcc
Eval Count 0
Decode Time 125 ms