|
2 | 2 |
|
3 | 3 | namespace Swader\Diffbot\Abstracts; |
4 | 4 |
|
| 5 | +use Swader\Diffbot\Exceptions\DiffbotException; |
| 6 | + |
5 | 7 | /** |
6 | 8 | * Class Api |
7 | 9 | * @package Swader\Diffbot\Abstracts |
8 | 10 | */ |
9 | | -abstract class Api |
| 11 | +abstract class Api implements \Swader\Diffbot\Interfaces\Api |
10 | 12 | { |
11 | 13 | /** @var int Timeout value in ms - defaults to 30s if empty */ |
12 | 14 | private $timeout = 30000; |
13 | 15 |
|
14 | 16 | /** @var string The URL onto which to unleash the API in question */ |
15 | 17 | private $url; |
16 | 18 |
|
| 19 | + /** @var string API URL to which to send the request */ |
| 20 | + protected $apiUrl; |
| 21 | + |
| 22 | + /** @var array Settings on which optional fields to fetch */ |
| 23 | + protected $fieldSettings = []; |
| 24 | + |
17 | 25 | public function __construct($url) |
18 | 26 | { |
19 | 27 | if (!is_string($url)) { |
@@ -60,4 +68,27 @@ public function setTimeout($timeout = null) |
60 | 68 | $this->timeout = $timeout; |
61 | 69 | return $this; |
62 | 70 | } |
| 71 | + |
| 72 | + public function __call($name, $arguments) |
| 73 | + { |
| 74 | + $prefix = substr(lcfirst($name), 0, 3); |
| 75 | + $field = lcfirst(substr($name, 3)); |
| 76 | + |
| 77 | + $fields = static::getOptionalFields(); |
| 78 | + if (in_array($field, $fields)) { |
| 79 | + if ($prefix == 'get') { |
| 80 | + return (isset($this->fieldSettings[$field])) ? $this->fieldSettings[$field] : false; |
| 81 | + } |
| 82 | + |
| 83 | + if ($prefix == 'set') { |
| 84 | + if (is_bool($arguments[0]) || $arguments[0] === null) { |
| 85 | + $this->fieldSettings[$field] = (bool)$arguments[0]; |
| 86 | + return $this; |
| 87 | + } |
| 88 | + throw new \InvalidArgumentException('Only booleans and null are allowed as optional field flags!'); |
| 89 | + } |
| 90 | + throw new \BadMethodCallException('Prefix "'.$prefix.'" not allowed.'); |
| 91 | + } |
| 92 | + throw new \BadMethodCallException($name . ': such a field does not exist for this API class.'); |
| 93 | + } |
63 | 94 | } |
0 commit comments