diff --git a/typo3/sysext/core/Classes/Utility/ArrayUtility.php b/typo3/sysext/core/Classes/Utility/ArrayUtility.php index 4e1bf28a286151a4f7a9cda38b2a34214e7e0ad8..0f347206b170d7bcf9d77e3c91a86f51b3b9ae4b 100644 --- a/typo3/sysext/core/Classes/Utility/ArrayUtility.php +++ b/typo3/sysext/core/Classes/Utility/ArrayUtility.php @@ -361,10 +361,16 @@ class ArrayUtility return $arrays; } $sortResult = uasort($arrays, static function (array $a, array $b) use ($key, $ascending) { - if (!isset($a[$key]) || !isset($b[$key])) { + if (!isset($a[$key], $b[$key])) { throw new \RuntimeException('The specified sorting key "' . $key . '" is not available in the given array.', 1373727309); } - return $ascending ? strcasecmp($a[$key], $b[$key]) : strcasecmp($b[$key], $a[$key]); + if (!is_scalar($a[$key])) { + throw new \RuntimeException(sprintf('The specified sorting key "%s" is not a scalar value, given "%s".', $key, gettype($a[$key])), 1373727310); + } + if (!is_scalar($b[$key])) { + throw new \RuntimeException(sprintf('The specified sorting key "%s" is not a scalar value, given "%s".', $key, gettype($b[$key])), 1373727311); + } + return $ascending ? strcasecmp((string)$a[$key], (string)$b[$key]) : strcasecmp((string)$b[$key], (string)$a[$key]); }); if (!$sortResult) { throw new \RuntimeException('The function uasort() failed for unknown reasons.', 1373727329); diff --git a/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php index cf9ddea1e563c8ac5a4f9e2ff558cca2389c966b..9c5890b1a1fcf6694ca3396047ad5838925d1c53 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php @@ -1042,6 +1042,82 @@ final class ArrayUtilityTest extends UnitTestCase ], ], ], + 'order by integers as string' => [ + [ + 0 => [ + 'uid' => '23', + 'title' => 'b', + 'dummy' => 4, + ], + 1 => [ + 'uid' => '22', + 'title' => 'c', + 'dummy' => 2, + ], + 2 => [ + 'uid' => '24', + 'title' => 'a', + 'dummy' => 3, + ], + ], + 'uid', + true, + [ + 1 => [ + 'uid' => '22', + 'title' => 'c', + 'dummy' => 2, + ], + 0 => [ + 'uid' => '23', + 'title' => 'b', + 'dummy' => 4, + ], + 2 => [ + 'uid' => '24', + 'title' => 'a', + 'dummy' => 3, + ], + ], + ], + 'order by integers' => [ + [ + 0 => [ + 'uid' => 23, + 'title' => 'b', + 'dummy' => 4, + ], + 1 => [ + 'uid' => 22, + 'title' => 'c', + 'dummy' => 2, + ], + 2 => [ + 'uid' => 24, + 'title' => 'a', + 'dummy' => 3, + ], + ], + 'uid', + true, + [ + 1 => [ + 'uid' => 22, + 'title' => 'c', + 'dummy' => 2, + ], + 0 => [ + 'uid' => 23, + 'title' => 'b', + 'dummy' => 4, + ], + 2 => [ + 'uid' => 24, + 'title' => 'a', + 'dummy' => 3, + ], + ], + ], ]; } @@ -1067,6 +1143,48 @@ final class ArrayUtilityTest extends UnitTestCase ArrayUtility::sortArraysByKey([['a'], ['a']], 'dummy'); } + #[Test] + public function sortArraysByKeyThrowsExceptionForNonScalarKeyA(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(1373727310); + + ArrayUtility::sortArraysByKey( + [ + [ + 'uid' => '23', + 'value' => new \stdClass(), + ], + 22 => [ + 'uid' => '22', + 'value' => 123, + ], + ], + 'value' + ); + } + + #[Test] + public function sortArraysByKeyThrowsExceptionForNonScalarKeyB(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(1373727311); + + ArrayUtility::sortArraysByKey( + [ + [ + 'uid' => '23', + 'value' => 123, + ], + 22 => [ + 'uid' => '22', + 'value' => [], + ], + ], + 'value' + ); + } + /////////////////////// // Tests concerning arrayExport ///////////////////////