From fed4cb7cd70f4b48242418ba4d2b8410a71c72c6 Mon Sep 17 00:00:00 2001 From: Asher Moshav Date: Sun, 1 Oct 2023 23:25:43 +0300 Subject: [PATCH] add type casting for strval function --- src/Table.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 */