Skip to content

Commit

Permalink
improve headers for object mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Nov 4, 2023
1 parent bea38a9 commit b179748
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function filter(Closure $callback)
*/
public function mapToObject(string $customClass = null)
{
$this->mapToHeaders();
$this->processor->mapToObject = true;
$this->processor->customObjectClass = $customClass;

Expand Down
11 changes: 10 additions & 1 deletion src/CsvProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ public function createObjectInstance(array $row)

public function normalizeHeaders($headers)
{
return array_map(fn ($header) => str_replace(' ', '_', strtolower((string) $header)), $headers);
return array_map(
function ($header) {
$header = str_replace(' ', '_', $header);
$header = str_replace(['(', ')'], '', $header);
$header = preg_replace('/[^a-zA-Z0-9_]/', '_', $header);

return strtolower($header);
},
$headers
);
}

public function skipColumnsByHeaderName($row)
Expand Down

0 comments on commit b179748

Please sign in to comment.