|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the light/apistore. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) lichunqiang <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace light\apistore; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* 调用总线 |
|
16
|
|
|
* ~~~ |
|
17
|
|
|
* $store = new ApiStore(); |
|
18
|
|
|
* $result = $store->phone->get('123123123'); |
|
19
|
|
|
* ~~~. |
|
20
|
|
|
*/ |
|
21
|
|
|
final class ApiStore |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The app key of calling the interface. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $apikey; |
|
29
|
|
|
|
|
30
|
|
|
public static $supportedApis = [ |
|
31
|
|
|
'ip' => 'light\apistore\apis\Ip', |
|
32
|
|
|
'phone' => 'light\apistore\apis\Phone', |
|
33
|
|
|
'translate' => 'light\apistore\apis\Translate', |
|
34
|
|
|
'idcard' => 'light\apistore\apis\IdCard', |
|
35
|
|
|
'currency' => 'light\apistore\apis\Currency', |
|
36
|
|
|
'pullword' => 'light\apistore\apis\PullWord', |
|
37
|
|
|
'distance' => 'light\apistore\apis\Distance', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Init. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $apikey 调用接口的API key |
|
44
|
|
|
*/ |
|
45
|
18 |
|
public function __construct($apikey) |
|
46
|
|
|
{ |
|
47
|
18 |
|
$this->apikey = $apikey; |
|
48
|
18 |
|
} |
|
49
|
|
|
/** |
|
50
|
|
|
* Implement the __get magic method. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name The property name or the api name |
|
53
|
|
|
* |
|
54
|
|
|
* @throws InvalidaCallException |
|
55
|
|
|
* @throws UnknownPropertyException |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
18 |
|
public function __get($name) |
|
60
|
|
|
{ |
|
61
|
18 |
|
if (isset(static::$supportedApis[$name])) { |
|
62
|
17 |
|
$class = static::$supportedApis[$name]; |
|
63
|
|
|
|
|
64
|
17 |
|
return new $class($this->apikey); |
|
65
|
|
|
} |
|
66
|
1 |
|
throw new \Exception('Not exists method'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Invoke methods. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name the name of method try to invoke |
|
73
|
|
|
* @param array $params the params passed to the method |
|
74
|
|
|
* |
|
75
|
|
|
* @throws InvalidCallException |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed the method result of calling |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function __call($name, $params) |
|
80
|
|
|
{ |
|
81
|
1 |
|
$obj = $this->{$name}; |
|
82
|
|
|
|
|
83
|
1 |
|
return call_user_func_array([$obj, 'get'], $params); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|