Skip to content

Commit c5b52f9

Browse files
committed
Add interaction with Eloquent models, move traits to their namespace
1 parent 9ed8d1d commit c5b52f9

7 files changed

+92
-3
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"symfony/http-client": "^4.3",
88
"illuminate/support": "^5.8"
99
},
10+
"suggest": {
11+
"illuminate/database": "Allows to send request with data from model"
12+
},
1013
"autoload": {
1114
"psr-4": {
1215
"Ivan770\\HttpClient\\": "src/"

src/Exceptions/ClassIsNotModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
use Exception;
6+
7+
class ClassIsNotModel extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null) {
10+
parent::__construct($message, $code, $previous);
11+
}
12+
13+
public function __toString() {
14+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Exceptions;
4+
5+
use Exception;
6+
7+
class EloquentNotAvailable extends Exception
8+
{
9+
public function __construct($message, $code = 0, Exception $previous = null) {
10+
parent::__construct($message, $code, $previous);
11+
}
12+
13+
public function __toString() {
14+
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
15+
}
16+
17+
}

src/HttpClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Ivan770\HttpClient;
44

55
use Illuminate\Support\Traits\Macroable;
6+
use Ivan770\HttpClient\Traits\Buildable;
7+
use Ivan770\HttpClient\Traits\InteractsWithEloquent;
8+
use Ivan770\HttpClient\Traits\Requestable;
69
use Symfony\Component\HttpClient\HttpClient as Client;
710

811
/**
@@ -14,7 +17,7 @@
1417
*/
1518
class HttpClient
1619
{
17-
use Buildable, Requestable, Macroable {
20+
use InteractsWithEloquent, Buildable, Requestable, Macroable {
1821
Macroable::__call as macroCall;
1922
}
2023

src/Buildable.php renamed to src/Traits/Buildable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Ivan770\HttpClient;
3+
namespace Ivan770\HttpClient\Traits;
44

55
trait Buildable
66
{

src/Traits/InteractsWithEloquent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Ivan770\HttpClient\Traits;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Ivan770\HttpClient\Exceptions\ClassIsNotModel;
7+
use Ivan770\HttpClient\Exceptions\EloquentNotAvailable;
8+
9+
trait InteractsWithEloquent
10+
{
11+
protected $model;
12+
13+
protected function eloquentAvailable()
14+
{
15+
if (class_exists(Model::class)) {
16+
return true;
17+
}
18+
throw new EloquentNotAvailable("Eloquent model class not found");
19+
}
20+
21+
protected function isModel($class)
22+
{
23+
if ($class instanceof Model) {
24+
return true;
25+
}
26+
throw new ClassIsNotModel("Provided class is not suitable for interaction");
27+
}
28+
29+
public function setModel($model)
30+
{
31+
if ($this->eloquentAvailable() && $this->isModel($model)) {
32+
$this->model = $model;
33+
}
34+
return $this;
35+
}
36+
37+
public function fetchModel($model = null)
38+
{
39+
if (!is_null($model)) {
40+
$this->setModel($model);
41+
}
42+
if ($this->isModel($this->model)) {
43+
$this->applyRequestOptions(["json" => $this->model->toArray()]);
44+
return $this;
45+
}
46+
}
47+
}

src/Requestable.php renamed to src/Traits/Requestable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
namespace Ivan770\HttpClient;
3+
namespace Ivan770\HttpClient\Traits;
4+
5+
use Ivan770\HttpClient\Response;
46

57
trait Requestable
68
{

0 commit comments

Comments
 (0)