Compare commits

..

No commits in common. "master" and "1.0.1" have entirely different histories.

11 changed files with 84 additions and 141 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ coverage_report
!.idea/runConfigurations/ !.idea/runConfigurations/
.DS_Store .DS_Store
live_phpunit.sh live_phpunit.sh
/nbproject/private/

View File

@ -1,7 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 Jamie Isaacs <pdt256@gmail.com> Copyright (c) 2014 Jamie Isaacs <pdt256@gmail.com>
Copyright (c) 2020 Netsyms Technologies <opensource@netsyms.com>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -15,6 +15,19 @@ Multiple packages can be added to get additional rates.
All code (including tests) conform to the PSR-2 coding standards. All code (including tests) conform to the PSR-2 coding standards.
The namespace and autoloader are using the PSR-4 standard. 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 ## Example

View File

@ -1,5 +1,5 @@
{ {
"name": "netsyms/shippingrates", "name": "pdt256/shipping",
"description": "Shipping Rate API", "description": "Shipping Rate API",
"license": "MIT", "license": "MIT",
"keywords": ["ship", "ups", "usps", "fedex"], "keywords": ["ship", "ups", "usps", "fedex"],
@ -7,10 +7,6 @@
{ {
"name": "Jamie Isaacs", "name": "Jamie Isaacs",
"email": "pdt256@gmail.com" "email": "pdt256@gmail.com"
},
{
"name": "Netsyms Technologies",
"email": "opensource@netsyms.com"
} }
], ],
"extra": { "extra": {

View File

@ -1,22 +0,0 @@
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=.

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>ShippingRates</name>
</data>
</configuration>
</project>

View File

@ -14,9 +14,4 @@
<directory suffix=".php">./tests</directory> <directory suffix=".php">./tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit> </phpunit>

View File

@ -1,63 +1,35 @@
<?php <?php namespace pdt256\Shipping;
namespace pdt256\Shipping; class Package
{
class Package { protected $weight;
protected $pounds;
protected $ounces;
protected $width; protected $width;
protected $length; protected $length;
protected $height; protected $height;
public function getWeight() { /**
$weight = 0; * @return mixed
if (!empty($this->getPounds())) { */
$weight += $this->getPounds(); public function getWeight()
} {
if (!empty($this->getOunces())) { return $this->weight;
$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);
} }
/** /**
* @param mixed $pounds * @param mixed $weight
* @return $this * @return $this
*/ */
public function setPounds($pounds) { public function setWeight($weight)
$this->pounds = $pounds; {
return $this; $this->weight = $weight;
}
/**
* @param mixed $ounces
* @return $this
*/
public function setOunces($ounces) {
$this->ounces = $ounces;
return $this; return $this;
} }
/** /**
* @return mixed * @return mixed
*/ */
public function getWidth() { public function getWidth()
{
return $this->width; return $this->width;
} }
@ -65,7 +37,8 @@ class Package {
* @param mixed $width * @param mixed $width
* @return $this * @return $this
*/ */
public function setWidth($width) { public function setWidth($width)
{
$this->width = $width; $this->width = $width;
return $this; return $this;
} }
@ -73,7 +46,8 @@ class Package {
/** /**
* @return mixed * @return mixed
*/ */
public function getLength() { public function getLength()
{
return $this->length; return $this->length;
} }
@ -81,7 +55,8 @@ class Package {
* @param mixed $length * @param mixed $length
* @return $this * @return $this
*/ */
public function setLength($length) { public function setLength($length)
{
$this->length = $length; $this->length = $length;
return $this; return $this;
} }
@ -89,7 +64,8 @@ class Package {
/** /**
* @return mixed * @return mixed
*/ */
public function getHeight() { public function getHeight()
{
return $this->height; return $this->height;
} }
@ -97,9 +73,9 @@ class Package {
* @param mixed $height * @param mixed $height
* @return $this * @return $this
*/ */
public function setHeight($height) { public function setHeight($height)
{
$this->height = $height; $this->height = $height;
return $this; return $this;
} }
} }

View File

@ -3,8 +3,8 @@ namespace pdt256\Shipping\RateRequest;
abstract class Adapter abstract class Adapter
{ {
protected $curlConnectTimeoutInMilliseconds = 1500; protected $curlConnectTimeoutInMilliseconds = 1000;
protected $curlDownloadTimeoutInSeconds = 50; protected $curlDownloadTimeoutInSeconds = 11;
abstract public function execute($url, $data = null); abstract public function execute($url, $data = null);
} }

View File

@ -1,5 +1,4 @@
<?php <?php
namespace pdt256\Shipping\USPS; namespace pdt256\Shipping\USPS;
use pdt256\Shipping; use pdt256\Shipping;
@ -11,17 +10,18 @@ use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
class Rate extends RateAdapter { 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; private $username;
private $password; private $password;
/** /**
* Codes of appropriate shipping types. Default value is specified in __construct. * Codes of appropriate shipping types. Default value is specified in __construct.
*/ */
public $approvedCodes; public $approvedCodes;
private $shipping_codes = [ private $shipping_codes = [
'domestic' => [ 'domestic' => [
'00' => 'First-Class Mail Parcel', '00' => 'First-Class Mail Parcel',
@ -67,19 +67,20 @@ class Rate extends RateAdapter {
], ],
]; ];
public function __construct($options = []) { public function __construct($options = [])
{
parent::__construct($options); parent::__construct($options);
$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', '1',
'4', '4',
]); ]);
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get()));
} }
protected function validate()
protected function validate() { {
$this->validatePackages(); $this->validatePackages();
Validator::checkIfNull($this->username, 'username'); Validator::checkIfNull($this->username, 'username');
Validator::checkIfNull($this->password, 'password'); Validator::checkIfNull($this->password, 'password');
@ -88,8 +89,8 @@ class Rate extends RateAdapter {
return $this; return $this;
} }
protected function prepare()
protected function prepare() { {
$packages = ''; $packages = '';
$sequence_number = 0; $sequence_number = 0;
foreach ($this->shipment->getPackages() as $p) { foreach ($this->shipment->getPackages() as $p) {
@ -118,30 +119,33 @@ class Rate extends RateAdapter {
$container = 'VARIABLE'; $container = 'VARIABLE';
} }
$packages .= '<Package ID="' . $sequence_number . '">' . $packages .=
'<Package ID="' . $sequence_number . '">' .
'<Service>ALL</Service>' . '<Service>ALL</Service>' .
'<ZipOrigination>' . $this->shipment->getFromPostalCode() . '</ZipOrigination>' . '<ZipOrigination>' . $this->shipment->getFromPostalCode() . '</ZipOrigination>' .
'<ZipDestination>' . $this->shipment->getToPostalCode() . '</ZipDestination>' . '<ZipDestination>' . $this->shipment->getToPostalCode() . '</ZipDestination>' .
'<Pounds>' . $p->getPounds() . '</Pounds>' . '<Pounds>' . $p->getWeight() . '</Pounds>' .
'<Ounces>' . $p->getOunces() . '</Ounces>' . '<Ounces>0</Ounces>' .
'<Container>' . $container . '</Container>' . '<Container>' . $container . '</Container>' .
'<Size>' . $size . '</Size>' . '<Size>' . $size . '</Size>' .
'<Width>' . $p->getWidth() . '</Width>' . '<Width>' . $p->getWidth() . '</Width>' .
'<Length>' . $p->getLength() . '</Length>' . '<Length>' . $p->getLength() . '</Length>' .
'<Height>' . $p->getHeight() . '</Height>' . '<Height>' . $p->getHeight() . '</Height>' .
'<Machinable>' . 'False' . '</Machinable>' . '<Machinable>' . 'False' . '</Machinable>' .
'</Package>'; '</Package>';
} }
$this->data = '<RateV4Request USERID="' . $this->username . '">' . $this->data =
'<RateV4Request USERID="' . $this->username . '">' .
'<Revision/>' . '<Revision/>' .
$packages . $packages .
'</RateV4Request>'; '</RateV4Request>';
return $this; return $this;
} }
protected function execute() { protected function execute()
{
if ($this->isProduction) { if ($this->isProduction) {
$url = $this->urlProd; $url = $this->urlProd;
} else { } else {
@ -155,7 +159,8 @@ class Rate extends RateAdapter {
return $this; return $this;
} }
protected function process() { protected function process()
{
try { try {
$dom = new DOMDocument('1.0', 'UTF-8'); $dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($this->response); $dom->loadXml($this->response);
@ -190,10 +195,10 @@ class Rate extends RateAdapter {
$quote = new Quote; $quote = new Quote;
$quote $quote
->setCarrier('usps') ->setCarrier('usps')
->setCode($code) ->setCode($code)
->setName($name) ->setName($name)
->setCost((int) $cost); ->setCost((int) $cost);
$rates[$quote->getCode()] = $quote; $rates[$quote->getCode()] = $quote;
} }
@ -202,5 +207,4 @@ class Rate extends RateAdapter {
return $this; return $this;
} }
} }

View File

@ -14,16 +14,8 @@ class PackageTest extends TestCase
$package->setHeight(8); $package->setHeight(8);
$this->assertEquals(5, $package->getWeight()); $this->assertEquals(5, $package->getWeight());
$this->assertEquals(5, $package->getPounds());
$this->assertEquals(0, $package->getOunces());
$this->assertEquals(6, $package->getWidth()); $this->assertEquals(6, $package->getWidth());
$this->assertEquals(7, $package->getLength()); $this->assertEquals(7, $package->getLength());
$this->assertEquals(8, $package->getHeight()); $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());
} }
} }