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 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; } } } /** * Create a new Address and Contact * * @param array $data Address and Contact Information * @return array Address ID and Contact ID */ public function addAddress( array $data ) { if ( ! isset( $data['location_type'] ) ) $this->setError( 'missing_data', 'location_type not set.' ); elseif ( ! isset( $this->getLocationTypes()[ $data['location_type'] ] ) ) $this->setError( 'invalid_data', 'Invalid location_type.' ); if ( ! isset( $data['town_id'] ) ) $this->setError( 'missing_data', 'town_id not set.' ); elseif ( ! isset( $this->getTowns()[ $data['town_id'] ] ) ) $this->setError( 'invalid_data', 'Invalid town_id.' ); if ( ! isset( $data['suburb_id'] ) ) $this->setError( 'missing_data', 'suburb_id not set.' ); elseif ( ! isset( $this->getSuburbs( $data['town_id'] )[ $data['suburb_id'] ] ) ) $this->setError( 'invalid_data', 'Invalid suburb_id.' ); if ( ! isset( $data['street'] ) ) $this->setError( 'missing_data', 'street not set.' ); if ( ! isset( $data['full_name'] ) ) $this->setError( 'missing_data', 'full_name not set.' ); if ( isset( $data['phone'] ) || isset( $data['cellphone'] ) ) $this->setError( 'missing_data', 'Please supply ether a phone or cellphone number...' ); if ( ! $this->hasErrors() ) { try { $result = $this->client()->add_address( $data, $this->token ); Cache::forget( 'collivery.addresses.'. $this->client_id ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['address_id'] ) ) { 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; } } } /** * Add's a contact person for a given Address ID * * @param array $data New Contact Data * @return int New Contact ID */ public function addContact( array $data ) { if ( ! isset( $data['address_id'] ) ) $this->setError( 'missing_data', 'address_id not set.' ); elseif ( ! is_array( $this->getAddress( $data['address_id'] ) ) ) $this->setError( 'invalid_data', 'Invalid address_id.' ); if ( ! isset( $data['street'] ) ) $this->setError( 'missing_data', 'street not set.' ); if ( ! isset( $data['full_name'] ) ) $this->setError( 'missing_data', 'full_name not set.' ); if ( isset( $data['phone'] ) || isset( $data['cellphone'] ) ) $this->setError( 'missing_data', 'Please supply ether a phone or cellphone number...' ); if ( ! $this->hasErrors() ) { try { $result = $this->client()->add_address( $data, $this->token ); Cache::forget( 'collivery.addresses.'. $this->client_id ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['address_id'] ) ) { 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 price based on the data provided. * * @param array $data Your Collivery Details * @return array Pricing for details supplied */ public function getPrice( array $data ) { if ( ! isset( $data['collivery_from'] ) && ! isset( $data['from_town_id'] ) ) $this->setError( 'missing_data', 'collivery_from/from_town_id not set.' ); elseif ( isset( $data['collivery_from'] ) && ! is_array( $this->getAddress( $data['collivery_from'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' ); elseif ( isset( $data['from_town_id'] ) && ! isset( $this->getTowns()[ $data['from_town_id'] ] ) ) $this->setError( 'invalid_data', 'Invalid Town ID for: from_town_id.' ); if ( ! isset( $data['collivery_to'] ) && ! isset( $data['to_town_id'] ) ) $this->setError( 'missing_data', 'collivery_to/to_town_id not set.' ); elseif ( isset( $data['collivery_to'] ) && ! is_array( $this->getAddress( $data['collivery_to'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' ); elseif ( isset( $data['to_town_id'] ) && ! isset( $this->getTowns()[ $data['to_town_id'] ] ) ) $this->setError( 'invalid_data', 'Invalid Town ID for: to_town_id.' ); if ( ! isset( $data['service'] ) ) $this->setError( 'missing_data', 'service not set.' ); if ( ! $this->hasErrors() ) { try { $result = $this->client()->get_price( $data, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( is_array( $result ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); 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; } } } /** * Validate Collivery * * Returns the validated data array of all details pertaining to a collivery. * This process validates the information based on services, time frames and parcel information. * Dates and times may be altered during this process based on the collection and delivery towns service parameters. * Certain towns are only serviced on specific days and between certain times. * This function automatically alters the values. * The parcels volumetric calculations are also done at this time. * It is important that the data is first validated before a collivery can be added. * * @param array $data Properties of the new Collivery * @return array The validated data */ public function validate( array $data ) { if ( ! isset( $data['collivery_from'] ) ) $this->setError( 'missing_data', 'collivery_from not set.' ); elseif ( ! is_array( $this->getAddress( $data['collivery_from'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' ); if ( ! isset( $data['contact_from'] ) ) $this->setError( 'missing_data', 'contact_from not set.' ); elseif ( ! isset( $this->getContacts( $data['collivery_from'] )[ $data['contact_from'] ] ) ) $this->setError( 'invalid_data', 'Invalid Contact ID for: contact_from.' ); if ( ! isset( $data['collivery_to'] ) ) $this->setError( 'missing_data', 'collivery_to not set.' ); elseif ( ! is_array( $this->getAddress( $data['collivery_to'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' ); if ( ! isset( $data['contact_to'] ) ) $this->setError( 'missing_data', 'contact_to not set.' ); elseif ( ! isset( $this->getContacts( $data['collivery_from'] )[ $data['contact_to'] ] ) ) $this->setError( 'invalid_data', 'Invalid Contact ID for: contact_to.' ); if ( ! isset( $data['collivery_type'] ) ) $this->setError( 'missing_data', 'collivery_type not set.' ); elseif ( ! isset( $this->getParcelTypes()[ $data['collivery_type'] ] ) ) $this->setError( 'invalid_data', 'Invalid collivery_type.' ); if ( ! isset( $data['service'] ) ) $this->setError( 'missing_data', 'service not set.' ); elseif ( ! isset( $this->getServices()[ $data['service'] ] ) ) $this->setError( 'invalid_data', 'Invalid service.' ); if ( ! $this->hasErrors() ) { try { $result = $this->client()->validate_collivery( $data, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( is_array( $result ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); 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; } } } /** * Creates a new Collivery based on the data array provided. * The array should first be validated before passing to this function. * The Waybill No is return apon successful creation of the collivery. * * @param array $data Properties of the new Collivery * @return int New Collivery ID */ public function addCollivery( array $data ) { if ( ! isset( $data['collivery_from'] ) ) $this->setError( 'missing_data', 'collivery_from not set.' ); elseif ( ! is_array( $this->getAddress( $data['collivery_from'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' ); if ( ! isset( $data['contact_from'] ) ) $this->setError( 'missing_data', 'contact_from not set.' ); elseif ( ! isset( $this->getContacts( $data['collivery_from'] )[ $data['contact_from'] ] ) ) $this->setError( 'invalid_data', 'Invalid Contact ID for: contact_from.' ); if ( ! isset( $data['collivery_to'] ) ) $this->setError( 'missing_data', 'collivery_to not set.' ); elseif ( ! is_array( $this->getAddress( $data['collivery_to'] ) ) ) $this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' ); if ( ! isset( $data['contact_to'] ) ) $this->setError( 'missing_data', 'contact_to not set.' ); elseif ( ! isset( $this->getContacts( $data['collivery_from'] )[ $data['contact_to'] ] ) ) $this->setError( 'invalid_data', 'Invalid Contact ID for: contact_to.' ); if ( ! isset( $data['collivery_type'] ) ) $this->setError( 'missing_data', 'collivery_type not set.' ); elseif ( ! isset( $this->getParcelTypes()[ $data['collivery_type'] ] ) ) $this->setError( 'invalid_data', 'Invalid collivery_type.' ); if ( ! isset( $data['service'] ) ) $this->setError( 'missing_data', 'service not set.' ); elseif ( ! isset( $this->getServices()[ $data['service'] ] ) ) $this->setError( 'invalid_data', 'Invalid service.' ); if ( ! $this->hasErrors() ) { try { $result = $this->client()->add_collivery( $data, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['collivery_id'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); return $result['collivery_id']; } else { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); else $this->setError( 'result_unexpected', 'No address_id returned.' ); return false; } } } /** * Accepts the newly created Collivery, moving it from Waiting Client Acceptance * to Accepted so that it can be processed. * * @param int $collivery_id ID of the Collivery you wish to accept * @return boolean Has the Collivery been accepted */ public function acceptCollivery( $collivery_id ) { try { $result = $this->client()->accept_collivery( $collivery_id, $this->token ); } catch (SoapFault $e) { $this->catchSoapFault( $e ); return false; } if ( isset( $result['result'] ) ) { if ( isset( $result['error_id'] ) ) $this->setError( $result['error_id'], $result['error'] ); return $result['result'] == 'Accepted'; } 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; } }

This is an error 404

There are `0` results


preferences:
1557.29 ms | 1398 KiB | 21 Q