2727 *
2828 * @author Andy Pieters <andy@andypieters.nl>
2929 */
30- class Api
31- {
32- /**
33- * @var int the version of the api
34- */
35- protected $ version = 1 ;
36-
37- /**
38- * @var array
39- */
40- protected $ data = array ();
41-
42- /**
43- * @var bool Is the ApiToken required for this API
44- */
45- protected $ apiTokenRequired = false ;
46- /**
47- * @var bool Is the serviceId required for this API
48- */
49- protected $ serviceIdRequired = false ;
50-
51- /**
52- * @return bool
53- */
54- public function isApiTokenRequired ()
55- {
56- return $ this ->apiTokenRequired ;
57- }
58-
59- /**
60- * @return bool
61- */
62- public function isServiceIdRequired ()
63- {
64- return $ this ->serviceIdRequired ;
65- }
66-
67- /**
68- * @return array
69- * @throws Error\Required\ApiToken
70- * @throws Error\Required\ServiceId
71- */
72- protected function getData ()
73- {
74- if ($ this ->isApiTokenRequired ()) {
75- Helper::requireApiToken ();
76- $ this ->data ['token ' ] = Config::getApiToken ();
77- }
78- if ($ this ->isServiceIdRequired ()){
79- Helper::requireServiceId ();
80- $ this ->data ['serviceId ' ] = Config::getServiceId ();
81- }
82- return $ this ->data ;
83- }
84-
85- /**
86- * @param object|array $result
87- * @return array
88- * @throws Error\Api
89- */
90- protected function processResult ($ result )
91- {
92- $ output = Helper::objectToArray ($ result );
93-
94- if (!is_array ($ output )){
95- throw new Error \Api ($ output );
96- }
97-
98- if ($ output ['request ' ]['result ' ] != 1 && $ output ['request ' ]['result ' ] !== 'TRUE ' ) {
99- throw new Error \Api ($ output ['request ' ]['errorId ' ] . ' - ' . $ output ['request ' ]['errorMessage ' ]);
100- }
101- return $ output ;
102- }
103-
104- /**
105- * @param $endpoint
106- * @param null|int $version
107- * @return array
108- *
109- * @throws Error\Api
110- * @throws Error\Error
111- */
112- public function doRequest ($ endpoint , $ version = null )
113- {
114- if ($ version === null ){
115- $ version = $ this ->version ;
116- }
117-
118- $ data = $ this ->getData ();
119-
120- $ uri = Config::getApiUrl ($ endpoint , (int ) $ version );
121-
122- $ curl = Config::getCurl ();
123-
124- if (Config::getCAInfoLocation ()){
125- // set a custom CAInfo file
126- $ curl ->setOpt (CURLOPT_CAINFO , Config::getCAInfoLocation ());
127- }
128-
129- $ curl ->setOpt (CURLOPT_SSL_VERIFYPEER , Config::getVerifyPeer ());
130-
131- $ result = $ curl ->post ($ uri , $ data );
132-
133- if ($ curl ->error ){
134- if (!empty ($ result )) {
135- if ($ result ->status === 'FALSE ' ) {
136- throw new Error \Api ($ result ->error );
137- }
138- }
139- throw new Error \Error ($ curl ->errorMessage );
140- }
141-
142- return $ this ->processResult ($ result );
143- }
30+ class Api {
31+ /**
32+ * @var int the version of the api
33+ */
34+ protected $ version = 1 ;
35+
36+ /**
37+ * @var array
38+ */
39+ protected $ data = array ();
40+
41+ /**
42+ * @var bool Is the ApiToken required for this API
43+ */
44+ protected $ apiTokenRequired = false ;
45+ /**
46+ * @var bool Is the serviceId required for this API
47+ */
48+ protected $ serviceIdRequired = false ;
49+
50+ /**
51+ * @param $endpoint
52+ * @param null|int $version
53+ *
54+ * @return array
55+ *
56+ * @throws Error\Api
57+ * @throws Error\Error
58+ */
59+ public function doRequest ( $ endpoint , $ version = null ) {
60+ if ( $ version === null ) {
61+ $ version = $ this ->version ;
62+ }
63+
64+ $ data = $ this ->getData ();
65+
66+ $ uri = Config::getApiUrl ( $ endpoint , (int ) $ version );
67+
68+ $ curl = Config::getCurl ();
69+
70+ if ( Config::getCAInfoLocation () ) {
71+ // set a custom CAInfo file
72+ $ curl ->setOpt ( CURLOPT_CAINFO , Config::getCAInfoLocation () );
73+ }
74+
75+ $ curl ->setOpt ( CURLOPT_SSL_VERIFYPEER , Config::getVerifyPeer () );
76+
77+ $ result = $ curl ->post ( $ uri , $ data );
78+
79+ if ( isset ( $ result ->status ) && $ result ->status === 'FALSE ' ) {
80+ throw new Error \Api ( $ result ->error );
81+ }
82+
83+ if ( $ curl ->error ) {
84+ throw new Error \Error ( $ curl ->errorMessage );
85+ }
86+
87+ return $ this ->processResult ( $ result );
88+ }
89+
90+ /**
91+ * @return array
92+ * @throws Error\Required\ApiToken
93+ * @throws Error\Required\ServiceId
94+ */
95+ protected function getData () {
96+ if ( $ this ->isApiTokenRequired () ) {
97+ Helper::requireApiToken ();
98+ $ this ->data ['token ' ] = Config::getApiToken ();
99+ }
100+ if ( $ this ->isServiceIdRequired () ) {
101+ Helper::requireServiceId ();
102+ $ this ->data ['serviceId ' ] = Config::getServiceId ();
103+ }
104+
105+ return $ this ->data ;
106+ }
107+
108+ /**
109+ * @return bool
110+ */
111+ public function isApiTokenRequired () {
112+ return $ this ->apiTokenRequired ;
113+ }
114+
115+ /**
116+ * @return bool
117+ */
118+ public function isServiceIdRequired () {
119+ return $ this ->serviceIdRequired ;
120+ }
121+
122+ /**
123+ * @param object|array $result
124+ *
125+ * @return array
126+ * @throws Error\Api
127+ */
128+ protected function processResult ( $ result ) {
129+ $ output = Helper::objectToArray ( $ result );
130+
131+ if ( ! is_array ( $ output ) ) {
132+ throw new Error \Api ( $ output );
133+ }
134+
135+ if ( isset ( $ output ['result ' ] ) ) {
136+ return $ output ;
137+ }
138+
139+ if (
140+ isset ( $ output ['request ' ] ) &&
141+ $ output ['request ' ]['result ' ] != 1 &&
142+ $ output ['request ' ]['result ' ] !== 'TRUE ' ) {
143+ throw new Error \Api ( $ output ['request ' ]['errorId ' ] . ' - ' . $ output ['request ' ]['errorMessage ' ] );
144+ }
145+
146+ return $ output ;
147+ }
144148}
0 commit comments