Skip to content

Commit

Permalink
Merge pull request #79 from AsherMoshav/main
Browse files Browse the repository at this point in the history
add type casting for strval function
  • Loading branch information
roberto-butti authored Oct 2, 2023
2 parents d393069 + fed4cb7 commit 460f6b2
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ public function groupBy(string|int $field): Table

foreach ($this->rows as $value) {
$property = $value->get($field);
if (array_key_exists(strval($property), $result)) {
$property = $this->castVariableForStrval($property);

if (!$property
|| array_key_exists(strval($property), $result)) {
continue;
}
$result[$property] = $value;
Expand All @@ -187,6 +190,21 @@ public function groupBy(string|int $field): Table
return self::make(array_values($result));
}


/**
* strval sometimes crashes when passed a mixed value, this fixes that problem.
*/
private function castVariableForStrval(mixed $property): bool|float|int|string|null
{
return match(gettype($property)) {
'boolean' => (bool) $property,
'double' => (float) $property,
'integer' => (int) $property,
'string' => (string) $property,
default => null,
};
}

/**
* Transform allows you to run a function over an entire table on a specific row
*/
Expand Down

0 comments on commit 460f6b2

Please sign in to comment.