Skip to content

Commit

Permalink
Merge pull request #2 from lintaba/feature/widenedbuilder
Browse files Browse the repository at this point in the history
widened builder compatibility
  • Loading branch information
lintaba authored Feb 1, 2022
2 parents 44ed899 + fa758ac commit 553bd51
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 74 deletions.
66 changes: 44 additions & 22 deletions src/Exports/QuickExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Lintaba\OrchidTables\Exports;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -14,7 +13,6 @@
use Maatwebsite\Excel\Concerns;
use Orchid\Screen\Layouts\Table;
use Orchid\Screen\TD;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use ReflectionClass;
use ReflectionMethod;
Expand All @@ -23,9 +21,9 @@ class QuickExport extends ExportWithFormats implements Concerns\FromIterator, Co
{
protected $builder;
/** @var Collection|TD[] $columns */
private $columns;
protected $columns;

public function __construct(Builder $builder, Table $table)
public function __construct($builder, Table $table)
{
$this->builder = $builder;

Expand All @@ -44,7 +42,7 @@ public function iterator(): Iterator
yield array_values($headers);
$rowNum++;

foreach ($this->builder->lazy() as $entry) {
foreach ($this->getData() as $entry) {
yield $columns->map(function ($col, $colIndex) use ($entry, $rowNum) {
return $this->exportField($colIndex, $rowNum, $col, $entry);
})->toArray();
Expand All @@ -54,14 +52,14 @@ public function iterator(): Iterator
$this->computedStyles['A1:' . $this->indexToLetter(count($headers)) . '1'] = ExportStyles::FORMAT_BOLD;
}

private function getHeaders(Collection $columns): array
protected function getHeaders(Collection $columns): array
{
return $columns->map(function ($e) {
return $e->getTitle();
})->toArray();
}

private function hackyGetColumns(Table $table)
protected function hackyGetColumns(Table $table)
{
$method = new ReflectionMethod($table, 'columns');
$method->setAccessible(true);
Expand All @@ -71,23 +69,33 @@ private function hackyGetColumns(Table $table)

protected function isDate($value, $entry, $fieldName): bool
{
return $value instanceof DateTimeInterface || $entry->hasCast(
$fieldName,
['date', 'datetime', 'immutable_date', 'immutable_datetime']
) || Str::of($fieldName)->endsWith('_at');
return $value instanceof DateTimeInterface
|| Str::of($fieldName)->endsWith('_at')
|| ($entry instanceof Model && $entry->hasCast(
$fieldName,
['date', 'datetime', 'immutable_date', 'immutable_datetime']
));
}

protected function getName(): string
{
return Str::slug((new ReflectionClass($this->builder->getModel()))->getShortName());
return Str::slug(
(new ReflectionClass(
$this->builder instanceof \Illuminate\Database\Eloquent\Builder ?
$this->builder->getModel() : $this->builder
))->getShortName()
);
}

protected function exportField(int $colIndex, int $rowNum, $col, $entry)
{
$this->computedStyles[$this->indexToLetter($colIndex + 1) . $rowNum] = $col->getStyle($entry);
if (method_exists($col, 'getStyle')) {
$this->computedStyles[$this->indexToLetter($colIndex + 1) . $rowNum] = $col->getStyle($entry);
}

$fieldName = $col->getName();
$value = $entry->getContent($fieldName) ?? data_get($entry, $fieldName);
$value = is_object($entry) && method_exists($entry, 'getContent') ?
$entry->getContent($fieldName) : data_get($entry, $fieldName);

$value = $col->exportGetValue($value, $entry, $rowNum);

Expand All @@ -98,21 +106,35 @@ protected function exportField(int $colIndex, int $rowNum, $col, $entry)
->implode('| ');
}
if ($this->isDate($value, $entry, $fieldName)) {
$value = $entry->{$fieldName} ? Date::dateTimeToExcel($entry->{$fieldName}) : '';
$value = $entry[$fieldName] ? Date::dateTimeToExcel($entry[$fieldName]) : '';
}
if ($value instanceof Model) {
$value = (string)$value;
}
if (is_array($value)) {
$value = implode(', ', array_map(static function ($v) {
if (is_array($v)) {
$v = $v['name'] ?? Arr::first($v);
}

return (string)$v;
}, $value));
$value = implode(
', ',
array_map(static function ($v) {
if (is_array($v)) {
$v = $v['name'] ?? Arr::first($v);
}

return (string)$v;
}, $value)
);
}

return $value;
}

protected function getData()
{
if ($this->builder instanceof \Illuminate\Database\Eloquent\Builder) {
return $this->builder->lazy();
}
if (is_iterable($this->builder)) {
return $this->builder;
}
throw new \RuntimeException('Export needs a builder or iterable.');
}
}
52 changes: 0 additions & 52 deletions src/Mixins/CellExportFormattableMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,56 +104,4 @@ public static function getStyle(): callable
return array_merge_recursive(...$acc);
};
}

public static function exportRender(): callable
{
return function ($renderer): self {
/** @var Cell $this */
$this->exportRenderMethod = $renderer;

return $this;
};
}

public static function exportGetValue(): callable
{
return function (...$arguments) {
/** @var Cell $this */
$callback = $this->exportRenderMethod ?? null;

return $callback === null ? $arguments[0] : $callback(...$arguments);
};
}

public static function notExportable(): callable
{
return function (bool $notExportable = true): self {
/** @var Cell $this */
$this->exportable = !$notExportable;

return $this;
};
}

public static function isExportable(): callable
{
return function (): bool {
/** @var Cell $this */
return $this->exportable ?? true;
};
}

public static function getTitle(): callable
{
return function () {
return $this->title;
};
}

public static function getName(): callable
{
return function () {
return $this->name;
};
}
}
53 changes: 53 additions & 0 deletions src/Mixins/CellMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,57 @@ public static function renderable(): callable
return $this;
};
}


public static function exportRender(): callable
{
return function ($renderer): self {
/** @var Cell $this */
$this->exportRenderMethod = $renderer;

return $this;
};
}

public static function exportGetValue(): callable
{
return function (...$arguments) {
/** @var Cell $this */
$callback = $this->exportRenderMethod ?? null;

return $callback === null ? $arguments[0] : $callback(...$arguments);
};
}

public static function notExportable(): callable
{
return function (bool $notExportable = true): self {
/** @var Cell $this */
$this->exportable = !$notExportable;

return $this;
};
}

public static function isExportable(): callable
{
return function (): bool {
/** @var Cell $this */
return $this->exportable ?? true;
};
}

public static function getTitle(): callable
{
return function () {
return $this->title;
};
}

public static function getName(): callable
{
return function () {
return $this->name;
};
}
}

0 comments on commit 553bd51

Please sign in to comment.