You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ShippingRates/src/RateAdapter.php

63 lines
1.2 KiB
PHP

<?php
namespace pdt256\Shipping;
use Exception;
abstract class RateAdapter
{
protected $isProduction;
/** @var Shipment */
protected $shipment;
protected $data;
protected $response;
protected $rates;
/** @var @var RateRequest\Adapter */
protected $rateRequest;
/**
* Prepare XML
*/
abstract protected function prepare();
/**
* Curl Request
*/
abstract protected function execute();
/**
* Convert to shipping rates array
*/
abstract protected function process();
public function __construct($options = [])
{
$this->rates = [];
$this->isProduction = (bool) Arr::get($options, 'prod', false);
$this->shipment = Arr::get($options, 'shipment');
}
public function setRequestAdapter(RateRequest\Adapter $rateRequest)
{
$this->rateRequest = $rateRequest;
}
public function getRates()
{
$this
->prepare()
->execute()
->process()
->sortByCost();
return array_values($this->rates);
}
protected function sortByCost()
{
uasort($this->rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());'));
}
}