@@ -34,7 +34,7 @@ public function __construct() {
3434 }
3535
3636 /** Determines the current application env */
37- public function setEnvironment ()
37+ protected function setEnvironment ()
3838 {
3939 if (Config::has ('services.usps.env ' )) {
4040
@@ -49,7 +49,7 @@ public function setEnvironment()
4949 }
5050
5151 /** Determines the api url to use based on running env */
52- public function setUrl ()
52+ protected function setUrl ()
5353 {
5454 $ this ->apiUrl = $ this ->env === 'local ' ? self ::LOCAL_URL : self ::PROD_URL ;
5555 }
@@ -59,62 +59,103 @@ public function setUrl()
5959 *
6060 * @throws Exception If no userid is found in config/services.php
6161 */
62- public function checkForUserId ()
62+ protected function checkForUserId ()
6363 {
6464 if (is_null ($ this ->userId )) {
65+
6566 throw new \Exception ('USPS: A user ID is required. None found in config/services.php ' );
67+
6668 }
6769 }
6870
6971 /**
70- * Converts entity data into xml format and url encodes
72+ * Converts entity data into xml format
7173 *
7274 * @return string
7375 */
7476 protected function convertToXML ()
7577 {
76- $ xml = ArrayToXml::convert ( $ this ->getRequestData (),
78+ return ArrayToXml::convert ( $ this ->getRequestData (),
7779 [
7880 'rootElementName ' => $ this ->rootElements [$ this ->apiType ],
7981
8082 '_attributes ' => ['USERID ' => $ this ->userId ]
8183
8284 ], false
8385 );
84- return urlencode ($ xml );
8586 }
8687
8788 /**
88- * Handles making the api request
89+ * Handles making the api request and formatting response type
8990 *
9091 * This method handles creating the http client and gathering
9192 * all necessary data for making the api request.
9293 *
93- * @return StreamInterface of the response content body
94+ * @param string|null $responseType
95+ * @return mixed depending on $responseType param
9496 */
95- public function makeRequest ()
97+ protected function makeRequest (string $ responseType = null )
9698 {
9799 $ this ->checkForUserId ();
98100
99- $ xml = $ this ->convertToXML ();
101+ $ xml = urlencode ( $ this ->convertToXML () );
100102
101103 $ client = new Client (['base_uri ' => $ this ->apiUrl , 'verify ' => config ('services.usps.verifyssl ' , true )]);
102104
103105 $ response = $ client ->request ('GET ' , "?API= $ this ->apiType &XML= $ xml " );
104106
105- return $ response ->getBody ();
107+ // converts stream to xml string
108+ $ body = (string ) $ response ->getBody ();
109+
110+ return $ this ->convertResponse ($ body , $ responseType );
111+ }
112+
113+ /**
114+ * Responsible for converting api responses to desired format
115+ *
116+ * The api response will return an assoc array by default
117+ *
118+ * @param string $body xml string from api response
119+ * @param string $responseType desired response format ('json', 'object', 'string')
120+ * @return mixed
121+ */
122+ protected function convertResponse ($ body , $ responseType )
123+ {
124+ switch (strtolower ($ responseType )) {
125+ case 'string ' :
126+ return $ body ;
127+ break ;
128+ case 'json ' :
129+ $ xml = simplexml_load_string ( $ body );
130+ return json_encode ($ xml );
131+ break ;
132+ case 'object ' :
133+ $ xml = simplexml_load_string ($ body );
134+ return json_decode (json_encode ($ xml ));
135+ break ;
136+ default :
137+ $ xml = simplexml_load_string ($ body );
138+ return json_decode (json_encode ($ xml ), true );
139+ break ;
140+ }
106141 }
107142
108143 /**
109144 * Each entity decides the array structure for the request data
110145 *
111146 * @return array
112147 */
113- abstract public function getRequestData ();
148+ abstract protected function getRequestData ();
114149
115- public function validate ()
150+ /**
151+ * Alias for makeRequest
152+ *
153+ * @param string $responseType The desired format of the response
154+ * @return mixed depending on $responseType param
155+ */
156+ public function validate (string $ responseType = null )
116157 {
117- return $ this ->makeRequest ();
158+ return $ this ->makeRequest ($ responseType );
118159 }
119160
120161}
0 commit comments