3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 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; } } } /** * Returns the Contact people of a given Address ID. * * @param int $address_id Address ID * @return array */ public function getContacts( $address_id ) { if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.contacts.'. $this->client_id .'.'. $address_id ) ) { return Cache::get( 'collivery.contacts.'. $this->client_id .'.'. $address_id ); } else { try { $result = $this->client()->get_contacts( $address_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['contacts'] ) ) { if ( $this->check_cache != 0 ) Cache::put( 'collivery.contacts.'. $this->client_id .'.'. $address_id, $result['contacts'], 60*24 ); return $result['contacts']; } 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 POD image for a given Waybill Number. * * @param int $collivery_id Collivery waybill number * @return array */ public function getPod( $collivery_id ) { if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.pod.'. $this->client_id .'.'. $collivery_id ) ) { return Cache::get( 'collivery.pod.'. $this->client_id .'.'. $collivery_id ); } else { try { $result = $this->client()->get_pod( $collivery_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['pod'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); else if ( $this->check_cache != 0 ) Cache::put( 'collivery.pod.'. $this->client_id .'.'. $collivery_id, $result['pod'], 60*24 ); return $result['pod']; } 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 avaibale parcel images for a given Waybill Number. * * @param int $collivery_id Collivery waybill number * @return array */ public function getParcelImageList( $collivery_id ) { if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.parcel_image_list.'. $this->client_id .'.'. $collivery_id ) ) { return Cache::get( 'collivery.parcel_image_list.'. $this->client_id .'.'. $collivery_id ); } else { try { $result = $this->client()->get_parcel_image_list( $collivery_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['images'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); else if ( $this->check_cache != 0 ) Cache::put( 'collivery.parcel_image_list.'. $this->client_id .'.'. $collivery_id, $result['images'], 60*12 ); return $result['images']; } 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 image of a given parcel-id of a waybill. * If the Waybill number is 54321 and there are 3 parcels, they would * be referenced by id's 54321-1, 54321-2 and 54321-3. * * @param string $parcel_id Parcel ID * @return array Array containing all the information * about the image including the image * itself in base64 */ public function getParcelImage( $parcel_id ) { if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.parcel_image.'. $this->client_id .'.'. $parcel_id ) ) { return Cache::get( 'collivery.parcel_image.'. $this->client_id .'.'. $parcel_id ); } else { try { $result = $this->client()->get_parcel_image( $parcel_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['image'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); else if ( $this->check_cache != 0 ) Cache::put( 'collivery.parcel_image.'. $this->client_id .'.'. $parcel_id, $result['image'], 60*24 ); return $result['image']; } 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 status tracking detail of a given Waybill number. * If the collivery is still active, the estimated time of delivery * will be provided. If delivered, the time and receivers name (if availble) * with returned. * * @param int $collivery_id Collivery ID * @return array Collivery Status Information */ public function getStatus( $collivery_id ) { if ( ( $this->check_cache == 2 ) && Cache::has( 'collivery.status.'. $this->client_id .'.'. $collivery_id ) ) { return Cache::get( 'collivery.status.'. $this->client_id .'.'. $collivery_id ); } else { try { $result = $this->client()->get_collivery_status( $collivery_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['status_id'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); else if ( $this->check_cache != 0 ) Cache::put( 'collivery.status.'. $this->client_id .'.'. $collivery_id, $result, 60*12 ); 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; } } } /** * 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; } }
Output for 5.3.0 - 7.1.0