diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index 82f8dad..20c2016 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -74,11 +74,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->key, 'key'); Validator::checkIfNull($this->password, 'password'); Validator::checkIfNull($this->accountNumber, 'accountNumber'); diff --git a/src/RateAdapter.php b/src/RateAdapter.php index 4bf3a5b..6ed0a6f 100644 --- a/src/RateAdapter.php +++ b/src/RateAdapter.php @@ -35,6 +35,19 @@ abstract class RateAdapter */ abstract protected function process(); + /** + * @throws \LogicException + * To be called from validate() when packages have to have 3 dimensions and weight + */ + protected function validatePackages() + { + foreach ($this->shipment->getPackages() as $package) { + Validator::checkIfNull($package->getWeight(), 'weight'); + Validator::checkIfNull($package->getLength(), 'length'); + Validator::checkIfNull($package->getHeight(), 'height'); + Validator::checkIfNull($package->getWidth(), 'width'); + } + } public function __construct($options = []) { $this->rates = []; diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index c8ecf67..c1242e8 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -99,11 +99,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->accessKey, 'accessKey'); Validator::checkIfNull($this->userId, 'userId'); Validator::checkIfNull($this->password, 'password'); diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index 2ade0ec..b6c815a 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -81,11 +81,7 @@ class Rate extends RateAdapter } protected function validate() { - foreach ($this->shipment->getPackages() as $package) { - Validator::checkIfNull($package->getWeight(), 'weight'); - Validator::checkIfNull($package->getLength(), 'length'); - Validator::checkIfNull($package->getHeight(), 'height'); - } + $this->validatePackages(); Validator::checkIfNull($this->username, 'username'); Validator::checkIfNull($this->password, 'password'); Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');