diff --git a/src/Table.php b/src/Table.php index 1d4990e..b26a656 100644 --- a/src/Table.php +++ b/src/Table.php @@ -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; @@ -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 */