StoreQueryMacro.php 0000644 00000003565 15152066161 0010375 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Sheet;
class StoreQueryMacro
{
public function __invoke()
{
return function (string $filePath, string $disk = null, string $writerType = null, $withHeadings = false) {
$export = new class($this, $withHeadings) implements FromQuery, WithHeadings
{
use Exportable;
/**
* @var bool
*/
private $withHeadings;
/**
* @var Builder
*/
private $query;
/**
* @param $query
* @param bool $withHeadings
*/
public function __construct($query, bool $withHeadings = false)
{
$this->query = $query;
$this->withHeadings = $withHeadings;
}
/**
* @return Builder
*/
public function query()
{
return $this->query;
}
/**
* @return array
*/
public function headings(): array
{
if (!$this->withHeadings) {
return [];
}
$firstRow = (clone $this->query)->first();
if ($firstRow) {
return array_keys(Sheet::mapArraybleRow($firstRow));
}
return [];
}
};
return $export->store($filePath, $disk, $writerType);
};
}
}
ImportMacro.php 0000644 00000002266 15152066161 0007522 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ImportMacro
{
public function __invoke()
{
return function (string $filename, string $disk = null, string $readerType = null) {
$import = new class(get_class($this->getModel())) implements ToModel, WithHeadingRow
{
use Importable;
/**
* @var string
*/
private $model;
/**
* @param string $model
*/
public function __construct(string $model)
{
$this->model = $model;
}
/**
* @param array $row
* @return Model|Model[]|null
*/
public function model(array $row)
{
return (new $this->model)->fill($row);
}
};
return $import->import($filename, $disk, $readerType);
};
}
}
ImportAsMacro.php 0000644 00000002664 15152066161 0010010 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
class ImportAsMacro
{
public function __invoke()
{
return function (string $filename, callable $mapping, string $disk = null, string $readerType = null) {
$import = new class(get_class($this->getModel()), $mapping) implements ToModel
{
use Importable;
/**
* @var string
*/
private $model;
/**
* @var callable
*/
private $mapping;
/**
* @param string $model
* @param callable $mapping
*/
public function __construct(string $model, callable $mapping)
{
$this->model = $model;
$this->mapping = $mapping;
}
/**
* @param array $row
* @return Model|Model[]|null
*/
public function model(array $row)
{
return (new $this->model)->fill(
($this->mapping)($row)
);
}
};
return $import->import($filename, $disk, $readerType);
};
}
}
DownloadCollectionMixin.php 0000644 00000004206 15152066161 0012052 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Sheet;
class DownloadCollectionMixin
{
/**
* @return callable
*/
public function downloadExcel()
{
return function (string $fileName, string $writerType = null, $withHeadings = false, array $responseHeaders = []) {
$export = new class($this, $withHeadings) implements FromCollection, WithHeadings
{
use Exportable;
/**
* @var bool
*/
private $withHeadings;
/**
* @var Collection
*/
private $collection;
/**
* @param Collection $collection
* @param bool $withHeading
*/
public function __construct(Collection $collection, bool $withHeading = false)
{
$this->collection = $collection->toBase();
$this->withHeadings = $withHeading;
}
/**
* @return Collection
*/
public function collection()
{
return $this->collection;
}
/**
* @return array
*/
public function headings(): array
{
if (!$this->withHeadings) {
return [];
}
$firstRow = $this->collection->first();
if ($firstRow instanceof Arrayable || \is_object($firstRow)) {
return array_keys(Sheet::mapArraybleRow($firstRow));
}
return $this->collection->collapse()->keys()->all();
}
};
return $export->download($fileName, $writerType, $responseHeaders);
};
}
}
DownloadQueryMacro.php 0000644 00000003537 15152066161 0011047 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Sheet;
class DownloadQueryMacro
{
public function __invoke()
{
return function (string $fileName, string $writerType = null, $withHeadings = false) {
$export = new class($this, $withHeadings) implements FromQuery, WithHeadings
{
use Exportable;
/**
* @var bool
*/
private $withHeadings;
/**
* @var Builder
*/
private $query;
/**
* @param $query
* @param bool $withHeadings
*/
public function __construct($query, bool $withHeadings = false)
{
$this->query = $query;
$this->withHeadings = $withHeadings;
}
/**
* @return Builder
*/
public function query()
{
return $this->query;
}
/**
* @return array
*/
public function headings(): array
{
if (!$this->withHeadings) {
return [];
}
$firstRow = (clone $this->query)->first();
if ($firstRow) {
return array_keys(Sheet::mapArraybleRow($firstRow));
}
return [];
}
};
return $export->download($fileName, $writerType);
};
}
}
StoreCollectionMixin.php 0000644 00000003660 15152066161 0011402 0 ustar 00 <?php
namespace Maatwebsite\Excel\Mixins;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class StoreCollectionMixin
{
/**
* @return callable
*/
public function storeExcel()
{
return function (string $filePath, string $disk = null, string $writerType = null, $withHeadings = false) {
$export = new class($this, $withHeadings) implements FromCollection, WithHeadings
{
use Exportable;
/**
* @var bool
*/
private $withHeadings;
/**
* @var Collection
*/
private $collection;
/**
* @param Collection $collection
* @param bool $withHeadings
*/
public function __construct(Collection $collection, bool $withHeadings = false)
{
$this->collection = $collection->toBase();
$this->withHeadings = $withHeadings;
}
/**
* @return Collection
*/
public function collection()
{
return $this->collection;
}
/**
* @return array
*/
public function headings(): array
{
if (!$this->withHeadings) {
return [];
}
return is_array($first = $this->collection->first())
? $this->collection->collapse()->keys()->all()
: array_keys($first->toArray());
}
};
return $export->store($filePath, $disk, $writerType);
};
}
}