11<?php
2+ /**
3+ * Copyright 2016 Xenofon Spafaridis
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ namespace Phramework \JSONAPI \Client ;
218
3- namespace Phramework \APISDK ;
4- use Phramework \APISDK \Exceptions \Exception ;
19+ use Phramework \JSONAPI \Client \Exceptions \Exception ;
20+ use Phramework \JSONAPI \Client \Response \Collection ;
21+ use Phramework \JSONAPI \Client \Response \Resource ;
522
623/**
724 * Class API
825 * @author Xenofon Spafaridis <[email protected] > 926 * @since 0.0.0
27+ * @todo handle errors
1028 */
11- class API
29+ abstract class API
1230{
1331 /**
1432 * @var object|null
1533 */
1634 protected static $ globalHeaders = null ;
35+ /**
36+ * @var string|null
37+ */
38+ protected static $ globalAPI = null ;
1739
1840 const METHOD_GET = 'GET ' ;
1941 const METHOD_HEAD = 'HEAD ' ;
@@ -30,18 +52,65 @@ class API
3052 * @var string
3153 * @todo
3254 */
33- protected static $ endpoint = ' user ' ;
55+ protected static $ endpoint = null ;
3456
3557 /**
3658 * @var string
3759 * @todo
3860 */
39- protected static $ type = ' user ' ;
61+ protected static $ type = null ;
4062
4163 /**
64+ * Overrides global API
4265 * @var string|null
4366 */
44- protected static $ api = null ;
67+ protected static $ API = null ;
68+
69+ /**
70+ * MAY be overridden
71+ * @return \stdClass
72+ */
73+ protected static function getHeaders ()
74+ {
75+ return new \stdClass ();
76+ }
77+
78+ /**
79+ * Get API url
80+ * @return string
81+ */
82+ protected function prepareAPI ()
83+ {
84+ if (static ::$ API !== null ) {
85+ return static ::$ API ;
86+ }
87+
88+ return static ::getGlobalAPI () ?? '' ;
89+ }
90+
91+ /**
92+ * Prepare headers
93+ * @param \stdClass|null $additional Additional headers
94+ * @return \stdClass
95+ */
96+ protected function prepareHeaders (\stdClass $ additional = null )
97+ {
98+ if ($ additional === null ) {
99+ $ additional = new \stdClass ();
100+ }
101+
102+ $ headers = static ::getGlobalHeaders ();
103+
104+ foreach (static ::getHeaders () as $ key => $ value ) {
105+ $ headers ->{$ key } = $ value ;
106+ }
107+
108+ foreach ($ additional as $ key => $ value ) {
109+ $ headers ->{$ key } = $ value ;
110+ }
111+
112+ return $ headers ;
113+ }
45114
46115 /**
47116 * Get collection of resources
@@ -52,8 +121,8 @@ class API
52121 * @param IncludeRelationship|null $include Include directive
53122 * @param \stdClass|null $additionalHeaders Will override global
54123 * headers
55- * @param \string[] ...$additional
56- * @return string
124+ * @param \string[] ...$additional Additional url
125+ * @return Collection
57126 * @example
58127 * ```php
59128 * $users = Users::get(
@@ -73,6 +142,7 @@ class API
73142 * )
74143 * );
75144 * ```
145+ * @todo provide examples for all arguments
76146 */
77147 public static function get (
78148 Page $ page = null ,
@@ -81,19 +151,24 @@ public static function get(
81151 Fields $ fields = null ,
82152 IncludeRelationship $ include = null ,
83153 \stdClass $ additionalHeaders = null ,
84- string ...$ additional //todo
154+ string ...$ additional
85155 ) {
86- $ API = 'https://translate.nohponex.gr/ ' ;
87- $ url = $ API . self ::$ endpoint ;
156+ $ API = self ::getGlobalAPI ();
88157
89- $ pagePart = ($ page ? $ page ->toURL () : '' );
90- $ filterPart = ($ filter ? $ filter ->toURL (self ::$ type ) : '' );
91- $ sortPart = ($ sort ? $ sort ->toURL () : '' );
92- $ fieldsPart = ($ fields ? $ fields ->toURL () : '' );
93- $ includePart = ($ include ? $ include ->toURL () : '' );
158+ $ url = $ API . static ::$ endpoint ;
159+
160+ //prepare parts
161+
162+ $ pagePart = ($ page ? $ page ->toURL () : '' );
163+ $ filterPart = ($ filter ? $ filter ->toURL (static ::$ type ) : '' );
164+ $ sortPart = ($ sort ? $ sort ->toURL () : '' );
165+ $ fieldsPart = ($ fields ? $ fields ->toURL () : '' );
166+ $ includePart = ($ include ? $ include ->toURL () : '' );
94167
95168 $ questionMark = false ;
96169
170+ //append parts
171+
97172 if (!empty ($ pagePart )) {
98173 $ url = $ url . ($ questionMark ? '& ' : '? ' ) . $ pagePart ;
99174 $ questionMark = true ;
@@ -119,16 +194,50 @@ public static function get(
119194 $ questionMark = true ;
120195 }
121196
197+ //Append additional
122198 $ url = $ url . implode ('' , $ additional );
123199
124- return $ url ;
200+ $ headers = static ::prepareHeaders ($ additionalHeaders );
201+
202+ /*var_dump($url);*/
203+
204+ list (
205+ $ responseStatusCode ,
206+ $ responseHeaders ,
207+ $ responseBody
208+ ) = static ::request (
209+ $ url ,
210+ self ::METHOD_GET ,
211+ $ headers
212+ );
213+ /*
214+ var_dump(
215+ $responseStatusCode,
216+ $responseHeaders,
217+ $responseBody
218+ );*/
219+
220+ $ collection = (new Collection ())->parse (
221+ $ responseBody
222+ );
223+
224+ $ resource = (new Resource ())->parse (
225+ $ responseBody
226+ );
227+
228+ return $ collection ;
229+
230+ //data
231+ //include
232+ //links
233+ //meta
125234 }
126235
127236 public static function getById (
128237 $ id ,
129238 Fields $ fields = null ,
130239 IncludeRelationship $ include = null ,
131- \stdClass $ headers = null ,
240+ \stdClass $ additionalHeaders = null ,
132241 string ...$ additional
133242 ) {
134243
@@ -167,8 +276,7 @@ public static function request(
167276 //$accept = 'application/json',
168277 $ encoding = NULL
169278
170- ){
171-
279+ ) {
172280 //Extract flags
173281 //Is the request binary
174282 $ binary = ($ flags & self ::REQUEST_BINARY ) != 0 ;
@@ -190,22 +298,33 @@ public static function request(
190298 $headers[] = 'Content-Encoding: ' . $encoding;
191299 }*/
192300
301+ $ headersArray = [];
302+
303+ foreach ($ headers as $ key => $ value ) {
304+ $ headersArray [] = "$ key: $ value " ;
305+ }
306+
193307 //Initialize curl
194308 $ handle = curl_init ();
309+
195310 curl_setopt ($ handle , CURLOPT_URL , $ url );
196- curl_setopt ($ handle , CURLOPT_HTTPHEADER , $ headers );
311+ curl_setopt ($ handle , CURLOPT_HTTPHEADER , $ headersArray );
197312 curl_setopt ($ handle , CURLOPT_RETURNTRANSFER , TRUE );
313+ curl_setopt ($ handle , CURLOPT_HEADER , true );
314+
198315 //Set timeout values ( in seconds )
199316 //curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $this->settings[self::SETTING_CURLOPT_CONNECTTIMEOUT]);
200317 //curl_setopt($handle, CURLOPT_TIMEOUT, $this->settings[self::SETTING_CURLOPT_TIMEOUT]);
201318 curl_setopt ($ handle , CURLOPT_NOSIGNAL , 1 );
202319 //Security options
203320 curl_setopt ($ handle , CURLOPT_SSL_VERIFYHOST , 2 );
204321 curl_setopt ($ handle , CURLOPT_SSL_VERIFYPEER , FALSE );
205- //On binary transfers
322+
323+ /*//On binary transfers
206324 if ($binary) {
207325 curl_setopt($handle, CURLOPT_BINARYTRANSFER, TRUE);
208- }
326+ }*/
327+
209328 //Switch on HTTP Request method
210329 switch ($ method ) {
211330 case self ::METHOD_GET : //On METHOD_GET
@@ -236,18 +355,17 @@ public static function request(
236355 //Get response
237356 $ response = curl_exec ($ handle );
238357 //Get response code
239- //Get response code
240358 $ responseStatusCode = curl_getinfo ($ handle , CURLINFO_HTTP_CODE );
241359 $ headerSize = curl_getinfo ($ handle , CURLINFO_HEADER_SIZE );
242360
243361 $ responseHeadersTemp = str_replace ("\r" , '' , substr ($ response , 0 , $ headerSize ));
244- $ responseHeaders = [] ;
362+ $ responseHeaders = new \ stdClass ;
245363
246364 foreach (explode ("\n" , $ responseHeadersTemp ) as $ i => $ line ) {
247365 if ($ i !== 0 && !empty ($ line )) {
248366 if (count ($ parts = explode (': ' , $ line )) === 2 ) {
249367 list ($ key , $ value ) = explode (': ' , $ line );
250- $ responseHeaders[ $ key] = $ value ;
368+ $ responseHeaders->{ $ key} = $ value ;
251369 }
252370 }
253371 }
@@ -259,11 +377,9 @@ public static function request(
259377 return [
260378 $ responseStatusCode ,
261379 $ responseHeaders ,
262- $ responseBody
380+ json_decode ( $ responseBody)
263381 ];
264382
265-
266-
267383 /* if (!$response) {
268384 throw new Exception('Error: ' . curl_error($handle));
269385 }*/
@@ -284,6 +400,24 @@ public static function request(
284400 $accept == 'application/json' ? json_decode($response, true) : $response );*/
285401 }
286402
403+ /**
404+ * @param string|null $API
405+ */
406+ public static function setGlobalAPI (string $ API = null )
407+ {
408+
409+ static ::$ globalAPI = $ API ;
410+ }
411+
412+ /**
413+ * @return string|null
414+ */
415+ public static function getGlobalAPI ()
416+ {
417+
418+ return static ::$ globalAPI ;
419+ }
420+
287421 /**
288422 * @param string $key
289423 * @param string $header
@@ -300,7 +434,7 @@ public static function setGlobalHeader(string $key, string $header)
300434 /**
301435 * @return null
302436 */
303- public static function getGlobalHeader ()
437+ public static function getGlobalHeaders ()
304438 {
305439 if (static ::$ globalHeaders === null ) {
306440 static ::$ globalHeaders = new \stdClass ();
0 commit comments