From ef5bbc91ddbd36851cbf33cd909a5c0ffc7f2a53 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 29 May 2020 17:38:34 -0600 Subject: [PATCH] Add pounds/ounces --- .gitignore | 1 + LICENSE.txt | 1 + README.md | 41 +++++++------------- composer.json | 8 +++- nbproject/project.properties | 22 +++++++++++ nbproject/project.xml | 9 +++++ src/Package.php | 72 ++++++++++++++++++++++++------------ src/USPS/Rate.php | 52 ++++++++++++-------------- tests/PackageTest.php | 8 ++++ 9 files changed, 133 insertions(+), 81 deletions(-) create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml diff --git a/.gitignore b/.gitignore index 8c1d8ea..c6495ad 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ coverage_report !.idea/runConfigurations/ .DS_Store live_phpunit.sh +/nbproject/private/ \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt index e8a0d4c..8686506 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2014 Jamie Isaacs +Copyright (c) 2020 Netsyms Technologies Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 97a52cc..3523f60 100644 --- a/README.md +++ b/README.md @@ -15,19 +15,6 @@ Multiple packages can be added to get additional rates. All code (including tests) conform to the PSR-2 coding standards. The namespace and autoloader are using the PSR-4 standard. -All pull requests are processed by Travis CI to conform to PSR-2 and to verify all unit tests pass. - -## Installation - -Add the following lines to your ``composer.json`` file. - -```JSON -{ - "require": { - "pdt256/shipping": "1.0.*" - } -} -``` ## Example @@ -56,7 +43,7 @@ $shipment->addPackage($package); ## UPS (Stub) Example -Below is an example request to get shipping rates from the UPS API. +Below is an example request to get shipping rates from the UPS API. Notice: The below line uses a stub class to fake a response from the UPS API. You can immediately use this method in your code until you get an account with UPS. @@ -93,7 +80,7 @@ Output array sorted by cost: (in cents) ```php array ( - 0 => + 0 => pdt256\Shipping\Quote::__set_state(array( 'code' => '03', 'name' => 'UPS Ground', @@ -102,7 +89,7 @@ array ( 'deliveryEstimate' => NULL, 'carrier' => 'ups', )), - 1 => + 1 => pdt256\Shipping\Quote::__set_state(array( 'code' => '02', 'name' => 'UPS 2nd Day Air', @@ -111,7 +98,7 @@ array ( 'deliveryEstimate' => NULL, 'carrier' => 'ups', )), - 2 => + 2 => pdt256\Shipping\Quote::__set_state(array( 'code' => '13', 'name' => 'UPS Next Day Air Saver', @@ -120,7 +107,7 @@ array ( 'deliveryEstimate' => NULL, 'carrier' => 'ups', )), - 3 => + 3 => pdt256\Shipping\Quote::__set_state(array( 'code' => '01', 'name' => 'UPS Next Day Air', @@ -157,7 +144,7 @@ Output array sorted by cost: (in cents) ```php array ( - 0 => + 0 => pdt256\Shipping\Quote::__set_state(array( 'code' => '4', 'name' => 'Parcel Post', @@ -166,7 +153,7 @@ array ( 'deliveryEstimate' => NULL, 'carrier' => 'usps', )), - 1 => + 1 => pdt256\Shipping\Quote::__set_state(array( 'code' => '1', 'name' => 'Priority Mail', @@ -209,7 +196,7 @@ Output array sorted by cost: (in cents) ```php array ( - 0 => + 0 => pdt256\Shipping\Quote::__set_state(array( 'code' => 'GROUND_HOME_DELIVERY', 'name' => 'Ground Home Delivery', @@ -218,13 +205,13 @@ array ( 'deliveryEstimate' => NULL, 'carrier' => 'fedex', )), - 1 => + 1 => pdt256\Shipping\Quote::__set_state(array( 'code' => 'FEDEX_EXPRESS_SAVER', 'name' => 'Fedex Express Saver', 'cost' => 2989, 'transitTime' => NULL, - 'deliveryEstimate' => + 'deliveryEstimate' => DateTime::__set_state(array( 'date' => '2014-09-30 20:00:00', 'timezone_type' => 3, @@ -232,13 +219,13 @@ array ( )), 'carrier' => 'fedex', )), - 2 => + 2 => pdt256\Shipping\Quote::__set_state(array( 'code' => 'FEDEX_2_DAY', 'name' => 'Fedex 2 Day', 'cost' => 4072, 'transitTime' => NULL, - 'deliveryEstimate' => + 'deliveryEstimate' => DateTime::__set_state(array( 'date' => '2014-09-29 20:00:00', 'timezone_type' => 3, @@ -246,13 +233,13 @@ array ( )), 'carrier' => 'fedex', )), - 3 => + 3 => pdt256\Shipping\Quote::__set_state(array( 'code' => 'STANDARD_OVERNIGHT', 'name' => 'Standard Overnight', 'cost' => 7834, 'transitTime' => NULL, - 'deliveryEstimate' => + 'deliveryEstimate' => DateTime::__set_state(array( 'date' => '2014-09-26 20:00:00', 'timezone_type' => 3, diff --git a/composer.json b/composer.json index 4a71f40..145139b 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "pdt256/shipping", + "name": "netsyms/shippingrates", "description": "Shipping Rate API", "license": "MIT", "keywords": ["ship", "ups", "usps", "fedex"], @@ -7,7 +7,11 @@ { "name": "Jamie Isaacs", "email": "pdt256@gmail.com" - } + }, + { + "name": "Netsyms Technologies", + "email": "opensource@netsyms.com" + }, ], "extra": { "branch-alias": { diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..cc9fabe --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,22 @@ +auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=false +auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=false +auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path= +auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=false +auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_path= +auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_enabled=false +auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_path= +auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_enabled=false +auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path= +auxiliary.org-netbeans-modules-php-phpunit.test_2e_groups_2e_ask=false +auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_all=false +auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_phpunit_2e_only=false +file.reference.ShippingRates-tests=tests +include.path=${php.global.include.path} +php.version=PHP_73 +source.encoding=UTF-8 +src.dir=src +tags.asp=false +tags.short=false +test.src.dir=${file.reference.ShippingRates-tests} +testing.providers=PhpUnit +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..537fcb5 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,9 @@ + + + org.netbeans.modules.php.project + + + ShippingRates + + + diff --git a/src/Package.php b/src/Package.php index b873fb1..27851fb 100644 --- a/src/Package.php +++ b/src/Package.php @@ -1,35 +1,63 @@ -getPounds())) { + $weight += $this->getPounds(); + } + if (!empty($this->getOunces())) { + $weight += ($this->getOunces() / 16); + } + if ($weight == 0) { + return null; + } + return $weight; + } + + public function getPounds() { + return $this->pounds ?? 0; + } + + public function getOunces() { + return $this->ounces ?? 0; + } + + public function setWeight($pounds) { + return $this->setPounds($pounds)->setOunces(0); + } + /** - * @return mixed + * @param mixed $pounds + * @return $this */ - public function getWeight() - { - return $this->weight; + public function setPounds($pounds) { + $this->pounds = $pounds; + return $this; } /** - * @param mixed $weight + * @param mixed $ounces * @return $this */ - public function setWeight($weight) - { - $this->weight = $weight; + public function setOunces($ounces) { + $this->ounces = $ounces; return $this; } /** * @return mixed */ - public function getWidth() - { + public function getWidth() { return $this->width; } @@ -37,8 +65,7 @@ class Package * @param mixed $width * @return $this */ - public function setWidth($width) - { + public function setWidth($width) { $this->width = $width; return $this; } @@ -46,8 +73,7 @@ class Package /** * @return mixed */ - public function getLength() - { + public function getLength() { return $this->length; } @@ -55,8 +81,7 @@ class Package * @param mixed $length * @return $this */ - public function setLength($length) - { + public function setLength($length) { $this->length = $length; return $this; } @@ -64,8 +89,7 @@ class Package /** * @return mixed */ - public function getHeight() - { + public function getHeight() { return $this->height; } @@ -73,9 +97,9 @@ class Package * @param mixed $height * @return $this */ - public function setHeight($height) - { + public function setHeight($height) { $this->height = $height; return $this; } + } diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index b6c815a..7183f23 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -1,4 +1,5 @@ [ '00' => 'First-Class Mail Parcel', @@ -67,20 +67,19 @@ class Rate extends RateAdapter ], ]; - public function __construct($options = []) - { + public function __construct($options = []) { parent::__construct($options); $this->username = Arr::get($options, 'username'); $this->password = Arr::get($options, 'password'); $this->approvedCodes = Arr::get($options, 'approvedCodes', [ - '1', - '4', + '1', + '4', ]); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get())); } - protected function validate() - { + + protected function validate() { $this->validatePackages(); Validator::checkIfNull($this->username, 'username'); Validator::checkIfNull($this->password, 'password'); @@ -89,8 +88,8 @@ class Rate extends RateAdapter return $this; } - protected function prepare() - { + + protected function prepare() { $packages = ''; $sequence_number = 0; foreach ($this->shipment->getPackages() as $p) { @@ -119,33 +118,30 @@ class Rate extends RateAdapter $container = 'VARIABLE'; } - $packages .= - '' . + $packages .= '' . 'ALL' . '' . $this->shipment->getFromPostalCode() . '' . '' . $this->shipment->getToPostalCode() . '' . - '' . $p->getWeight() . '' . - '0' . + '' . $p->getPounds() . '' . + '' . $p->getOunces() . '' . '' . $container . '' . '' . $size . '' . '' . $p->getWidth() . '' . '' . $p->getLength() . '' . '' . $p->getHeight() . '' . '' . 'False' . '' . - ''; + ''; } - $this->data = - '' . + $this->data = '' . '' . $packages . - ''; + ''; return $this; } - protected function execute() - { + protected function execute() { if ($this->isProduction) { $url = $this->urlProd; } else { @@ -159,8 +155,7 @@ class Rate extends RateAdapter return $this; } - protected function process() - { + protected function process() { try { $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadXml($this->response); @@ -195,10 +190,10 @@ class Rate extends RateAdapter $quote = new Quote; $quote - ->setCarrier('usps') - ->setCode($code) - ->setName($name) - ->setCost((int) $cost); + ->setCarrier('usps') + ->setCode($code) + ->setName($name) + ->setCost((int) $cost); $rates[$quote->getCode()] = $quote; } @@ -207,4 +202,5 @@ class Rate extends RateAdapter return $this; } + } diff --git a/tests/PackageTest.php b/tests/PackageTest.php index 4da2d6b..1e275f7 100644 --- a/tests/PackageTest.php +++ b/tests/PackageTest.php @@ -14,8 +14,16 @@ class PackageTest extends TestCase $package->setHeight(8); $this->assertEquals(5, $package->getWeight()); + $this->assertEquals(5, $package->getPounds()); + $this->assertEquals(0, $package->getOunces()); $this->assertEquals(6, $package->getWidth()); $this->assertEquals(7, $package->getLength()); $this->assertEquals(8, $package->getHeight()); + + $package->setPounds(3); + $package->setOunces(4); + $this->assertEquals(3, $package->getPounds()); + $this->assertEquals(4, $package->getOunces()); + $this->assertEquals(3.25, $package->getWeight()); } }