1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Artstorm\MonkeyLearn; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Artstorm\MonkeyLearn\HttpClient\HttpClient; |
8
|
|
|
use Artstorm\MonkeyLearn\HttpClient\HttpClientInterface; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* MonkeyLearn API base uri. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
const BASE_URI = 'https://api.monkeylearn.com/v2/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* HTTP client used for communication. |
21
|
|
|
* |
22
|
|
|
* @var HttpClientInterface |
23
|
|
|
*/ |
24
|
|
|
protected $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* MonkeyLearn API token. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $token; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Map group name to class names. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $map = [ |
39
|
|
|
'classification' => 'Classification', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Assign dependencies. |
44
|
|
|
* |
45
|
|
|
* @param string $token |
46
|
|
|
* @param HttpClientInterface $httpClient |
47
|
|
|
*/ |
48
|
26 |
|
public function __construct($token, HttpClientInterface $httpClient = null) |
49
|
|
|
{ |
50
|
26 |
|
$this->token = $token; |
51
|
|
|
|
52
|
26 |
|
$this->httpClient = $httpClient; |
53
|
26 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the API group to call a method within. |
57
|
|
|
* |
58
|
|
|
* @api |
59
|
|
|
* |
60
|
|
|
* @param string $group |
61
|
|
|
* |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
* |
64
|
|
|
* @return ApiInterface |
65
|
|
|
*/ |
66
|
8 |
|
public function api($group) |
67
|
|
|
{ |
68
|
8 |
|
if (array_key_exists($group, $this->map)) { |
69
|
4 |
|
$apiClass = sprintf('%s\\Api\\%s', __NAMESPACE__, $this->map[$group]); |
70
|
|
|
|
71
|
4 |
|
$api = new $apiClass($this); |
72
|
4 |
|
} else { |
73
|
4 |
|
throw new InvalidArgumentException( |
74
|
4 |
|
sprintf('Undefined API group called: "%s"', $group) |
75
|
4 |
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return $api; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the client to use for HTTP communication. |
83
|
|
|
* |
84
|
|
|
* @internal |
85
|
|
|
* |
86
|
|
|
* @return HttpClientInterface |
87
|
|
|
*/ |
88
|
14 |
|
public function getHttpClient() |
89
|
|
|
{ |
90
|
14 |
|
if (!$this->httpClient) { |
91
|
2 |
|
$this->httpClient = new HttpClient([ |
92
|
2 |
|
'base_uri' => self::BASE_URI, |
93
|
|
|
'headers' => [ |
94
|
2 |
|
'Authorization' => 'Token '.$this->token, |
95
|
|
|
'content-type' => 'application/json' |
96
|
2 |
|
] |
97
|
2 |
|
]); |
98
|
2 |
|
} |
99
|
|
|
|
100
|
14 |
|
return $this->httpClient; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Enables debug data in responses. |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
2 |
|
public function debug() |
109
|
|
|
{ |
110
|
2 |
|
$this->getHttpClient()->addConfigOption('debug'); |
111
|
|
|
|
112
|
2 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Enables sandbox mode for custom modules. |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function sandbox() |
121
|
|
|
{ |
122
|
2 |
|
$this->getHttpClient()->addConfigOption('sandbox'); |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Magic method to call API groups directly via property. |
129
|
|
|
* |
130
|
|
|
* @param string $group |
131
|
|
|
* |
132
|
|
|
* @return ApiInterface |
133
|
|
|
*/ |
134
|
4 |
|
public function __get($group) |
135
|
|
|
{ |
136
|
4 |
|
return $this->getApiObject($group); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get Api Group object. |
141
|
|
|
* |
142
|
|
|
* @param string $group |
143
|
|
|
* |
144
|
|
|
* @throws BadMethodCallException |
145
|
|
|
* |
146
|
|
|
* @return ApiInterface |
147
|
|
|
*/ |
148
|
4 |
|
protected function getApiObject($group) |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
4 |
|
return $this->api($group); |
152
|
2 |
|
} catch (InvalidArgumentException $e) { |
153
|
2 |
|
throw new BadMethodCallException( |
154
|
2 |
|
sprintf('Undefined method called: "%s"', $group) |
155
|
2 |
|
); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|