// This \Closure will find actual handler by date from handler's extra data $selector = function ($handlers, PackageInterface $package) { /** @var CalculatorInterface $currentHandler */ $currentHandler = null; $currentDate = null; /** @var CalculatorHandlerInterface $handler */ foreach ($handlers as $handler) { $extraData = $handler->get('extra_data'); $date = new \DateTime($extraData['date']); if ($package->getCalculationDate() >= $date && (is_null($currentHandler) || $date > $currentDate)) { $currentHandler = $handler; } } return $currentHandler; }; $calculator = new SelectiveCalculator(['handlers' => [AsendiaCalculatorHandler::create($config1), DhlCalculatorHandler::create($config2)], 'selector' => $selector]); $weight = new Weight(); $weight->setValue(10); $weight->setUnit('lb'); $dimensions = new Dimensions(); $dimensions->setLength(10); $dimensions->setWidth(10); $dimensions->setHeight(10); $dimensions->setUnit('in'); $senderAddress = new Address(); $senderAddress->setCountryCode('USA'); $recipientAddress = new Address(); $recipientAddress->setCountryCode('RUS'); $package = new Package(); // Change this date to calculate shipping by other calculator. // For example, `2015-12-12`.
public function testValidateWeightException() { $this->setExpectedException('EsteIt\\ShippingCalculator\\Exception\\InvalidWeightException', 'Sender country weight limit is exceeded.'); $calculator = new AsendiaCalculatorHandler(['zone_calculators' => [], 'import_countries' => [$this->getFixture('import_country_usa')], 'export_countries' => [$this->getFixture('export_country_usa')], 'fuel_subcharge' => 0.07000000000000001, 'mass_unit' => 'lb', 'dimensions_unit' => 'in', 'maximum_dimension' => 41.338, 'maximum_girth' => 77.755]); $calculator->validateWeight($this->getFixture('package_3')); }
<?php use EsteIt\ShippingCalculator\Calculator\BaseCalculator; use EsteIt\ShippingCalculator\CalculatorHandler\AsendiaCalculatorHandler; use EsteIt\ShippingCalculator\Model\Weight; use EsteIt\ShippingCalculator\Model\Dimensions; use EsteIt\ShippingCalculator\Model\Address; use EsteIt\ShippingCalculator\Model\Package; include_once __DIR__ . '/../vendor/autoload.php'; $config = (include __DIR__ . '/../src/Resources/Asendia/PMEI/tariff_2015_06_15.php'); $calculator = new BaseCalculator(['handler' => AsendiaCalculatorHandler::create($config)]); $weight = new Weight(); $weight->setValue(10); $weight->setUnit('lb'); $dimensions = new Dimensions(); $dimensions->setLength(10); $dimensions->setWidth(10); $dimensions->setHeight(10); $dimensions->setUnit('in'); $senderAddress = new Address(); $senderAddress->setCountryCode('USA'); $recipientAddress = new Address(); $recipientAddress->setCountryCode('RUS'); $package = new Package(); $package->setCalculationDate(new \DateTime()); $package->setWeight($weight); $package->setDimensions($dimensions); $package->setSenderAddress($senderAddress); $package->setRecipientAddress($recipientAddress); $result = $calculator->calculate($package); var_dump($result->getTotalCost());