Merge pull request #9 from perk11/validation

Validation
pull/11/head
Jamie Isaacs 10 years ago
commit 51a8b8e331

@ -7,6 +7,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -15,19 +16,18 @@ class Rate extends RateAdapter
private $urlDev = 'https://gatewaybeta.fedex.com/web-services/'; private $urlDev = 'https://gatewaybeta.fedex.com/web-services/';
private $urlProd = 'https://gateway.fedex.com/web-services/'; private $urlProd = 'https://gateway.fedex.com/web-services/';
private $key = 'XXX'; private $key;
private $password = 'XXX'; private $password;
private $accountNumber = 'XXX'; private $accountNumber;
private $meterNumber = 'XXX'; private $meterNumber;
private $dropOffType = 'BUSINESS_SERVICE_CENTER'; /**
* Type of Drop off, default value "BUSINESS_SERVICE_CENTER" is defined in __construct if not specified.
public $approvedCodes = [ */
'PRIORITY_OVERNIGHT', private $dropOffType;
'FEDEX_2_DAY', /**
'FEDEX_EXPRESS_SAVER', * Codes of appropriate shipping types. Default value is specified in __construct.
'FEDEX_GROUND', */
'GROUND_HOME_DELIVERY', public $approvedCodes;
];
private $shippingCodes = [ private $shippingCodes = [
'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First International Priority', 'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First International Priority',
@ -61,12 +61,37 @@ class Rate extends RateAdapter
$this->password = Arr::get($options, 'password'); $this->password = Arr::get($options, 'password');
$this->accountNumber = Arr::get($options, 'accountNumber'); $this->accountNumber = Arr::get($options, 'accountNumber');
$this->meterNumber = Arr::get($options, 'meterNumber'); $this->meterNumber = Arr::get($options, 'meterNumber');
$this->approvedCodes = Arr::get($options, 'approvedCodes'); $this->approvedCodes = Arr::get($options, 'approvedCodes', [
$this->dropOffType = Arr::get($options, 'dropOffType'); 'PRIORITY_OVERNIGHT',
'FEDEX_2_DAY',
'FEDEX_EXPRESS_SAVER',
'FEDEX_GROUND',
'GROUND_HOME_DELIVERY',
]);
$this->dropOffType = Arr::get($options, 'dropOffType', 'BUSINESS_SERVICE_CENTER');
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->key, 'key');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->accountNumber, 'accountNumber');
Validator::checkIfNull($this->meterNumber, 'meterNumber');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode');
Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode');
Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential');
return $this;
}
protected function prepare() protected function prepare()
{ {
$date = time(); $date = time();
@ -103,7 +128,6 @@ class Rate extends RateAdapter
'</Dimensions>' . '</Dimensions>' .
'</RequestedPackageLineItems>'; '</RequestedPackageLineItems>';
} }
$this->data = '<?xml version="1.0"?>' . $this->data = '<?xml version="1.0"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' .
'xmlns="http://fedex.com/ws/rate/v13">' . 'xmlns="http://fedex.com/ws/rate/v13">' .

@ -16,6 +16,10 @@ abstract class RateAdapter
/** @var @var RateRequest\Adapter */ /** @var @var RateRequest\Adapter */
protected $rateRequest; protected $rateRequest;
/**
* Make sure all necessary fields are set
*/
abstract protected function validate();
/** /**
* Prepare XML * Prepare XML
*/ */
@ -47,6 +51,7 @@ abstract class RateAdapter
public function getRates() public function getRates()
{ {
$this $this
->validate()
->prepare() ->prepare()
->execute() ->execute()
->process() ->process()

@ -58,7 +58,6 @@ class Ship
// Build approvedCodes // Build approvedCodes
foreach ($this->shipping_options as $shipping_group => $row) { foreach ($this->shipping_options as $shipping_group => $row) {
foreach ($row as $_carrier => $row2) { foreach ($row as $_carrier => $row2) {
if (!isset($approvedCodes[$_carrier])) { if (!isset($approvedCodes[$_carrier])) {
$approvedCodes[$_carrier] = []; $approvedCodes[$_carrier] = [];
@ -89,9 +88,7 @@ class Ship
$group_codes = array_keys($row2); $group_codes = array_keys($row2);
if (! empty($rates[$carrier])) { if (! empty($rates[$carrier])) {
foreach ($rates[$carrier] as $row3) { foreach ($rates[$carrier] as $row3) {
if (in_array($row3->getCode(), $group_codes)) { if (in_array($row3->getCode(), $group_codes)) {
$row3->setCarrier($carrier); $row3->setCarrier($carrier);
@ -127,9 +124,7 @@ class Ship
$group_codes = array_keys($row2); $group_codes = array_keys($row2);
if (!empty($rates[$carrier])) { if (!empty($rates[$carrier])) {
foreach ($rates[$carrier] as $row3) { foreach ($rates[$carrier] as $row3) {
if (in_array($row3->getCode(), $group_codes)) { if (in_array($row3->getCode(), $group_codes)) {
$row3->setCarrier($carrier); $row3->setCarrier($carrier);
$display_rates[$shipping_group][] = $row3; $display_rates[$shipping_group][] = $row3;

@ -1,11 +1,11 @@
<?php <?php
namespace pdt256\Shipping\UPS; namespace pdt256\Shipping\UPS;
use pdt256\Ship;
use pdt256\Shipping\Arr; use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -14,15 +14,14 @@ class Rate extends RateAdapter
private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate'; private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate';
private $urlProd = 'https://www.ups.com/ups.app/xml/Rate'; private $urlProd = 'https://www.ups.com/ups.app/xml/Rate';
private $accessKey = 'XXX'; private $accessKey;
private $userId = 'XXX'; private $userId;
private $password = 'XXX'; private $password;
private $shipperNumber = 'XXX'; private $shipperNumber;
/**
public $approvedCodes = [ * Codes of appropriate shipping types. Default value is specified in __construct.
'03', */
'12', public $approvedCodes;
];
private $shippingCodes = [ private $shippingCodes = [
'US' => [ // United States 'US' => [ // United States
@ -90,12 +89,34 @@ class Rate extends RateAdapter
$this->userId = Arr::get($options, 'userId'); $this->userId = Arr::get($options, 'userId');
$this->password = Arr::get($options, 'password'); $this->password = Arr::get($options, 'password');
$this->shipperNumber = Arr::get($options, 'shipperNumber'); $this->shipperNumber = Arr::get($options, 'shipperNumber');
$this->approvedCodes = Arr::get($options, 'approvedCodes'); $this->approvedCodes = Arr::get($options, 'approvedCodes', [
'03',
'12',
]);
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->accessKey, 'accessKey');
Validator::checkIfNull($this->userId, 'userId');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->shipperNumber, 'shipperNumber');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode');
Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode');
Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential');
return $this;
}
protected function prepare() protected function prepare()
{ {
$service_code = '03'; $service_code = '03';

@ -6,6 +6,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -14,13 +15,12 @@ class Rate extends RateAdapter
private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll'; private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll';
private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll'; private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll';
private $username = 'XXX'; private $username;
private $password = 'XXX'; private $password;
/**
public $approvedCodes = [ * Codes of appropriate shipping types. Default value is specified in __construct.
'1', */
'4', public $approvedCodes;
];
private $shipping_codes = [ private $shipping_codes = [
'domestic' => [ 'domestic' => [
@ -73,10 +73,26 @@ class Rate extends RateAdapter
$this->username = Arr::get($options, 'username'); $this->username = Arr::get($options, 'username');
$this->password = Arr::get($options, 'password'); $this->password = Arr::get($options, 'password');
$this->approvedCodes = Arr::get($options, 'approvedCodes'); $this->approvedCodes = Arr::get($options, 'approvedCodes', [
'1',
'4',
]);
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->username, 'username');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
return $this;
}
protected function prepare() protected function prepare()
{ {
$packages = ''; $packages = '';

@ -0,0 +1,12 @@
<?php
namespace pdt256\Shipping;
class Validator
{
public static function checkIfNull($value, $name)
{
if ($value === null) {
throw new \LogicException("$name is not set");
}
}
}

@ -49,6 +49,7 @@ class RateTest extends \PHPUnit_Framework_TestCase
$this->shipment->setFromStateProvinceCode('CA') $this->shipment->setFromStateProvinceCode('CA')
->setFromPostalCode('90401') ->setFromPostalCode('90401')
->setFromCountryCode('US') ->setFromCountryCode('US')
->setFromIsResidential(true)
->setToPostalCode('78703') ->setToPostalCode('78703')
->setToCountryCode('US') ->setToCountryCode('US')
->setToIsResidential(true) ->setToIsResidential(true)
@ -59,12 +60,15 @@ class RateTest extends \PHPUnit_Framework_TestCase
{ {
$rateAdapter = new Rate([ $rateAdapter = new Rate([
'prod' => false, 'prod' => false,
'drop_off_type' => 'BUSINESS_SERVICE_CENTER', 'key' => 'XXX',
'password' => 'XXX',
'accountNumber' => 'XXX',
'meterNumber' => 'XXX',
'dropOffType' => 'BUSINESS_SERVICE_CENTER',
'shipment' => $this->shipment, 'shipment' => $this->shipment,
'approvedCodes' => $this->approvedCodes, 'approvedCodes' => $this->approvedCodes,
'requestAdapter' => new StubFedex, 'requestAdapter' => new StubFedex,
]); ]);
$rates = $rateAdapter->getRates(); $rates = $rateAdapter->getRates();
$ground = new Quote('fedex', 'GROUND_HOME_DELIVERY', 'Ground Home Delivery', 1655); $ground = new Quote('fedex', 'GROUND_HOME_DELIVERY', 'Ground Home Delivery', 1655);

@ -49,6 +49,7 @@ class ShipTest extends \PHPUnit_Framework_TestCase
$s->setFromStateProvinceCode('CA') $s->setFromStateProvinceCode('CA')
->setFromPostalCode('90401') ->setFromPostalCode('90401')
->setFromCountryCode('US') ->setFromCountryCode('US')
->setFromIsResidential(true)
->setToPostalCode('78703') ->setToPostalCode('78703')
->setToCountryCode('US') ->setToCountryCode('US')
->setToIsResidential(true); ->setToIsResidential(true);
@ -105,9 +106,9 @@ class ShipTest extends \PHPUnit_Framework_TestCase
'prod' => false, 'prod' => false,
'key' => 'XXXX', 'key' => 'XXXX',
'password' => 'XXXX', 'password' => 'XXXX',
'account_number' => 'XXXX', 'accountNumber' => 'XXXX',
'meter_number' => 'XXXX', 'meterNumber' => 'XXXX',
'drop_off_type' => 'BUSINESS_SERVICE_CENTER', 'dropOffType' => 'BUSINESS_SERVICE_CENTER',
'shipment' => $this->shipment, 'shipment' => $this->shipment,
'approvedCodes' => $approvedCodes, 'approvedCodes' => $approvedCodes,
'requestAdapter' => new RateRequest\StubFedex(), 'requestAdapter' => new RateRequest\StubFedex(),

@ -48,6 +48,7 @@ class RateTest extends \PHPUnit_Framework_TestCase
$this->shipment->setFromStateProvinceCode('CA') $this->shipment->setFromStateProvinceCode('CA')
->setFromPostalCode('90401') ->setFromPostalCode('90401')
->setFromCountryCode('US') ->setFromCountryCode('US')
->setFromIsResidential(true)
->setToPostalCode('78703') ->setToPostalCode('78703')
->setToCountryCode('US') ->setToCountryCode('US')
->setToIsResidential(true) ->setToIsResidential(true)
@ -57,6 +58,10 @@ class RateTest extends \PHPUnit_Framework_TestCase
public function testMockRates() public function testMockRates()
{ {
$rateAdapter = new Rate([ $rateAdapter = new Rate([
'accessKey' => 'XXX',
'userId' => 'XXX',
'password' => 'XXX',
'shipperNumber' => 'XXX',
'prod' => false, 'prod' => false,
'shipment' => $this->shipment, 'shipment' => $this->shipment,
'approvedCodes' => $this->approvedCodes, 'approvedCodes' => $this->approvedCodes,

Loading…
Cancel
Save