| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Service; |
| 4 |
|
| 5 |
use App\Billingo\Enums\HttpMethodEnum; |
| 6 |
|
| 7 |
class ApiContext |
| 8 |
{ |
| 9 |
protected string $url; |
| 10 |
protected array $content = []; |
| 11 |
protected HttpMethodEnum $method; |
| 12 |
|
| 13 |
public function getUrl(): string |
| 14 |
{ |
| 15 |
return $this->url; |
| 16 |
} |
| 17 |
|
| 18 |
public function setUrl(string $url): self |
| 19 |
{ |
| 20 |
$this->url = $url; |
| 21 |
|
| 22 |
return $this; |
| 23 |
} |
| 24 |
|
| 25 |
public function getContent(): array |
| 26 |
{ |
| 27 |
return $this->content; |
| 28 |
} |
| 29 |
|
| 30 |
public function setContent(array $content): self |
| 31 |
{ |
| 32 |
$this->content = $content; |
| 33 |
|
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
public function getMethod(): HttpMethodEnum |
| 38 |
{ |
| 39 |
return $this->method; |
| 40 |
} |
| 41 |
|
| 42 |
public function setMethod(HttpMethodEnum $method): self |
| 43 |
{ |
| 44 |
$this->method = $method; |
| 45 |
|
| 46 |
return $this; |
| 47 |
} |
| 48 |
|
| 49 |
public function fromArray(array $apiContext): self |
| 50 |
{ |
| 51 |
$callableFunctions = [ |
| 52 |
'url' => 'setUrl', |
| 53 |
'method' => 'setMethod', |
| 54 |
'content' => 'setContent', |
| 55 |
]; |
| 56 |
|
| 57 |
array_walk($callableFunctions, function ($function, $name) use ($apiContext) { |
| 58 |
|
| 59 |
if (isset($apiContext[$name])) { |
| 60 |
$this->$function($apiContext[$name]); |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
public function selfControl(): bool |
| 68 |
{ |
| 69 |
return !empty($this->url) && !empty($this->method); |
| 70 |
} |
| 71 |
} |
| 72 |
|