private function buildFile(SheetInterface $sheet) : FileInterface { $csv = tmpfile(); $columns = $sheet->columns()->keys()->sort(function ($a, $b) : bool { return $a > $b; })->toPrimitive(); $default = array_fill_keys(array_values($columns), ''); if ($this->withHeader) { fputcsv($csv, $columns, $this->delimiter); } $sheet->rows()->values()->sort(function (RowInterface $a, RowInterface $b) : bool { return $a->identifier() > $b->identifier(); })->reduce($csv, function ($carry, RowInterface $row) use($default) { fputcsv($carry, $this->fill($default, $row->cells()), $this->delimiter); return $carry; }); return new Csv($sheet->name(), new Stream($csv)); }