Skip to content

Commit 3593d4b

Browse files
authored
Merge pull request #3 from ctwillie/development
Merged v0.1.1
2 parents 55c3441 + 640ebeb commit 3593d4b

File tree

6 files changed

+60
-31
lines changed

6 files changed

+60
-31
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ composer.lock
44
index.php
55
notes.php
66
todo.txt
7-
/src/Removed
7+
/src/Removed
8+
dev-notes.txt

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ $address = new Address([
6262

6363
$response = $address->validate();
6464
```
65-
The USPS api supports up to 5 address validations per request. If you need to validate more than one address at a time, pass a multi dim array to the `Address` constructor.
65+
The USPS api supports up to 5 address validations per request. If you need to validate more than one address at a time, pass an array of addresses to the `Address` constructor.
6666

6767
```php
6868
use ctwillie\Usps\Address;
@@ -81,7 +81,7 @@ $address2 = [
8181
'Zip5' => 20770
8282
];
8383

84-
$addresses = new Address([$address1, $address2])
84+
$addresses = new Address([$address1, $address2]);
8585

8686
$response = $addresses->validate();
8787
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"autoload": {
2626
"psr-4": {
27-
"ctwillie\\": "src/"
27+
"ctwillie\\Usps\\": "src/"
2828
}
2929
},
3030
"minimum-stability": "dev"

dev-notes.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Usps/API.php renamed to src/API.php

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
File renamed without changes.

0 commit comments

Comments
 (0)