Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Illuminate/Support/Traits/InteractsWithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use stdClass;

Expand Down Expand Up @@ -284,6 +285,20 @@ public function float($key, $default = 0.0)
return (float) $this->data($key, $default);
}

/**
* Retrieve data clamped between min and max values.
*
* @param string $key
* @param int|float $min
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
Comment on lines +293 to +300
Copy link
Contributor

@shaedrich shaedrich Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure but people might appreciate a shortcut 🤔

Suggested change
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
* @param int|float|null $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max = null, $default = 0)
{
if (func_num_args() === 2) {
$max = $min;
$min = 0;
}
return Number::clamp($this->data($key, $default), $min, $max);
}

or

Suggested change
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
* @param int|float|null $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max = null, $default = 0)
{
if (func_num_args() === 2) {
[$min, $max] = [0, $min];
}
return Number::clamp($this->data($key, $default), $min, $max);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll leave it as is for now. I have a feeling this one is maybe a bit on the "we already have Number::clamp()" side of the house, but we'll see.


/**
* Retrieve data from the instance as a Carbon instance.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1801,4 +1801,15 @@ public function testItDoesNotGenerateJsonErrorsForEmptyContent()

$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
}

public function testItClampsValues()
{
$request = Request::create('/', 'GET', ['per_page' => 100, 'float' => 9.24]);
$this->assertSame(100, $request->clamp('per_page', 100, 101));
$this->assertSame(10, $request->clamp('per_page', -10, 10));
$this->assertSame(25, $request->clamp('per_page_2', 25, 100, 1));
$this->assertSame(100, $request->clamp('per_page', 1, 250, 99));
$this->assertSame(22.4, $request->clamp('per_page', 1.11, 22.4, 2));
$this->assertSame(9.24, $request->clamp('float', 1, 10));
}
}
Loading