Skip to content

[11.x] Add ability to transform Http\Client\Response into Fluent#53771

Merged
taylorotwell merged 3 commits into
laravel:11.xfrom
stevebauman:http-client-fluent-response
Dec 6, 2024
Merged

[11.x] Add ability to transform Http\Client\Response into Fluent#53771
taylorotwell merged 3 commits into
laravel:11.xfrom
stevebauman:http-client-fluent-response

Conversation

@stevebauman
Copy link
Copy Markdown
Contributor

@stevebauman stevebauman commented Dec 5, 2024

This PR adds a fluent method on the Http\Client\Response class to be able to transform response data into a Fluent instance:

$fluent = Http::get('https://api.example.com/products/1')->fluent();

$fluent->float('price'); // 420.69
$fluent->collect('colors'); // Collection(['red', 'blue'])
$fluent->boolean('on_sale'); // true
$fluent->integer('current_stock'); // 69

*/
public function fluent($key = null)
{
return new Fluent((array) $this->json($key));
Copy link
Copy Markdown
Contributor Author

@stevebauman stevebauman Dec 5, 2024

Choose a reason for hiding this comment

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

We have to make sure we cast this to an array, otherwise PHP will throw a warning on Fluent::__construct due to attempting to iterate over something non-iterable (if the returned value is not an array):

public function __construct($attributes = [])
{
foreach ($attributes as $key => $value) {
$this->attributes[$key] = $value;
}
}

@taylorotwell taylorotwell merged commit 2edd9a9 into laravel:11.x Dec 6, 2024
browner12 pushed a commit to browner12/framework that referenced this pull request Dec 10, 2024
…aravel#53771)

* Add ability to transform a response into a fluent instance

* Add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants