|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2019 Dynamika Web |
|
4
|
|
|
* @link https://github.com/dynamikaweb/yii2-citta-api |
|
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace dynamikaweb\api; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* @author Rodrigo Dornelles <[email protected]> <[email protected]> |
|
12
|
|
|
* @version 0.1 (28/04/2020) |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class CittaApi |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $url_reference |
|
19
|
|
|
* @param object $instance |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $url_reference; |
|
22
|
|
|
private static $instance; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* __construct |
|
26
|
|
|
* |
|
27
|
|
|
* @see Sigleton Pattern |
|
28
|
|
|
*/ |
|
29
|
|
|
private function __construct () |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* url |
|
36
|
|
|
* |
|
37
|
|
|
* @see Sigleton Pattern |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|void $url_reference |
|
40
|
|
|
* @return object self |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function url($uri = null) |
|
44
|
|
|
{ |
|
45
|
|
|
// change API URI |
|
46
|
|
|
if($uri !== null){ |
|
47
|
|
|
self::$url_reference = $uri; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// create object |
|
51
|
|
|
if(self::$instance === null){ |
|
52
|
|
|
self::$instance = new self; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// unice instance |
|
56
|
|
|
return self::$instance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* run |
|
61
|
|
|
* |
|
62
|
|
|
* call @api |
|
63
|
|
|
* |
|
64
|
|
|
* @param string|array $uri |
|
65
|
|
|
* @return object |
|
66
|
|
|
*/ |
|
67
|
|
|
private static function run($uri) |
|
68
|
|
|
{ |
|
69
|
|
|
$curl = new \Curl\Curl(); |
|
70
|
|
|
$redirects_count = 0; |
|
71
|
|
|
|
|
72
|
|
|
// base URI reference |
|
73
|
|
|
if (!self::$url_reference){ |
|
74
|
|
|
throw new CittaException('URI Reference Error'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// consult URI |
|
78
|
|
|
if (is_array($uri)){ |
|
79
|
|
|
$curl->get(self::$url_reference.\yii\helpers\Url::to($uri)); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
} else if (is_string($uri)){ |
|
|
|
|
|
|
82
|
|
|
$curl->get(self::$url_reference."/{$uri}"); |
|
83
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
throw new CittaException('URI Type Error'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// redirect URI |
|
89
|
|
|
while (300 <= $curl->http_status_code && $curl->http_status_code < 400){ |
|
|
|
|
|
|
90
|
|
|
// probably loop |
|
91
|
|
|
if (++$redirects_count > 10){ |
|
92
|
|
|
throw new CittaException('Too many redirects'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($curl->response_headers as $header) { |
|
|
|
|
|
|
96
|
|
|
// ignore header |
|
97
|
|
|
if (strpos($header, 'Location:') === false){ |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// try new uri |
|
102
|
|
|
$curl->get(strtr($header,[ |
|
103
|
|
|
'Location: ' => '', |
|
104
|
|
|
'location: ' => '', |
|
105
|
|
|
'Location:' => '', |
|
106
|
|
|
'location:' => '' |
|
107
|
|
|
]) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// return request |
|
113
|
|
|
return $curl; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* request |
|
118
|
|
|
* |
|
119
|
|
|
* dataProvider |
|
120
|
|
|
* |
|
121
|
|
|
* @param array|string $uri |
|
122
|
|
|
* @return array|void $dataProviderParams |
|
123
|
|
|
* |
|
124
|
|
|
* @throws CittaException |
|
125
|
|
|
* |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function request($uri, $dataProviderParams = []) |
|
128
|
|
|
{ |
|
129
|
|
|
// cal; api |
|
130
|
|
|
$api = self::run($uri); |
|
131
|
|
|
|
|
132
|
|
|
// resquest error |
|
133
|
|
|
if ($api->error && $api->http_status_code){ |
|
134
|
|
|
throw new CittaException("HTTP Status {$api->http_status_code} Error"); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// curl error |
|
138
|
|
|
if ($api->error){ |
|
139
|
|
|
throw new CittaException($api->error_message); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// return request |
|
143
|
|
|
return new \yii\data\ArrayDataProvider( |
|
|
|
|
|
|
144
|
|
|
\yii\Helpers\ArrayHelper::merge( |
|
|
|
|
|
|
145
|
|
|
['allModels' => \yii\helpers\Json::decode($api->response, true)], |
|
|
|
|
|
|
146
|
|
|
$dataProviderParams |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.