1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Vultr PHP library. |
4
|
|
|
* |
5
|
|
|
* (c) Albert Leitato <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Vultr; |
12
|
|
|
|
13
|
|
|
/* |
14
|
|
|
* Vultr.com API Client. |
15
|
|
|
* |
16
|
|
|
* @version 1.0 |
17
|
|
|
* |
18
|
|
|
* @author Albert Leitato <[email protected]> |
19
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
20
|
|
|
* |
21
|
|
|
* @see https://github.com/weezqyd/vultr-php |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
use Http\Exceptions; |
25
|
|
|
use Http\Adapter\GuzzleHttpAdapter; |
26
|
|
|
use Http\Adapter\AdapterInterface; |
27
|
|
|
|
28
|
|
|
class Vultr |
29
|
|
|
{ |
30
|
|
|
use Support\Str; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AdapterInterface |
34
|
|
|
*/ |
35
|
|
|
protected $adapter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new Vultr API instance. |
39
|
|
|
* |
40
|
|
|
* This is the entry point to the api resource |
41
|
|
|
* |
42
|
|
|
* @param AdapterInterface $adapter |
43
|
|
|
*/ |
44
|
|
|
public function __construct($token, AdapterInterface $adapter = null) |
45
|
|
|
{ |
46
|
|
|
$this->createAdapter($adapter, $token); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Call API resources as though the were Vultr::class properties. |
51
|
|
|
* |
52
|
|
|
* @param mixed $resource |
53
|
|
|
* |
54
|
|
|
* @return mixed Api Resource if it exists |
55
|
|
|
**/ |
56
|
|
|
public function __get($resource) |
57
|
|
|
{ |
58
|
|
|
return $this->getClass($resource); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create resource instance. |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
**/ |
66
|
|
|
protected function getClass($class) |
67
|
|
|
{ |
68
|
|
|
$resource = static::studly($class); |
69
|
|
|
$class = 'Vultr\\Api\\'.$resource; |
70
|
|
|
if (\class_exists($class)) { |
71
|
|
|
return new $class($this->adapter); |
72
|
|
|
} |
73
|
|
|
throw new Exceptions\ErrorException("The class $class does not exist"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the default client adapter. |
78
|
|
|
* |
79
|
|
|
* @param \Http\Adapter\AdapterInterface $adapter |
80
|
|
|
**/ |
81
|
|
|
public function setClient(AdapterInterface $adapter) |
82
|
|
|
{ |
83
|
|
|
$this->adapter = $adapter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set up the Guzzle adapter. |
88
|
|
|
* |
89
|
|
|
* @param string $token Api Token |
90
|
|
|
**/ |
91
|
|
|
protected function createGuzzleAdapter($token) |
92
|
|
|
{ |
93
|
|
|
$options = ['headers' => [ |
94
|
|
|
'API-Key' => $token, |
95
|
|
|
'Accept' => 'application/json', |
96
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
$adapter = new GuzzleHttpAdapter($options); |
100
|
|
|
$this->setClient($adapter); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a client adapter. |
105
|
|
|
* |
106
|
|
|
* @param Http\Adapter\AdapterInterface|null $adapter |
107
|
|
|
* @param string|null $token API Token |
108
|
|
|
**/ |
109
|
|
|
protected function createAdapter($adapter, $token = null) |
110
|
|
|
{ |
111
|
|
|
if ($adapter instanceof AdapterInterface) { |
112
|
|
|
$this->setClient($adapter); |
113
|
|
|
} else { |
114
|
|
|
$this->createGuzzleAdapter($token); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.