Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Oct 17, 2023
1 parent 0bf1541 commit 500517a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
25 changes: 16 additions & 9 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Heller\SimpleCsv;

use Closure;

class Csv
{
protected $processor;
Expand Down Expand Up @@ -44,21 +46,18 @@ public function delimiter(string $delimiter)
* @param int|array $headerRow The header row number or an array of header names
* @return $this
*/
public function mapToHeaders(array|int $headerRow = 1)
public function mapToHeaders(array|int|bool $headerRow = 1)
{
$this->processor->mapToHeaderRow = true;
$this->processor->shouldMapToHeaders = (bool) $headerRow;

if (is_array($headerRow)) {
$this->skipRows([]);
$this->processor->headers = $headerRow;

return $this;
return $this->setHeaders($headerRow);
}

$this->setHeaderRow($headerRow);

// Skip the header row when processing the CSV
$this->skipRows([$this->processor->headerRow]);
$this->skipRows([$headerRow]);

return $this;
}
Expand All @@ -77,6 +76,14 @@ public function setHeaderRow(int $row)
return $this;
}

public function setHeaders(array $headers)
{
$this->skipRows([]);
$this->processor->headers = $headers;

return $this;
}

/**
* Get the header row as an array.
*
Expand Down Expand Up @@ -116,10 +123,10 @@ public function skipColumns(int|array $columns)
/**
* Filter the rows with a callback function.
*
* @param callable $callback The callback function to filter the rows
* @param Closure $callback The callback function to filter the rows
* @return $this
*/
public function filter(callable $callback)
public function filter(Closure $callback)
{
$this->processor->filterCallback = $callback;

Expand Down
47 changes: 28 additions & 19 deletions src/CsvProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

namespace Heller\SimpleCsv;

use Closure;
use Heller\SimpleCsv\Support\FileHandler;

class CsvProcessor
{
public $mapToHeaderRow = false;
public $shouldMapToHeaders = false;

public $headerRow;
public int $headerRow = 1;

public array $skipRows = [];

public array $skipColumns = [];

public $filterCallback;
public ?Closure $filterCallback = null;

public bool $mapToObject = false;

public $customObjectClass;
public ?string $customObjectClass = null;

public string $delimiter = ',';

public array $headers = [];

public $fileHandler;

public function __construct(FileHandler $fileHandler)
public function __construct(public FileHandler $fileHandler)
{
$this->fileHandler = $fileHandler;
}

public function process()
Expand All @@ -48,7 +46,7 @@ public function process()

$row = $this->processRow($row);

if ($this->filterCallback && ! call_user_func($this->filterCallback, $row)) {
if ($this->filterCallback instanceof \Closure && ! call_user_func($this->filterCallback, $row)) {
continue; // Skip rows that don't match the filter criteria
}

Expand All @@ -66,22 +64,33 @@ public function process()
*/
public function processRow(array $row)
{
// Read CSV file and handle header row
$header = $this->mapToHeaderRow ? $this->getHeaderRow() : null;

// dd($header);
$row = $this->skipColumnsByIndex($row);
$row = $this->skipColumnsByHeaderName($row);

if ($this->mapToHeaderRow) {
if ($this->mapToObject) {
$header = $this->normalizeHeaders($header);
}
$row = $this->combineWithHeader($header, $row); // Combine with header if applicable
if (! $this->shouldMapToHeaders) {
return $row;
}

// Read CSV file and handle header row
$header = $this->getHeaderRow();

if (! $header) {
return $row;
}

$header = $this->mapToObject ? $this->normalizeHeaders($header) : $header;

$row = $this->combineWithHeader(
$header,
$row
);

if (! $this->mapToObject) {
return $row;
}

// Create an object instance based on user preference (stdClass or custom class)
return $this->mapToObject ? $this->createObjectInstance($row) : $row;
return $this->createObjectInstance($row);
}

public function getHeaderRow()
Expand Down

0 comments on commit 500517a

Please sign in to comment.