3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Cache { public static function has( $name ){ return false; } public static function get( $name ){ return null; } public static function put( $name, $value, $time = 1440 ){ return true; } } 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->init() ) 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; } } } /** * Handle error messages in SoapFault * * @param SoapFault $e SoapFault Object */ protected function catchSoapFault( $e ) { $this->setError( $e->faultcode, $e->faultstring ); } /** * Add a new error * * @param string $id Error ID * @param string $text Error text */ protected function setError( $id, $text ) { $this->errors[ $id ] = $text; } /** * Retrieve errors */ public function getErrors() { return $this->errors; } /** * Check if this instance has an error */ public function hasErrors() { return !empty($this->errors); } /** * Clears all the Errors */ public function clearErrors() { $this->errors = array(); } /** * Disable Cached completely and retrieve data directly from the webservice */ public function disableCache() { $this->check_cache = 0; } /** * Ignore Cached data and retrieve data directly from the webservice * Save returned data to Cache */ public function ignoreCache() { $this->check_cache = 1; } /** * Check if cache exists before querying the webservice * If webservice was queried, save returned data to Cache */ public function enableCache() { $this->check_cache = 2; } } $col = new Collivery;

preferences:
35.22 ms | 402 KiB | 5 Q