Skip to content

Commit

Permalink
refactoring collection()
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed May 25, 2019
1 parent 9fe2cbe commit 46b8792
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/Traits/SheetsCollection.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Revolution\Google\Sheets\Traits;

use Illuminate\Support\Collection;

trait SheetsCollection
{
/**
* @return \Illuminate\Support\Collection
* @return Collection
*/
public function get(): Collection
{
Expand All @@ -16,31 +17,17 @@ public function get(): Collection
}

/**
* @param array $header
* @param array|\Illuminate\Support\Collection $rows
* @param array $header
* @param array|Collection $rows
*
* @return \Illuminate\Support\Collection
* @return Collection
*/
public function collection(array $header, $rows): Collection
{
$collection = [];

if ($rows instanceof Collection) {
$rows = $rows->toArray();
}

foreach ($rows as $row) {
$col = [];

foreach ($header as $index => $head) {
$col[$head] = empty($row[$index]) ? '' : $row[$index];
}

if (!empty($col)) {
$collection[] = $col;
}
}
return Collection::make($rows)->map(function ($item) use ($header) {
$row = Collection::make($item)->pad(count($header), '');

return Collection::make($collection);
return Collection::make($header)->combine($row);
});
}
}

0 comments on commit 46b8792

Please sign in to comment.