From a9695f4b5728a327a31628470dd6a288d046c47d Mon Sep 17 00:00:00 2001 From: Serhii Yaniuk Date: Fri, 16 May 2025 18:10:31 +0300 Subject: [PATCH] Add dot notation access to Http\Client\Request::data() --- src/Illuminate/Http/Client/Request.php | 17 ++++++++++++----- tests/Http/HttpClientTest.php | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Http/Client/Request.php b/src/Illuminate/Http/Client/Request.php index 7e6891221864..be19af5a8224 100644 --- a/src/Illuminate/Http/Client/Request.php +++ b/src/Illuminate/Http/Client/Request.php @@ -156,17 +156,24 @@ public function hasFile($name, $value = null, $filename = null) /** * Get the request's data (form parameters or JSON). * - * @return array + * @param string|null $key + * @return ($key is null ? array : mixed) */ - public function data() + public function data($key = null) { if ($this->isForm()) { - return $this->parameters(); + $data = $this->parameters(); } elseif ($this->isJson()) { - return $this->json(); + $data = $this->json(); + } else { + $data = $this->data ?? []; + } + + if (is_null($key)) { + return $data; } - return $this->data ?? []; + return Arr::get($data, $key); } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index af70017090ef..817498ee05e8 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -422,6 +422,22 @@ public function testResponseCanBeReturnedAsFluent() $this->assertEquals(new Fluent([]), $response->fluent('missing_key')); } + public function testCanAccessDataOfRequestUsingDotNotation() + { + $this->factory->fake(); + + $this->factory->post('http://foo.com/json', [ + 'parent' => ['name' => 'Taylor'], + ]); + + $this->factory->assertSent(function (Request $request) { + return $request->data() === ['parent' => ['name' => 'Taylor']] && + $request->data('parent') === ['name' => 'Taylor'] && + $request->data('parent.name') === 'Taylor' && + $request->data('data.missing.key') === null; + }); + } + public function testSendRequestBodyAsJsonByDefault() { $body = '{"test":"phpunit"}';