diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 7719055f6224..1a6a3f85dfe8 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 $to + * @param int|float $by + * @param int|float $offset + * @return array + */ + public static function pairs(int|float $to, int|float $by, int|float $offset = 1) + { + $output = []; + + for ($lower = 0; $lower < $to; $lower += $by) { + $upper = $lower + $by; + + if ($upper > $to) { + $upper = $to; + } + + $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)); + } }