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

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

Merged
merged 3 commits into from
Dec 6, 2024

Conversation

stevebauman
Copy link
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
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
40 checks passed
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