Skip to content

Commit

Permalink
Update ExportExcelClass.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSantiago82 authored Dec 21, 2022
1 parent a62d73c commit b7f79d8
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions src/Classes/ExportExcelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class ExportExcelClass implements FromCollection, WithHeadings
private $collection_formats;
private $exceptions;

public function __construct($type, $collection_to_export, $collection_headings = null, $exceptions = null, $collection_format = null) {

public function __construct($type, $collection_to_export, $collection_headings = null, $exceptions = null, $collection_format = null)
{
$this->type = $type;
$this->collection = $collection_to_export;
$this->headings = $collection_headings;
Expand All @@ -27,72 +29,52 @@ public function __construct($type, $collection_to_export, $collection_headings =
$this->getExportedCollection();
}


public function getExportedCollection()
{
$collectionKeys = array_keys($this->headings);

$subCollection = $this->collection->map(function ($collectionItem, $collectionKey) use ($collectionKeys) {
$result = [];

foreach ($this->headings as $itemKey => $itemValue) {
$collection = Str::of($itemKey)->explode('.');
$val = $collectionItem;

foreach ($collection as $rel) {
if ($val === null) {
break;
}
$val = optional($val)->$rel;

if($itemValue['type'] == 'date')
{
$result[$itemKey] = $val->format($itemValue['format']);
}else{
$result[$itemKey] = $val;
}
}

}
$collectionResult = collect($result);

$newCollectionItem = collect($collectionItem);
$newCollectionItem = [];

foreach ($this->collection_formats as $keyFormat => $value)
{
if($value['type'] == 'date')
{
$newCollectionItem[$keyFormat] = $collectionItem->$keyFormat->format($value['format']);
}
}

if($this->type == 'full_collection') {
$exceptions = array_keys($this->exceptions);
foreach ($this->collection as $collection) {

$mergeCollection = collect($newCollectionItem)->except($collectionKeys)->except($exceptions);
foreach ($this->headings as $itemKey => $itemValue) {

$fullCollection = $collectionResult->merge($mergeCollection);
$this->collectionHeadings = $fullCollection->keys()->toArray();
if (Str::contains($itemKey, '.')) {
$cols = Str::of($itemKey)->explode('.');

$colnew = $collection;
foreach ($cols as $col) {
if ($colnew !== null) {
$colnew = $colnew->$col;
}
}

return $fullCollection->all();
$result[$itemKey] = $colnew;

}
elseif($this->type == 'merged_collection')
{
return $collectionResult->all();
} elseif ($itemValue['type'] == 'date') {
$result[$itemKey] = optional($collection->$itemKey)->format($itemValue['format']);
} else {
$result[$itemKey] = $collection->$itemKey;
}
}

});
$newCollectionItem[] = $result;
}

$this->headings = $this->collectionHeadings;
$this->collection = $subCollection;
$this->headings = $collectionKeys;
$this->collection = collect($newCollectionItem);
}


public function collection()
{
return $this->collection;
}


public function headings(): array
{
return $this->headings;
Expand Down

0 comments on commit b7f79d8

Please sign in to comment.