Provide rate quotes back as objects

pull/3/head
Troy Davisson 10 years ago
parent 6d42cd1273
commit 1f98232ac0

@ -9,7 +9,9 @@
"email": "pdt256@gmail.com" "email": "pdt256@gmail.com"
} }
], ],
"require": {}, "require": {
"nesbot/carbon": "1.*"
},
"require-dev": { "require-dev": {
"phpunit/phpunit": "4.0.*" "phpunit/phpunit": "4.0.*"
}, },

@ -1,8 +1,10 @@
<?php <?php
namespace pdt256\Shipping\Fedex; namespace pdt256\Shipping\Fedex;
use Carbon\Carbon;
use pdt256\Shipping; use pdt256\Shipping;
use pdt256\Shipping\Arr; use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use DOMDocument; use DOMDocument;
@ -220,13 +222,18 @@ class Rate extends RateAdapter
->getElementsByTagName('TotalNetCharge')->item(0) ->getElementsByTagName('TotalNetCharge')->item(0)
->getElementsByTagName('Amount')->item(0)->nodeValue; ->getElementsByTagName('Amount')->item(0)->nodeValue;
$this->rates[] = array( $quote = new Quote;
'code' => $code, $quote
'name' => $name, ->setCarrier('fedex')
'cost' => (int) $cost * 100, ->setCode($code)
'delivery_ts' => $delivery_ts, ->setName($name)
'transit_time' => $transit_time, ->setCost((int) $cost * 100)
); ->setTransitTime($transit_time);
if ($delivery_ts) {
$quote->setDeliveryEstimate(new Carbon($delivery_ts));
}
$this->rates[] = $quote;
} }
return $this; return $this;

@ -0,0 +1,123 @@
<?php namespace pdt256\Shipping;
use Carbon\Carbon;
class Quote
{
protected $code;
protected $name;
protected $cost;
protected $transit_time;
protected $delivery_ts;
protected $carrier;
/**
* @return mixed
*/
public function getCarrier()
{
return $this->carrier;
}
/**
* @param mixed $carrier
* @return $this
*/
public function setCarrier($carrier)
{
$this->carrier = $carrier;
return $this;
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
/**
* @param string $code
* @return $this
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getCost()
{
return $this->cost;
}
/**
* Quoted cost of this service, in pennies
*
* @param int $cost
* @return $this
*/
public function setCost($cost)
{
$this->cost = $cost;
return $this;
}
/**
* @return mixed
*/
public function getTransitTime()
{
return $this->transit_time;
}
/**
* @param mixed $transit_time
* @return $this
*/
public function setTransitTime($transit_time)
{
$this->transit_time = $transit_time;
return $this;
}
/**
* @return mixed
*/
public function getDeliveryEstimate()
{
return $this->delivery_ts;
}
/**
* @param Carbon $estimate
* @return $this
*/
public function setDeliveryEstimate(Carbon $estimate)
{
$this->delivery_ts = $estimate;
return $this;
}
}

@ -43,11 +43,11 @@ abstract class RateAdapter
->process() ->process()
->sort_by_cost(); ->sort_by_cost();
return $this->rates; return array_values($this->rates);
} }
protected function sort_by_cost() protected function sort_by_cost()
{ {
uasort($this->rates, create_function('$a, $b', 'return ($a["cost"] > $b["cost"]);')); uasort($this->rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());'));
} }
} }

@ -88,13 +88,13 @@ class Ship
foreach ($rates[$carrier] as $row3) { foreach ($rates[$carrier] as $row3) {
if (in_array($row3['code'], $group_codes)) { if (in_array($row3->getCode(), $group_codes)) {
$row3['carrier'] = $carrier; $row3->setCarrier($carrier);
if ($cheapest_row === NULL) { if ($cheapest_row === NULL) {
$cheapest_row = $row3; $cheapest_row = $row3;
} else { } else {
if ($row3['cost'] < $cheapest_row['cost']) { if ($row3->getCost() < $cheapest_row->getCost()) {
$cheapest_row = $row3; $cheapest_row = $row3;
} }
} }
@ -126,8 +126,8 @@ class Ship
foreach ($rates[$carrier] as $row3) { foreach ($rates[$carrier] as $row3) {
if (in_array($row3['code'], $group_codes)) { if (in_array($row3->getCode(), $group_codes)) {
$row3['carrier'] = $carrier; $row3->setCarrier($carrier);
$display_rates[$shipping_group][] = $row3; $display_rates[$shipping_group][] = $row3;
} }
} }
@ -142,6 +142,6 @@ class Ship
protected function sort_by_cost( & $rates) protected function sort_by_cost( & $rates)
{ {
uasort($rates, create_function('$a, $b', 'return ($a["cost"] > $b["cost"]);')); uasort($rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());'));
} }
} }

@ -3,6 +3,7 @@ namespace pdt256\Shipping\UPS;
use pdt256\Ship; use pdt256\Ship;
use pdt256\Shipping\Arr; use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use DOMDocument; use DOMDocument;
@ -233,11 +234,13 @@ class Rate extends RateAdapter
continue; continue;
} }
$this->rates[] = array( $quote = new Quote;
'code' => $code, $quote
'name' => $name, ->setCarrier('ups')
'cost' => (int) $cost * 100, ->setCode($code)
); ->setName($name)
->setCost((int) $cost * 100);
$this->rates[] = $quote;
} }
return $this; return $this;

@ -3,6 +3,7 @@ namespace pdt256\Shipping\USPS;
use pdt256\Shipping; use pdt256\Shipping;
use pdt256\Shipping\Arr; use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use DOMDocument; use DOMDocument;
@ -178,6 +179,7 @@ class Rate extends RateAdapter
throw $e; throw $e;
} }
/** @var Quote[] $rates */
$rates = []; $rates = [];
foreach ($postage_list as $postage) { foreach ($postage_list as $postage) {
@ -191,16 +193,19 @@ class Rate extends RateAdapter
} }
if (array_key_exists($code, $rates)) { if (array_key_exists($code, $rates)) {
$cost = $rates[$code]['cost'] + ($cost * 100); $cost = $rates[$code]->getCost() + ($cost * 100);
} else { } else {
$cost = $cost * 100; $cost = $cost * 100;
} }
$rates[$code] = [ $quote = new Quote;
'code' => $code, $quote
'name' => $name, ->setCarrier('usps')
'cost' => (int) $cost, ->setCode($code)
]; ->setName($name)
->setCost((int) $cost);
$rates[$quote->getCode()] = $quote;
} }
$this->rates = array_values($rates); $this->rates = array_values($rates);

@ -1,5 +1,7 @@
<?php <?php
use Carbon\Carbon;
use pdt256\Shipping\Package; use pdt256\Shipping\Package;
use pdt256\Shipping\Quote;
use pdt256\Shipping\Ship; use pdt256\Shipping\Ship;
use pdt256\Shipping\Shipment; use pdt256\Shipping\Shipment;
use pdt256\Shipping\USPS; use pdt256\Shipping\USPS;
@ -123,18 +125,23 @@ class ShipTest extends PHPUnit_Framework_TestCase
$usps = new USPS\Rate($this->getUSPSOptions()); $usps = new USPS\Rate($this->getUSPSOptions());
$usps_rates = $usps->get_rates(); $usps_rates = $usps->get_rates();
$this->assertEquals(json_encode([ $post = new Quote;
1 => [ $post
'code' => '4', ->setCarrier('usps')
'name' => 'Parcel Post', ->setCode(4)
'cost' => 1001, ->setName('Parcel Post')
], ->setCost(1001);
0 => [
'code' => '1', $priority = new Quote;
'name' => 'Priority Mail', $priority
'cost' => 1220, ->setCarrier('usps')
], ->setCode(1)
]), json_encode($usps_rates)); ->setName('Priority Mail')
->setCost(1220);
$expected_return = [$post, $priority];
$this->assertEquals($expected_return, $usps_rates);
} }
public function testUPSRate() public function testUPSRate()
@ -142,28 +149,37 @@ class ShipTest extends PHPUnit_Framework_TestCase
$ups = new UPS\Rate($this->getUPSOptions()); $ups = new UPS\Rate($this->getUPSOptions());
$ups_rates = $ups->get_rates(); $ups_rates = $ups->get_rates();
$this->assertEquals(json_encode([ $ground = new Quote;
0 => [ $ground
'code' => '03', ->setCarrier('ups')
'name' => 'UPS Ground', ->setCode('03')
'cost' => 1900, ->setName('UPS Ground')
], ->setCost(1900);
1 => [
'code' => '02', $twodayair = new Quote;
'name' => 'UPS 2nd Day Air', $twodayair
'cost' => 4900, ->setCarrier('ups')
], ->setCode('02')
2 => [ ->setName('UPS 2nd Day Air')
'code' => '13', ->setCost(4900);
'name' => 'UPS Next Day Air Saver',
'cost' => 8900, $nextdaysaver = new Quote;
], $nextdaysaver
3 => [ ->setCarrier('ups')
'code' => '01', ->setCode('13')
'name' => 'UPS Next Day Air', ->setName('UPS Next Day Air Saver')
'cost' => 9300, ->setCost(8900);
],
]), json_encode($ups_rates)); $nextdayair = new Quote;
$nextdayair
->setCarrier('ups')
->setCode('01')
->setName('UPS Next Day Air')
->setCost(9300);
$expected_return = [$ground, $twodayair, $nextdaysaver, $nextdayair];
$this->assertEquals($expected_return, $ups_rates);
} }
public function testFedexRate() public function testFedexRate()
@ -171,36 +187,44 @@ class ShipTest extends PHPUnit_Framework_TestCase
$fedex = new Fedex\Rate($this->getFedexOptions()); $fedex = new Fedex\Rate($this->getFedexOptions());
$fedex_rates = $fedex->get_rates(); $fedex_rates = $fedex->get_rates();
$this->assertEquals(json_encode([ $ground = new Quote;
3 => [ $ground
'code' => 'GROUND_HOME_DELIVERY', ->setCarrier('fedex')
'name' => 'Ground Home Delivery', ->setCode('GROUND_HOME_DELIVERY')
'cost' => 1600, ->setName('Ground Home Delivery')
'delivery_ts' => NULL, ->setCost(1600)
'transit_time' => 'THREE_DAYS', ->setTransitTime('THREE_DAYS');
],
2 => [ $express = new Quote;
'code' => 'FEDEX_EXPRESS_SAVER', $express
'name' => 'Fedex Express Saver', ->setCarrier('fedex')
'cost' => 2900, ->setCode('FEDEX_EXPRESS_SAVER')
'delivery_ts' => '2014-09-30T20:00:00', ->setName('Fedex Express Saver')
'transit_time' => NULL, ->setCost(2900)
], ->setDeliveryEstimate(new Carbon('2014-09-30T20:00:00'))
1 => [ ->setTransitTime(null);
'code' => 'FEDEX_2_DAY',
'name' => 'Fedex 2 Day', $secondday = new Quote;
'cost' => 4000, $secondday
'delivery_ts' => '2014-09-29T20:00:00', ->setCarrier('fedex')
'transit_time' => NULL, ->setCode('FEDEX_2_DAY')
], ->setName('Fedex 2 Day')
0 => [ ->setCost(4000)
'code' => 'STANDARD_OVERNIGHT', ->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00'))
'name' => 'Standard Overnight', ->setTransitTime(null);
'cost' => 7800,
'delivery_ts' => '2014-09-26T20:00:00', $overnight = new Quote;
'transit_time' => NULL, $overnight
], ->setCarrier('fedex')
]), json_encode($fedex_rates)); ->setCode('STANDARD_OVERNIGHT')
->setName('Standard Overnight')
->setCost(7800)
->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00'))
->setTransitTime(null);
$expected_result = [$ground, $express, $secondday, $overnight];
$this->assertEquals($expected_result, $fedex_rates);
} }
public function testDisplayOptions() public function testDisplayOptions()
@ -219,102 +243,36 @@ class ShipTest extends PHPUnit_Framework_TestCase
$ship = Ship::factory($this->shipping_options); $ship = Ship::factory($this->shipping_options);
$display_rates = $ship->get_display_rates($rates); $display_rates = $ship->get_display_rates($rates);
$this->assertEquals(json_encode([ $post = new Quote;
$post->setCode(4)
->setName('Parcel Post')
->setCost(1001)
->setCarrier('usps');
$fedex_two_day = new Quote;
$fedex_two_day->setCode('FEDEX_2_DAY')
->setName('Fedex 2 Day')
->setCost(4000)
->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00'))
->setCarrier('fedex');
$overnight = new Quote;
$overnight->setCode('STANDARD_OVERNIGHT')
->setName('Standard Overnight')
->setCost(7800)
->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00'))
->setCarrier('fedex');
$this->assertEquals([
'Standard Shipping' => [ 'Standard Shipping' => [
0 => [ $post,
'code' => '4',
'name' => 'Parcel Post',
'cost' => 1001,
'carrier' => 'usps',
],
], ],
'Two-Day Shipping' => [ 'Two-Day Shipping' => [
0 => [ $fedex_two_day,
'code' => 'FEDEX_2_DAY',
'name' => 'Fedex 2 Day',
'cost' => 4000,
'delivery_ts' => '2014-09-29T20:00:00',
'transit_time' => NULL,
'carrier' => 'fedex',
],
], ],
'One-Day Shipping' => [ 'One-Day Shipping' => [
0 => [ $overnight,
'code' => 'STANDARD_OVERNIGHT',
'name' => 'Standard Overnight',
'cost' => 7800,
'delivery_ts' => '2014-09-26T20:00:00',
'transit_time' => NULL,
'carrier' => 'fedex',
],
], ],
]), json_encode($display_rates)); ], $display_rates);
} }
// // Readme Examples:
// public function testUSPSReadmeExample()
// {
// $usps = new USPS\Rate([
// 'prod' => FALSE,
// 'username' => 'XXXX',
// 'password' => 'XXXX',
// 'shipment' => array_merge($this->shipment, [
// 'size' => 'LARGE',
// 'container' => 'RECTANGULAR',
// ]),
// 'approved_codes' => [
// '1', // 1-3 business days
// '4', // 2-8 business days
// ],
// 'request_adapter' => new RateRequest\StubUSPS(),
// ]);
//
// $usps_rates = $usps->get_rates();
// var_export($usps_rates);
// }
//
// public function testUPSReadmeExample()
// {
// $ups = new UPS\Rate([
// 'prod' => FALSE,
// 'shipment' => $this->shipment,
// 'approved_codes' => [
// '03', // 1-5 business days
// '02', // 2 business days
// '01', // next business day 10:30am
// '13', // next business day by 3pm
// '14', // next business day by 8am
// ],
// 'request_adapter' => new RateRequest\StubUPS(),
// ]);
//
// $ups_rates = $ups->get_rates();
// var_export($ups_rates);
// }
//
// public function testFedexReadmeExample()
// {
// $fedex = new Fedex\Rate([
// 'prod' => FALSE,
// 'key' => 'XXXX',
// 'password' => 'XXXX',
// 'account_number' => 'XXXX',
// 'meter_number' => 'XXXX',
// 'drop_off_type' => 'BUSINESS_SERVICE_CENTER',
// 'shipment' => array_merge($this->shipment, [
// 'packaging_type' => 'YOUR_PACKAGING',
// ]),
// 'approved_codes' => [
// 'FEDEX_EXPRESS_SAVER', // 1-3 business days
// 'FEDEX_GROUND', // 1-5 business days
// 'GROUND_HOME_DELIVERY', // 1-5 business days
// 'FEDEX_2_DAY', // 2 business days
// 'STANDARD_OVERNIGHT', // overnight
// ],
// 'request_adapter' => new RateRequest\StubFedex(),
// ]);
//
// $fedex_rates = $fedex->get_rates();
// var_export($fedex_rates);
// }
} }

Loading…
Cancel
Save