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\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -15,19 +16,18 @@ class Rate extends RateAdapter
private $urlDev = 'https://gatewaybeta.fedex.com/web-services/';
private $urlProd = 'https://gateway.fedex.com/web-services/';
private $key = 'XXX';
private $password = 'XXX';
private $accountNumber = 'XXX';
private $meterNumber = 'XXX';
private $dropOffType = 'BUSINESS_SERVICE_CENTER';
public $approvedCodes = [
'PRIORITY_OVERNIGHT',
'FEDEX_2_DAY',
'FEDEX_EXPRESS_SAVER',
'FEDEX_GROUND',
'GROUND_HOME_DELIVERY',
];
private $key;
private $password;
private $accountNumber;
private $meterNumber;
/**
* Type of Drop off, default value "BUSINESS_SERVICE_CENTER" is defined in __construct if not specified.
*/
private $dropOffType;
/**
* Codes of appropriate shipping types. Default value is specified in __construct.
*/
public $approvedCodes;
private $shippingCodes = [
'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First International Priority',
@ -61,12 +61,37 @@ class Rate extends RateAdapter
$this->password = Arr::get($options, 'password');
$this->accountNumber = Arr::get($options, 'accountNumber');
$this->meterNumber = Arr::get($options, 'meterNumber');
$this->approvedCodes = Arr::get($options, 'approvedCodes');
$this->dropOffType = Arr::get($options, 'dropOffType');
$this->approvedCodes = Arr::get($options, 'approvedCodes', [
'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()));
}
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()
{
$date = time();
@ -103,7 +128,6 @@ class Rate extends RateAdapter
'</Dimensions>' .
'</RequestedPackageLineItems>';
}
$this->data = '<?xml version="1.0"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' .
'xmlns="http://fedex.com/ws/rate/v13">' .

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

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

@ -1,11 +1,11 @@
<?php
namespace pdt256\Shipping\UPS;
use pdt256\Ship;
use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -14,15 +14,14 @@ class Rate extends RateAdapter
private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate';
private $urlProd = 'https://www.ups.com/ups.app/xml/Rate';
private $accessKey = 'XXX';
private $userId = 'XXX';
private $password = 'XXX';
private $shipperNumber = 'XXX';
public $approvedCodes = [
'03',
'12',
];
private $accessKey;
private $userId;
private $password;
private $shipperNumber;
/**
* Codes of appropriate shipping types. Default value is specified in __construct.
*/
public $approvedCodes;
private $shippingCodes = [
'US' => [ // United States
@ -90,12 +89,34 @@ class Rate extends RateAdapter
$this->userId = Arr::get($options, 'userId');
$this->password = Arr::get($options, 'password');
$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()));
}
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()
{
$service_code = '03';

@ -6,6 +6,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -14,13 +15,12 @@ class Rate extends RateAdapter
private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll';
private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll';
private $username = 'XXX';
private $password = 'XXX';
public $approvedCodes = [
'1',
'4',
];
private $username;
private $password;
/**
* Codes of appropriate shipping types. Default value is specified in __construct.
*/
public $approvedCodes;
private $shipping_codes = [
'domestic' => [
@ -73,10 +73,26 @@ class Rate extends RateAdapter
$this->username = Arr::get($options, 'username');
$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()));
}
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()
{
$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')
->setFromPostalCode('90401')
->setFromCountryCode('US')
->setFromIsResidential(true)
->setToPostalCode('78703')
->setToCountryCode('US')
->setToIsResidential(true)
@ -59,12 +60,15 @@ class RateTest extends \PHPUnit_Framework_TestCase
{
$rateAdapter = new Rate([
'prod' => false,
'drop_off_type' => 'BUSINESS_SERVICE_CENTER',
'key' => 'XXX',
'password' => 'XXX',
'accountNumber' => 'XXX',
'meterNumber' => 'XXX',
'dropOffType' => 'BUSINESS_SERVICE_CENTER',
'shipment' => $this->shipment,
'approvedCodes' => $this->approvedCodes,
'requestAdapter' => new StubFedex,
]);
$rates = $rateAdapter->getRates();
$ground = new Quote('fedex', 'GROUND_HOME_DELIVERY', 'Ground Home Delivery', 1655);

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

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

Loading…
Cancel
Save