Skip to content

Commit 4eb5cc5

Browse files
committed
Use Arrayable parsing instead of separate Eloquent trait
1 parent cac5327 commit 4eb5cc5

File tree

7 files changed

+26
-67
lines changed

7 files changed

+26
-67
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ $client->authBasic(["username", "password"])->get("https://example.com");
118118
$client->authBearer("tokenhere")->get("https://example.com");
119119
```
120120

121-
### Eloquent interaction
122-
You can use your Eloquent models as data source for request
121+
### Arrayable parsing
122+
You can use any Arrayable class (Eloquent models, collections, etc.) as data source for request
123123
```php
124124
$model = User::find(1);
125-
$client->setModel($model)->fetchModel()->post("https://example.com");
126-
// You can also use short variant
127-
$client->fetchModel($model)->post("https://example.com");
125+
$client->parse($model)->post("https://example.com");
128126
```
129127

130128
### Data pipelining

src/Builder.php

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ivan770\HttpClient;
44

5+
use Illuminate\Contracts\Support\Arrayable;
6+
57
class Builder
68
{
79
/**
@@ -55,24 +57,24 @@ public function auth($type, $credentials)
5557
/**
5658
* Add HTTP basic auth to request
5759
*
58-
* @param array|string $credentials "Authentication credentials"
60+
* @param array|string $credentials Authentication credentials
5961
* @return $this
6062
*/
6163
public function authBasic($credentials)
6264
{
63-
$this->applyRequestOptions(["auth_basic" => $credentials]);
65+
$this->applyRequestOptions(['auth_basic' => $credentials]);
6466
return $this;
6567
}
6668

6769
/**
6870
* Add Bearer token to request
6971
*
70-
* @param string $credentials "Bearer token"
72+
* @param string $credentials Bearer token
7173
* @return $this
7274
*/
7375
public function authBearer($credentials)
7476
{
75-
$this->applyRequestOptions(["auth_bearer" => $credentials]);
77+
$this->applyRequestOptions(['auth_bearer' => $credentials]);
7678
return $this;
7779
}
7880

@@ -84,7 +86,7 @@ public function authBearer($credentials)
8486
*/
8587
public function headers($headers)
8688
{
87-
$this->applyRequestOptions(["headers" => $headers]);
89+
$this->applyRequestOptions(['headers' => $headers]);
8890
return $this;
8991
}
9092

@@ -96,7 +98,7 @@ public function headers($headers)
9698
*/
9799
public function body($body)
98100
{
99-
$this->applyRequestOptions(["body" => $body]);
101+
$this->applyRequestOptions(['body' => $body]);
100102
return $this;
101103
}
102104

@@ -108,7 +110,7 @@ public function body($body)
108110
*/
109111
public function json($json)
110112
{
111-
$this->applyRequestOptions(["json" => $json]);
113+
$this->applyRequestOptions(['json' => $json]);
112114
return $this;
113115
}
114116

@@ -120,7 +122,7 @@ public function json($json)
120122
*/
121123
public function query($query)
122124
{
123-
$this->applyRequestOptions(["query" => $query]);
125+
$this->applyRequestOptions(['query' => $query]);
124126
return $this;
125127
}
126128

@@ -131,7 +133,7 @@ public function query($query)
131133
*/
132134
public function withoutRedirects()
133135
{
134-
$this->applyRequestOptions(["max_redirects" => 0]);
136+
$this->applyRequestOptions(['max_redirects' => 0]);
135137
return $this;
136138
}
137139

@@ -144,7 +146,13 @@ public function withoutRedirects()
144146
*/
145147
public function proxy($proxy = null, $noproxy = null)
146148
{
147-
$this->applyRequestOptions(["proxy" => $proxy, "no_proxy" => $noproxy]);
149+
$this->applyRequestOptions(['proxy' => $proxy, 'no_proxy' => $noproxy]);
150+
return $this;
151+
}
152+
153+
public function parse(Arrayable $arrayable)
154+
{
155+
$this->applyRequestOptions(['json' => $arrayable->toArray()]);
148156
return $this;
149157
}
150158
}

src/Facade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Facade extends Base
88
{
99
protected static function getFacadeAccessor()
1010
{
11-
return "HttpClient";
11+
return 'HttpClient';
1212
}
1313
}

src/HttpClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
class HttpClient
3535
{
36-
use InteractsWithEloquent, Requestable, Macroable {
36+
use Requestable, Macroable {
3737
Macroable::__call as macroCall;
3838
}
3939

src/Response/MockResponse.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function pipelineAvailable()
6262
if (class_exists(Pipeline::class)) {
6363
return true;
6464
}
65-
throw new PipelineNotAvailable("Pipeline class cannot be found");
65+
throw new PipelineNotAvailable('Pipeline class cannot be found');
6666
}
6767

6868
/**
@@ -107,7 +107,7 @@ public function toCollection()
107107
return $this->data;
108108
}
109109

110-
throw new DataIsNotCollection("Passed data has to be collection instance");
110+
throw new DataIsNotCollection('Passed data has to be collection instance');
111111
}
112112

113113
/**

src/Response/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function pipelineAvailable()
3434
if (class_exists(Pipeline::class)) {
3535
return true;
3636
}
37-
throw new PipelineNotAvailable("Pipeline class cannot be found");
37+
throw new PipelineNotAvailable('Pipeline class cannot be found');
3838
}
3939

4040
protected function getContainer()

src/Traits/InteractsWithEloquent.php

-47
This file was deleted.

0 commit comments

Comments
 (0)