Skip to content

Commit

Permalink
Better type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
spekulatius committed Nov 14, 2022
1 parent b79130f commit 7081e80
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/UsesParsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,25 @@ public function csvDecodeWithHeader(
}

// Helper method to cast types
protected function castType(string $entry)
public function castType(string $entry)
{
// Looks like an int?
if ($entry == (int) $entry) {
if ($entry == (string) (int) $entry) {
return (int) $entry;
}

// Looks like a float?
if ($entry == (float) $entry) {
if ($entry == (string) (float) $entry) {
return (float) $entry;
}

return $entry;
}





/**
* A boilerplate function to process the various calling options for `parseX` functions.
*/
Expand Down Expand Up @@ -235,12 +238,27 @@ public function parseCsv(?string $csvStringOrUrl = null): array
}


// // Associate
// $csv = array_map('str_getcsv', file($file));
// array_walk($csv, function(&$a) use ($csv) {
// $a = array_combine($csv[0], $a);
// });
// array_shift($csv);
























Expand Down

0 comments on commit 7081e80

Please sign in to comment.