Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Adds datetime parsing to Request instance #39945

Merged
merged 12 commits into from
Dec 17, 2021
22 changes: 22 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Date;
use SplFileInfo;
use stdClass;
use Symfony\Component\VarDumper\VarDumper;
Expand Down Expand Up @@ -296,6 +297,27 @@ public function boolean($key = null, $default = false)
return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN);
}

/**
* Retrieve input from the request as a Carbon instance.
*
* @param string $key
* @param string|null $format
* @param string|null $tz
* @return \Illuminate\Support\Carbon|null
*/
public function date($key, $format = null, $tz = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not $timezone?

{
if ($this->isNotFilled($key)) {
return null;
}

if (is_null($format)) {
return Date::parse($this->input($key), $tz);
}

return Date::createFromFormat($format, $this->input($key), $tz);
}

/**
* Retrieve input from the request as a collection.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Route;
use Illuminate\Session\Store;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand Down Expand Up @@ -532,6 +534,55 @@ public function testCollectMethod()
$this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com'], $request->collect()->all());
}

public function testDateMethod()
{
$request = Request::create('/', 'GET', [
'as_null' => null,
'as_invalid' => 'invalid',

'as_datetime' => '20-01-01 16:30:25',
'as_format' => '1577896225',
'as_timezone' => '20-01-01 13:30:25',

'as_date' => '2020-01-01',
'as_time' => '16:30:25',
]);

$current = Carbon::create(2020, 1, 1, 16, 30, 25);

$this->assertNull($request->date('as_null'));
$this->assertNull($request->date('doesnt_exists'));

$this->assertEquals($current, $request->date('as_datetime'));
$this->assertEquals($current, $request->date('as_format', 'U'));
$this->assertEquals($current, $request->date('as_timezone', null, 'America/Santiago'));

$this->assertTrue($request->date('as_date')->isSameDay($current));
$this->assertTrue($request->date('as_time')->isSameSecond('16:30:25'));
}

public function testDateMethodExceptionWhenValueInvalid()
{
$this->expectException(InvalidArgumentException::class);

$request = Request::create('/', 'GET', [
'date' => 'invalid',
]);

$request->date('date');
}

public function testDateMethodExceptionWhenFormatInvalid()
{
$this->expectException(InvalidArgumentException::class);

$request = Request::create('/', 'GET', [
'date' => '20-01-01 16:30:25',
]);

$request->date('date', 'invalid_format');
}

public function testArrayAccess()
{
$request = Request::create('/', 'GET', ['name' => null, 'foo' => ['bar' => null, 'baz' => '']]);
Expand Down