@ 2014-02-03T11:53:10Z <?php
class Collivery
{
protected $token;
protected $client;
protected $config;
protected $errors = array();
protected $check_cache = 2;
protected $default_address_id;
protected $client_id;
protected $user_id;
/**
* Setup class with basic Config
*
* @param Array $config Configuration Array
*/
function __construct( array $config = array() )
{
$this->config = (object) array(
'app_name' => 'Default App Name', // Application Name
'app_version' => '0.0.1', // Application Version
'app_host' => '', // Framework/CMS name and version, eg 'Wordpress 3.8.1 WooCommerce 2.0.20' / 'Joomla! 2.5.17 VirtueMart 2.0.26d'
'app_url' => '', // URL your site is hosted on
'user_email' => 'demo@collivery.co.za',
'user_password' => 'demo',
'demo' => false,
);
foreach ( $config as $key => $value ) {
$this->config->$key = $value;
}
if ( $this->config->demo ){
$this->config->user_email = 'demo@collivery.co.za';
$this->config->user_password = 'demo';
}
$this->authenticate();
}
/**
* Setup the Soap Object
*
* @return SoapClient MDS Collivery Soap Client
*/
protected function init ()
{
if ( ! $this->client ){
try {
$this->client = new SoapClient( // Setup the soap client
'http://www.collivery.co.za/wsdl/v2', // URL to WSDL File
array( 'cache_wsdl' => WSDL_CACHE_NONE ) // Don't cache the WSDL file
);
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
}
return true;
}
/**
* Checks if the Soap Client has been set, and returns it.
*
* @return SoapClient Webserver Soap Client
*/
protected function client()
{
if ( ! $this->client ) {
$this->init();
}
if ( ! $this->token ) {
$this->authenticate();
}
return $this->client;
}
/**
* Authenticate and set the token
*
* @return string
*/
protected function authenticate()
{
if ( ( $this->check_cache == 2 ) && Cache::has('collivery.auth') ) {
$authenticate = Cache::get('collivery.auth');
$this->default_address_id = $authenticate['default_address_id'];
$this->client_id = $authenticate['client_id'];
$this->user_id = $authenticate['user_id'];
$this->token = $authenticate['token'];
return true;
} else {
if ( ! $this->initSoap() ) return false;
$user_email = $this->config->user_email;
$user_password = $this->config->user_password;
try {
$authenticate = $this->client->authenticate($user_email, $user_password, $this->token,
array(
'name' => $this->config->app_name . ' mds/collivery/class',
'version' => $this->config->app_version,
'host' => $this->config->app_host,
'url' => $this->config->app_url,
'lang' => 'PHP '. phpversion(),
));
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( is_array( $authenticate ) && isset( $authenticate['token'] ) ){
if ( $this->check_cache != 0 ) Cache::put( 'collivery.auth', $authenticate, 50 );
$this->default_address_id = $authenticate['default_address_id'];
$this->client_id = $authenticate['client_id'];
$this->user_id = $authenticate['user_id'];
$this->token = $authenticate['token'];
return true;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns a list of towns and their ID's for creating new addresses.
* Town can be filtered by country of province (ZAF Only).
*
* @param string $country Filter towns by Country
* @param string $province Filter towns by South African Provinces
* @return array List of towns and their ID's
*/
public function getTowns( $country = "ZAF", $province = null )
{
if ( ( $this->check_cache == 2 ) && is_null( $province ) && Cache::has( 'collivery.towns.'. $country ) ) {
return Cache::get( 'collivery.towns.'.$country );
} elseif ( ( $this->check_cache == 2 ) && ! is_null( $province ) && Cache::has( 'collivery.towns.'. $country .'.'. $province ) ) {
return Cache::get( 'collivery.towns.'.$country.'.'.$province );
} else {
try {
$result = $this->client()->get_towns( $this->token, $country, $province );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['towns'] ) ) {
if ( is_null( $province ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.towns.'. $country, $result['towns'], 60*24 );
} else {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.towns.'. $country .'.'. $province, $result['towns'], 60*24 );
}
return $result['towns'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Allows you to search for town and suburb names starting with the given string.
* The minimum string length to search is two characters.
* Returns a list of towns, suburbs, and the towns the suburbs belong to with their ID's for creating new addresses.
* The idea is that this could be used in an auto complete function.
*
* @param string $name Start of town/suburb name
* @return array List of towns and their ID's
*/
function searchTowns( $name )
{
if ( strlen( $name ) < 2 ) {
return $this->get_towns();
} elseif ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.search_towns.'. $name ) ) {
return Cache::get( 'collivery.search_towns.'. $name );
} else {
try {
$result = $this->client()->search_towns( $name, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.search_towns.'. $name, $result, 60*24 );
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns all the suburbs of a town.
*
* @param int $town_id ID of the Town to return suburbs for
* @return array
*/
public function getSuburbs( $town_id )
{
if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.suburbs.'. $town_id ) ) {
return Cache::get( 'collivery.suburbs.'. $town_id );
} else {
try {
$result = $this->client()->get_suburbs( $town_id, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['suburbs'] ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.suburbs.'. $town_id, $result['suburbs'], 60*24*7 );
return $result['suburbs'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns the type of Address Locations.
* Certain location type incur a surcharge due to time spent during
* delivery.
*
* @return array
*/
public function getLocationTypes()
{
if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.location_types' ) ) {
return Cache::get( 'collivery.location_types' );
} else {
try {
$result = $this->client()->get_location_types( $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['results'] ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.location_types', $result['results'], 60*24*7 );
return $result['results'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns the available Collivery services types.
*
* @return array
*/
public function getServices()
{
if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.services' ) ) {
return Cache::get( 'collivery.services' );
} else {
try {
$result = $this->client()->get_services( $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['results'] ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.services', $result['results'], 60*24*7 );
return $result['results'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns the available Parcel Type ID and value array for use in adding a collivery.
*
* @return array Parcel Types
*/
public function getParcelTypes()
{
if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.parcel_types' ) ) {
return Cache::get( 'collivery.parcel_types' );
} else {
try {
$result = $this->client()->get_parcel_types( $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( is_array( $result ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.parcel_types', $result, 60*24*7 );
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns the available Parcel Type ID and value array for use in adding a collivery.
*
* @param int $address_id The ID of the address you wish to retrieve.
* @return array Address
*/
public function getAddress( $address_id )
{
if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.address.'. $this->client_id .'.'. $address_id ) ) {
return Cache::get( 'collivery.address.'. $this->client_id .'.'. $address_id );
} else {
try {
$result = $this->client()->get_address( $address_id, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['address'] ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.address.'. $this->client_id .'.'. $address_id, $result['address'], 60*24 );
return $result['address'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns all the addresses belonging to a client.
*
* @param array $filter Filter Addresses
* @return array
*/
public function getAddresses( array $filter = array() )
{
if ( ( $this->check_cache == 2 ) && empty( $filter ) && Cache::has( 'collivery.addresses.'. $this->client_id ) ) {
return Cache::get( 'collivery.addresses.'. $this->client_id );
} else {
try {
$result = $this->client()->get_addresses( $this->token, $filter );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['addresses'] ) ) {
if ( $this->check_cache != 0 ) Cache::put( 'collivery.addresses.'. $this->client_id, $result['addresses'], 60*24 );
return $result['addresses'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.4.1 0.010 0.010 16.61 7.4.0 0.009 0.009 16.61 7.3.13 0.016 0.003 16.31 7.3.12 0.012 0.006 16.42 7.3.11 0.007 0.007 16.20 7.3.10 0.009 0.009 16.33 7.3.9 0.011 0.004 16.21 7.3.8 0.004 0.007 16.57 7.3.7 0.007 0.004 16.24 7.3.6 0.004 0.011 16.35 7.3.5 0.003 0.013 16.16 7.3.4 0.008 0.008 16.48 7.3.3 0.009 0.003 16.35 7.3.2 0.007 0.007 18.02 7.3.1 0.010 0.006 18.05 7.3.0 0.003 0.007 18.04 7.2.26 0.011 0.007 16.53 7.2.25 0.011 0.011 16.68 7.2.24 0.006 0.009 16.62 7.2.23 0.003 0.014 16.41 7.2.22 0.010 0.007 16.57 7.2.21 0.004 0.011 16.68 7.2.20 0.004 0.011 16.34 7.2.19 0.016 0.003 16.51 7.2.18 0.009 0.006 16.44 7.2.17 0.003 0.014 16.41 7.2.16 0.003 0.009 16.51 7.2.15 0.007 0.010 18.33 7.2.14 0.000 0.016 18.31 7.2.13 0.006 0.006 18.29 7.2.12 0.010 0.006 18.20 7.2.11 0.012 0.000 18.11 7.2.10 0.007 0.007 18.19 7.2.9 0.013 0.007 18.19 7.2.8 0.007 0.013 18.20 7.2.7 0.010 0.000 18.29 7.2.6 0.000 0.012 18.28 7.2.5 0.007 0.003 18.24 7.2.4 0.006 0.010 18.25 7.2.3 0.004 0.007 18.14 7.2.2 0.003 0.013 18.16 7.2.1 0.003 0.013 18.25 7.2.0 0.004 0.010 18.65 7.1.33 0.009 0.006 16.98 7.1.32 0.007 0.007 17.32 7.1.31 0.009 0.009 17.15 7.1.30 0.003 0.010 17.13 7.1.29 0.010 0.007 17.08 7.1.28 0.009 0.006 17.14 7.1.27 0.007 0.007 17.06 7.1.26 0.006 0.010 17.16 7.1.25 0.006 0.003 17.20 7.1.24 0.010 0.007 17.00 7.1.23 0.006 0.006 17.17 7.1.22 0.004 0.011 17.12 7.1.21 0.000 0.014 17.25 7.1.20 0.007 0.011 17.11 7.1.19 0.004 0.008 17.09 7.1.18 0.006 0.006 17.27 7.1.17 0.003 0.009 17.03 7.1.16 0.004 0.011 17.14 7.1.15 0.006 0.009 16.99 7.1.14 0.007 0.010 17.07 7.1.13 0.006 0.009 17.12 7.1.12 0.003 0.006 17.10 7.1.11 0.003 0.009 17.19 7.1.10 0.003 0.008 17.62 7.1.9 0.004 0.007 17.04 7.1.8 0.004 0.011 17.13 7.1.7 0.004 0.007 16.87 7.1.6 0.010 0.010 18.34 7.1.5 0.015 0.010 25.80 7.1.4 0.010 0.007 17.15 7.1.3 0.004 0.011 17.27 7.1.2 0.004 0.008 17.34 7.1.1 0.006 0.010 17.21 7.1.0 0.007 0.026 19.85 7.0.33 0.003 0.009 16.91 7.0.32 0.000 0.012 16.80 7.0.31 0.004 0.014 16.79 7.0.30 0.011 0.004 16.87 7.0.29 0.004 0.011 16.82 7.0.28 0.007 0.007 16.88 7.0.27 0.004 0.008 16.75 7.0.26 0.004 0.011 17.09 7.0.25 0.003 0.010 16.87 7.0.24 0.003 0.009 16.88 7.0.23 0.007 0.007 16.93 7.0.22 0.003 0.013 16.95 7.0.21 0.009 0.006 16.77 7.0.20 0.005 0.006 16.62 7.0.19 0.006 0.006 16.84 7.0.18 0.005 0.005 17.01 7.0.17 0.011 0.004 16.76 7.0.16 0.008 0.004 16.85 7.0.15 0.007 0.007 16.86 7.0.14 0.005 0.039 19.38 7.0.13 0.004 0.011 16.82 7.0.12 0.007 0.010 16.77 7.0.11 0.010 0.003 16.76 7.0.10 0.003 0.028 18.48 7.0.9 0.010 0.045 18.43 7.0.8 0.006 0.027 18.45 7.0.7 0.018 0.043 18.44 7.0.6 0.009 0.040 18.40 7.0.5 0.010 0.043 18.71 7.0.4 0.008 0.047 17.51 7.0.3 0.010 0.041 17.36 7.0.2 0.006 0.047 17.34 7.0.1 0.005 0.045 17.43 7.0.0 0.005 0.052 17.41 5.6.40 0.010 0.003 15.63 5.6.39 0.012 0.006 15.93 5.6.38 0.009 0.006 15.99 5.6.37 0.010 0.006 15.48 5.6.36 0.015 0.000 15.84 5.6.35 0.007 0.011 15.76 5.6.34 0.006 0.006 16.15 5.6.33 0.008 0.004 15.83 5.6.32 0.004 0.007 15.71 5.6.31 0.009 0.003 15.42 5.6.30 0.014 0.000 15.83 5.6.29 0.011 0.004 15.98 5.6.28 0.003 0.041 18.36 5.6.27 0.009 0.006 15.54 5.6.26 0.006 0.009 15.72 5.6.25 0.006 0.024 18.39 5.6.24 0.003 0.038 18.15 5.6.23 0.008 0.032 18.17 5.6.22 0.005 0.044 18.35 5.6.21 0.010 0.042 18.37 5.6.20 0.012 0.040 18.40 5.6.19 0.008 0.023 18.45 5.6.18 0.006 0.024 18.52 5.6.17 0.007 0.043 18.36 5.6.16 0.005 0.030 18.50 5.6.15 0.007 0.044 18.47 5.6.14 0.010 0.038 18.51 5.6.13 0.008 0.040 18.38 5.6.12 0.002 0.033 18.48 5.6.11 0.006 0.036 18.36 5.6.10 0.007 0.043 18.40 5.6.9 0.007 0.044 18.43 5.6.8 0.004 0.047 18.02 5.6.7 0.011 0.038 18.04 5.6.6 0.007 0.040 18.01 5.6.5 0.012 0.032 18.15 5.6.4 0.005 0.047 18.18 5.6.3 0.013 0.039 17.98 5.6.2 0.005 0.045 18.11 5.6.1 0.004 0.032 18.03 5.6.0 0.009 0.027 18.06 5.5.38 0.003 0.027 18.19 5.5.37 0.005 0.040 18.09 5.5.36 0.007 0.045 18.07 5.5.35 0.005 0.028 17.92 5.5.34 0.010 0.042 18.14 5.5.33 0.005 0.047 18.25 5.5.32 0.013 0.045 18.27 5.5.31 0.007 0.037 18.22 5.5.30 0.007 0.046 18.18 5.5.29 0.009 0.037 18.15 5.5.28 0.007 0.043 18.17 5.5.27 0.010 0.043 18.35 5.5.26 0.005 0.046 18.23 5.5.25 0.005 0.044 18.13 5.5.24 0.005 0.046 17.99 5.5.23 0.012 0.040 18.02 5.5.22 0.009 0.040 18.00 5.5.21 0.008 0.023 17.92 5.5.20 0.011 0.036 17.86 5.5.19 0.011 0.032 17.93 5.5.18 0.007 0.042 17.80 5.5.17 0.007 0.007 15.77 5.5.16 0.002 0.041 17.95 5.5.15 0.008 0.033 17.88 5.5.14 0.013 0.030 18.02 5.5.13 0.007 0.039 17.89 5.5.12 0.011 0.044 17.78 5.5.11 0.007 0.041 17.90 5.5.10 0.010 0.040 17.88 5.5.9 0.010 0.040 17.80 5.5.8 0.007 0.043 17.72 5.5.7 0.001 0.031 17.85 5.5.6 0.003 0.038 17.83 5.5.5 0.008 0.040 17.92 5.5.4 0.004 0.041 17.87 5.5.3 0.003 0.032 17.77 5.5.2 0.007 0.044 17.73 5.5.1 0.010 0.042 17.78 5.5.0 0.008 0.032 17.79 5.4.45 0.008 0.031 15.99 5.4.44 0.005 0.030 15.85 5.4.43 0.012 0.038 15.85 5.4.42 0.004 0.045 15.84 5.4.41 0.007 0.045 15.86 5.4.40 0.009 0.035 15.67 5.4.39 0.012 0.021 15.71 5.4.38 0.012 0.035 15.76 5.4.37 0.014 0.032 15.69 5.4.36 0.006 0.026 15.76 5.4.35 0.004 0.027 15.69 5.4.34 0.004 0.034 15.69 5.4.33 0.000 0.012 12.46 5.4.32 0.002 0.028 15.77 5.4.31 0.008 0.038 15.68 5.4.30 0.008 0.038 15.78 5.4.29 0.003 0.031 15.85 5.4.28 0.005 0.043 15.88 5.4.27 0.005 0.042 15.71 5.4.26 0.007 0.038 15.75 5.4.25 0.007 0.042 15.84 5.4.24 0.010 0.038 15.76 5.4.23 0.005 0.043 15.82 5.4.22 0.007 0.040 15.66 5.4.21 0.002 0.044 15.66 5.4.20 0.013 0.033 15.76 5.4.19 0.002 0.029 15.75 5.4.18 0.005 0.026 15.79 5.4.17 0.007 0.038 15.82 5.4.16 0.005 0.038 15.79 5.4.15 0.008 0.035 15.69 5.4.14 0.005 0.035 14.44 5.4.13 0.003 0.026 14.49 5.4.12 0.003 0.025 14.42 5.4.11 0.012 0.035 14.50 5.4.10 0.003 0.030 14.43 5.4.9 0.010 0.020 14.58 5.4.8 0.003 0.041 14.46 5.4.7 0.010 0.035 14.44 5.4.6 0.008 0.036 14.49 5.4.5 0.006 0.036 14.56 5.4.4 0.006 0.036 14.51 5.4.3 0.005 0.025 14.47 5.4.2 0.008 0.024 14.49 5.4.1 0.003 0.033 14.45 5.4.0 0.008 0.037 14.26 5.3.29 0.005 0.044 13.81 5.3.28 0.005 0.044 13.79 5.3.27 0.004 0.026 13.82 5.3.26 0.005 0.023 13.85 5.3.25 0.009 0.038 13.84 5.3.24 0.004 0.027 13.80 5.3.23 0.007 0.020 13.77 5.3.22 0.007 0.022 13.75 5.3.21 0.005 0.043 13.78 5.3.20 0.011 0.023 13.85 5.3.19 0.007 0.042 13.94 5.3.18 0.010 0.027 13.72 5.3.17 0.006 0.033 13.85 5.3.16 0.005 0.040 14.02 5.3.15 0.007 0.025 13.85 5.3.14 0.004 0.029 13.84 5.3.13 0.002 0.042 13.90 5.3.12 0.009 0.031 13.88 5.3.11 0.006 0.040 13.76 5.3.10 0.005 0.041 13.55 5.3.9 0.007 0.039 13.55 5.3.8 0.003 0.049 13.55 5.3.7 0.010 0.033 13.37 5.3.6 0.003 0.037 13.44 5.3.5 0.006 0.024 13.41 5.3.4 0.005 0.026 13.49 5.3.3 0.003 0.042 13.35 5.3.2 0.007 0.034 13.31 5.3.1 0.005 0.038 13.16 5.3.0 0.010 0.033 13.24
preferences:dark mode live preview
34.01 ms | 401 KiB | 5 Q