Skip to content

Commit 0d9b3ea

Browse files
committed
added doc blocks, edited documentation
1 parent 1ae629f commit 0d9b3ea

File tree

3 files changed

+102
-22
lines changed

3 files changed

+102
-22
lines changed

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel-Usps
22

3-
This package provides a very simple wrapper for the United States Postal Service API. Currently, this package only provides address verification features, but will soon comprise all features offered by the USPS API. In the meantime, consider using [johnpaulmedina/laravel-usps](https://github.com/johnpaulmedina/laravel-usps), which is a great package.
3+
This package provides a very simple wrapper for the United States Postal Service API. Currently, this package only provides address validation features, but will soon comprise all features offered by the USPS API. In the meantime, consider using [johnpaulmedina/laravel-usps](https://github.com/johnpaulmedina/laravel-usps), which is a great package.
44

55
### Prerequisites
66

@@ -42,7 +42,49 @@ In `config/services.php` add these three settings.
4242
## Usage
4343

4444
The current features offered by this package are listed below.
45-
- [Address Verification](#Address)
45+
- [Address Validation](#Address)
46+
47+
48+
## Address Validation
49+
50+
This `Address` class handles creating and formatting address data. Pass the constructor an associative array of address details. Array keys are case-sensitive.
51+
Below is an example of creating an address and making an api request for validation.
52+
53+
```php
54+
use ctwillie\Usps\Address;
55+
56+
$address = new Address([
57+
'Address2' => '6406 Ivy Lane',
58+
'City' => 'Greenbelt',
59+
'State' => 'MD',
60+
'Zip5' => 20770
61+
]);
62+
63+
$response = $address->validate();
64+
```
65+
The USPS api limits 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.
66+
67+
```php
68+
use ctwillie\Usps\Address;
69+
70+
$address1 = new Address([
71+
'Address2' => '6406 Ivy Lane',
72+
'City' => 'Greenbelt',
73+
'State' => 'MD',
74+
'Zip5' => 20770
75+
]);
76+
77+
$address2 = new Address([
78+
'Address2' => '6406 Ivy Lane',
79+
'City' => 'Greenbelt',
80+
'State' => 'MD',
81+
'Zip5' => 20770
82+
]);
83+
84+
$addresses = [$address1, $address2];
85+
86+
$response = $addresses->validate();
87+
```
4688

4789
## Contributing
4890

src/Usps/API.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,75 @@
77
use Spatie\ArrayToXml\ArrayToXml;
88
use GuzzleHttp\Client;
99

10+
/**
11+
* Class used by each entity to interact with the API and get configurations
12+
*
13+
* Each entity will be responsible for managing its own data used in the api
14+
* request. This class handles the common functions, such as, data formatting,
15+
* creating http client, making request, returning response etc.
16+
*
17+
*/
1018
abstract class API {
1119

1220
const PROD_URL = 'https://secure.shippingapis.com/ShippingAPI.dll';
1321
const LOCAL_URL = 'http://production.shippingapis.com/ShippingAPITest.dll';
1422
protected $env;
1523
protected $apiUrl;
1624
protected $userId;
25+
protected $rootElements = [
26+
'Verify' => 'AddressValidateRequest'
27+
];
1728

29+
/** Determines env to set api url, http client settings etc. */
1830
public function __construct() {
1931
$this->setEnvironment();
2032
$this->setUrl();
2133
$this->userId = config('services.usps.userid');
2234
}
2335

24-
// Determines the current application environment
36+
/** Determines the current application env */
2537
public function setEnvironment()
2638
{
2739
if (Config::has('services.usps.env')) {
40+
2841
$this->env = strtolower(config('services.usps.env'));
42+
2943
} elseif (App::environment(['local', 'development'])) {
44+
3045
$this->env = 'local';
31-
}
46+
}
47+
3248
$this->env = 'production';
3349
}
3450

51+
/** Determines the api url to use based on running env */
3552
public function setUrl()
3653
{
3754
$this->apiUrl = $this->env === 'local' ? self::LOCAL_URL : self::PROD_URL;
3855
}
3956

57+
/**
58+
* Makes sure a userid has been provided
59+
*
60+
* @throws Exception If no userid is found in config/services.php
61+
*/
4062
public function checkForUserId()
4163
{
4264
if (is_null($this->userId)) {
4365
throw new \Exception('USPS: A user ID is required. None found in config/services.php');
4466
}
4567
}
4668

47-
// create a map to determine the rootElementName foreach api type
69+
/**
70+
* Converts entity data into xml format and url encodes
71+
*
72+
* @return string
73+
*/
4874
protected function convertToXML()
4975
{
5076
$xml = ArrayToXml::convert( $this->getRequestData(),
5177
[
52-
'rootElementName' => $this->rootElementName,
78+
'rootElementName' => $this->rootElements[$this->apiType],
5379

5480
'_attributes' => ['USERID' => $this->userId]
5581

@@ -58,6 +84,14 @@ protected function convertToXML()
5884
return urlencode($xml);
5985
}
6086

87+
/**
88+
* Handles making the api request
89+
*
90+
* This method handles creating the http client and gathering
91+
* all necessary data for making the api request.
92+
*
93+
* @return StreamInterface of the response content body
94+
*/
6195
public function makeRequest()
6296
{
6397
$this->checkForUserId();
@@ -71,8 +105,12 @@ public function makeRequest()
71105
return $response->getBody();
72106
}
73107

74-
// Abstract to be defined by each entity
75-
abstract public function getRequestData() : array;
108+
/**
109+
* Each entity decides the array structure for the request data
110+
*
111+
* @return array
112+
*/
113+
abstract public function getRequestData();
76114

77115
public function validate()
78116
{

src/Usps/Address.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace ctwillie\Usps;
44

5+
/**
6+
* This class gathers data for the 'Verify' api route
7+
*
8+
* It is responsible for formatting address request data that
9+
* will be handled by the API class
10+
*/
511
class Address extends API
612
{
7-
// use map to determine root element
8-
// key will be api type ('verify' => 'AddressValidateRequest')
9-
protected $rootElementName = 'AddressValidateRequest';
1013
protected $apiType = 'Verify';
1114
protected $addresses = [];
1215
protected $allowedProperties = ['Address1','Address2','City','State','Zip4','Zip5'];
@@ -16,8 +19,6 @@ class Address extends API
1619
* Handles single or multiple addresses passes to constructor
1720
*
1821
* @param array $addresses
19-
* @return void
20-
*
2122
*/
2223
public function __construct(array $addresses = [])
2324
{
@@ -26,11 +27,9 @@ public function __construct(array $addresses = [])
2627
}
2728

2829
/**
29-
* Handles multiple addresses
30+
* Handles adding multiple addresses
3031
*
3132
* @param array $addresses
32-
* @return void
33-
*
3433
*/
3534
public function handleMultipleAddresses(array $addresses)
3635
{
@@ -55,13 +54,9 @@ public function handleMultipleAddresses(array $addresses)
5554
}
5655

5756
/**
58-
* Handles single address
57+
* Handles adding a single address
5958
*
6059
* @param array $address
61-
* @return void
62-
*
63-
* Add addAddress method which calls this method and checks five address limit
64-
*
6560
*/
6661
public function handleSingleAddress(array $address)
6762
{
@@ -80,7 +75,12 @@ public function handleSingleAddress(array $address)
8075
}
8176
}
8277

83-
public function getRequestData() : array
78+
/**
79+
* Formats the request data.
80+
*
81+
* @return array
82+
*/
83+
public function getRequestData()
8484
{
8585
return ['Address' => $this->addresses];
8686
}

0 commit comments

Comments
 (0)