From cf21a14ba6e98c0544584433bff42d44a5aa3cf9 Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Tue, 25 Jun 2024 08:33:10 -0500 Subject: [PATCH 1/2] Add Number::pairs --- src/Illuminate/Support/Number.php | 25 +++++++++++++++++++++++++ tests/Support/SupportNumberTest.php | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 7719055f6224..cfc89856581a 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -232,6 +232,31 @@ public static function clamp(int|float $number, int|float $min, int|float $max) return min(max($number, $min), $max); } + /** + * Split the given number into pairs of min/max values. + * + * @param int|float $number + * @param int|float $split + * @param int|float $offset + * @return array + */ + public static function pairs(int|float $number, int|float $split, int|float $offset = 1) + { + $output = []; + + for ($lower = 0; $lower < $number; $lower += $split) { + $upper = $lower + $split; + + if ($upper > $number) { + $upper = $number; + } + + $output[] = [$lower + $offset, $upper]; + } + + return $output; + } + /** * Execute the given callback using the given locale. * diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index e54c03d77d1c..d62f8716b250 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -286,4 +286,11 @@ public function testSummarize() $this->assertSame('-1Q', Number::abbreviate(-1000000000000000)); $this->assertSame('-1KQ', Number::abbreviate(-1000000000000000000)); } + + public function testPairs() + { + $this->assertSame([[1,10],[11,20],[21,25]], Number::pairs(25, 10)); + $this->assertSame([[0,10],[10,20],[20,25]], Number::pairs(25, 10, 0)); + $this->assertSame([[0,2.5],[2.5,5.0],[5.0,7.5],[7.5,10.0]], Number::pairs(10, 2.5, 0)); + } } From 849b4d6deb22d345607a92dc6d871dfe845cba04 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 27 Jun 2024 10:27:09 +0200 Subject: [PATCH 2/2] formatting --- src/Illuminate/Support/Number.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index cfc89856581a..1a6a3f85dfe8 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -235,20 +235,20 @@ public static function clamp(int|float $number, int|float $min, int|float $max) /** * Split the given number into pairs of min/max values. * - * @param int|float $number - * @param int|float $split + * @param int|float $to + * @param int|float $by * @param int|float $offset * @return array */ - public static function pairs(int|float $number, int|float $split, int|float $offset = 1) + public static function pairs(int|float $to, int|float $by, int|float $offset = 1) { $output = []; - for ($lower = 0; $lower < $number; $lower += $split) { - $upper = $lower + $split; + for ($lower = 0; $lower < $to; $lower += $by) { + $upper = $lower + $by; - if ($upper > $number) { - $upper = $number; + if ($upper > $to) { + $upper = $to; } $output[] = [$lower + $offset, $upper];