Created
March 21, 2012 18:33
-
-
Save pawellenart/2150884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Eurotax Valuation Webservice Handler | |
* | |
* This class allows connecting to Eurotax Webservice, fetching dictionary data | |
* and request vehicle valuation. | |
* | |
* Usage example: | |
* <code> | |
* <?php | |
* require 'ETGPLConnect.class.php'; | |
* | |
* $etgpl = new ETGPLConnect(); | |
* | |
* $etgpl->setCredentials('username', 'password', 'signature')->initialize(); | |
* | |
* $result = $etgpl->getVehicleTypes(); // returns array | |
* $result = $etgpl->getMakes(10); // returns array | |
* $valuation = $etgpl->getValuationShort(14627, '2011-08-10', 50000); // returns integer | |
* ?> | |
* </code> | |
* @author Pawel Lenart <pawel.lenart@bielbit.pl> | |
* @copyright Copyright (c) 2011, Pawel Lenart | |
* @version 2.0 | |
* @package eurotax | |
*/ | |
/** | |
* General error constant | |
*/ | |
define('EC_GENERAL_ERROR', 2); | |
/** | |
* Credentials error constant | |
*/ | |
define('EC_CREDENTIALS_ERROR', 4); | |
/** | |
* Connection error constant | |
*/ | |
define('EC_CONNECTION_ERROR', 8); | |
/** | |
* Initialization error constant | |
*/ | |
define('EC_INITIALIZATION_ERROR', 16); | |
/** | |
* Result error constant | |
*/ | |
define('EC_RESULT_ERROR', 32); | |
/** | |
* Extended Exception class | |
* @package eurotax | |
*/ | |
class ETGPLException extends Exception { | |
/** | |
* Create new ETGPLException with $message and $code | |
* @param string $message Error message | |
* @param integer $code Error code | |
*/ | |
public function __construct($message, $code) { | |
parent::__construct($message, $code); | |
} | |
} | |
/** | |
* Class for handling short vehicle valuation through webservice | |
* @package eurotax | |
*/ | |
class ETGPLConnect { | |
/** | |
* WSDL address | |
* @var string | |
*/ | |
private $_soapURL = 'https://net.eurotax.pl/etgplws/ETGPLService.asmx?WSDL'; | |
/** | |
* SOAP client options ('trace' allows request and response debugging) | |
* @var array | |
*/ | |
private $_soapClientOptions = array('trace' => 1); | |
/** | |
* SOAP Version Request (from Eurotax's webservice documentation) | |
* @var string | |
*/ | |
private $_soapVersionRequest = '1.0.0'; | |
/** | |
* Country of data request | |
* @var string | |
*/ | |
private $_soapCountry = 'PL'; | |
/** | |
* Data language | |
* @var string | |
*/ | |
private $_soapLanguage = 'PL'; | |
/** | |
* SOAP namespace | |
* @var string | |
*/ | |
private $_soapNS = 'http://www.eurotax.pl/etgplws/'; | |
/** | |
* SOAP header name for credentials | |
* @var string | |
*/ | |
private $_soapHeaderName = 'ETGPLHeader'; | |
/** | |
* SOAP header resource | |
* @var resource | |
*/ | |
private $_soapHeader; | |
/** | |
* SOAP client resource | |
* @var resource | |
*/ | |
protected $_soapClient; | |
/** | |
* SOAP result resource | |
* @var resource | |
*/ | |
private $_soapResult; | |
/** | |
* Webservice connection indicator | |
* @var bool | |
*/ | |
private $_initialized = false; | |
/** | |
* Username value for credentials | |
* @var string | |
*/ | |
private $_username = NULL; | |
/** | |
* Password value for credentials | |
* @var string | |
*/ | |
private $_password = NULL; | |
/** | |
* Signature value for credentials | |
* @var string | |
*/ | |
private $_signature = NULL; | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Connection initialization | |
* @access public | |
*/ | |
public function initialize() { | |
if (!$this->areCredentialsSet()) { | |
throw new ETGPLException('Credentials are not set', EC_CREDENTIALS_ERROR); | |
} | |
try { | |
$this->_soapClient = new SoapClient($this->_soapURL, $this->_soapClientOptions); | |
$this->setHeader(); | |
$this->_initialized = true; | |
} | |
catch (SoapFault $sf) { | |
throw new ETGPLException('Connection error. SoapClient says: ' . $sf->faultstring, EC_CONNECTION_ERROR); | |
} | |
} | |
/** | |
* Sets the credentials for connection | |
* @access public | |
* @param string $username Username | |
* @param string $password Password | |
* @param string $signature Signature | |
* @return resource | |
*/ | |
public function setCredentials($username, $password, $signature) { | |
$this->_username = $username; | |
$this->_password = $password; | |
$this->_signature = $signature; | |
return $this; | |
} | |
/** | |
* Returns last raw response got from webservice | |
* @access protected | |
* @return string | |
*/ | |
protected function debugResponse() { | |
if (!$this->_initialized) { | |
throw new ETGPLException('Connection to webservice is not initialized', EC_INITIALIZATION_ERROR); | |
} | |
return $this->_soapClient->__getLastResponse(); | |
} | |
/** | |
* Gets vehicle types | |
* @access public | |
* @return array | |
*/ | |
public function getVehicleTypes() { | |
$results = $this->call('GetArts', | |
array( | |
'retXML' => false | |
) | |
); | |
$return_array = array(); | |
foreach ($results as $result) { | |
$return_array[] = array('id' => $result[0], 'name' => $result[1]); | |
} | |
return $return_array; | |
} | |
/** | |
* Gets vehicle makes | |
* @access public | |
* @param integer $veh_type_id Vehicle type identifier | |
* @return array | |
*/ | |
public function getMakes($veh_type_id) { | |
$results = $this->call('GetMarks', | |
array( | |
'retXML' => false, | |
'IdArt' => $veh_type_id | |
) | |
); | |
$return_array = array(); | |
foreach ($results as $result) { | |
$return_array[] = array('id' => $result[1], 'name' => $result[2]); | |
} | |
return $return_array; | |
} | |
/** | |
* Gets models of a particular make | |
* @access public | |
* @param integer $veh_type_id Vehicle type identifier | |
* @param integer $make_id Make identifier | |
* @param string $first_reg_date First registration date (YYYY-MM-DD format) | |
* @return array | |
*/ | |
public function getModels($veh_type_id, $make_id, $first_reg_date) { | |
$return_array = array(); | |
if ($this->checkDate($first_reg_date)) { | |
list($_y, $_m, $_d) = explode('-', $first_reg_date); | |
$results = $this->call('GetModelsFirstReg', | |
array( | |
'retXML' => false, | |
'IdArt' => $veh_type_id, | |
'IdMar' => $make_id, | |
'FirestReg' => array( | |
'_d' => $_d, | |
'_m' => $_m, | |
'_y' => $_y | |
) | |
) | |
); | |
foreach ($results as $result) { | |
$return_array[] = array('id' => $result[2], 'name' => $result[3]); | |
} | |
} | |
return $return_array; | |
} | |
/** | |
* Gets types of a particular model | |
* @access public | |
* @param integer $model_id Model identifier | |
* @param string $first_reg_date First registration date (YYYY-MM-DD format) | |
* @param integer $doors Number of doors | |
* @param string $fuel_code Fuel identifier | |
* @return array | |
*/ | |
public function getTypes($model_id, $first_reg_date, $doors, $fuel_code) { | |
$return_array = array(); | |
if ($this->checkDate($first_reg_date)) { | |
$doors_less_4 = ((int)$doors < 4) ? true : false; | |
list($_y, $_m, $_d) = explode('-', $first_reg_date); | |
$results = $this->call('GetTypesFirstRegEx', | |
array( | |
'retXML' => false, | |
'IdMod' => $model_id, | |
'FirestReg' => array( | |
'_d' => $_d, | |
'_m' => $_m, | |
'_y' => $_y | |
), | |
'DorsLess4' => $doors_less_4, | |
'Fuel' => $fuel_code | |
) | |
); | |
foreach ($results as $result) { | |
$return_array[] = array( | |
'id' => (int)$result[3], | |
'name' => (preg_match('/\d+KM/', $result[6])) ? $result[6] : $result[6] . ' ' . (int)$result[12] . 'KM' | |
); | |
} | |
} | |
return $return_array; | |
} | |
/** | |
* Gets data for a particular vehicle type | |
* @access public | |
* @param integer $type_id Type identifier | |
* @return array | |
*/ | |
public function getTypeData($type_id) { | |
$results = $this->call('GetType', | |
array( | |
'retXML' => false, | |
'EC_Code' => $type_id | |
) | |
); | |
$return_array = array(); | |
foreach ($results as $result) { | |
$body_name = $this->getBodyName($result[9]); | |
$gearbox = ($result[18] == 'M') ? 'Manualna' : 'Automatyczna'; | |
$return_array = array( | |
'capacity' => (int)$result[10], | |
'power_kw' => (int)$result[13], | |
'power_hp' => (int)$result[12], | |
'body' => $body_name, | |
'gearbox' => $gearbox, | |
'seats' => (int)$result[21], | |
'gears' => (int)$result[19], | |
'total_weight' => (int)$result[22] | |
); | |
} | |
return $return_array; | |
} | |
/** | |
* Gets fuel types | |
* @access public | |
* @return array | |
*/ | |
public function getFuelTypes() { | |
$results = $this->call('GetDictionaryFuelAll', array('retXML' => false)); | |
$return_array = array(); | |
foreach ($results as $result) { | |
$return_array[] = array('id' => $result[0], 'name' => $result[1]); | |
} | |
return $return_array; | |
} | |
/** | |
* Requests short valuation of a vehicle | |
* @access public | |
* @param integer $type_id Type identifier | |
* @param string $first_reg_date First registration date (YYYY-MM-DD format) | |
* @param integer $mileage Vehicle's mileage | |
* @return boolean|integer | |
*/ | |
public function getValuationShort($type_id, $first_reg_date, $mileage) { | |
$valuation = false; | |
if ($this->checkDate($first_reg_date)) { | |
$return_array = $this->getValuationShortRaw($type_id, $first_reg_date, $mileage); | |
// if no error | |
if ((int)$return_array[4]['value'] == 0) { | |
$valuation = (int)$return_array[10]['value'] + (int)$return_array[13]['value'] + (int)$return_array[16]['value']; | |
} | |
} | |
return $valuation; | |
} | |
protected function getValuationShortRaw($type_id, $first_reg_date, $mileage) { | |
$return_array = array(); | |
if ($this->checkDate($first_reg_date)) { | |
list($_fy, $_fm, $_fd) = explode('-', $first_reg_date); | |
list($_vy, $_vm, $_vd) = explode('-', date('Y-m-d')); | |
$this->call('getValuationShort', | |
array( | |
'shortin' => array( | |
'_eccode' => $type_id, | |
'_dateval' => array( | |
'_d' => $_vd, | |
'_m' => $_vm, | |
'_y' => $_vy | |
), | |
'_datreg' => array( | |
'_d' => $_fd, | |
'_m' => $_fm, | |
'_y' => $_fy | |
), | |
'_mileage' => $mileage | |
) | |
) | |
); | |
$xml = xml_parser_create(); | |
$xml_string = $this->debugResponse(); | |
xml_parse_into_struct($xml, $xml_string, $return_array); | |
} | |
return $return_array; | |
} | |
/** | |
* Raw webservice function call | |
* @access protected | |
* @param string $function_name Function name | |
* @param array $parameters Array of parameters for a function | |
* @return array | |
*/ | |
protected function call($function_name, array $parameters) { | |
if (!$this->_initialized) { | |
throw new ETGPLException('Connection to webservice is not initialized', EC_INITIALIZATION_ERROR); | |
} | |
try { | |
$this->_soapResult = $this->_soapClient->__soapCall($function_name, array('parameters' => $parameters), null, $this->_soapHeader); | |
return $this->getResults(); | |
} | |
catch (SoapFault $sf) { | |
echo $this->_soapClient->__getLastRequest(); | |
throw new ETGPLException('Connection error. SoapClient says: ' . $sf->faultstring, EC_CONNECTION_ERROR); | |
} | |
} | |
/** | |
* Gets a body name | |
* @access private | |
* @param string $body_code Vehicle's body identifier | |
* @return string | |
*/ | |
private function getBodyName($body_code) { | |
$result = $this->call('GetDictionaryBody', array('retXML' => false, 'BodyCode' => $body_code)); | |
return $result[0][1]; | |
} | |
/** | |
* Gets well-formatted array from a result | |
* @see convertResultToArray() | |
* @access private | |
* @return array | |
*/ | |
private function getResults() { | |
if (!$this->_soapResult) { | |
throw new ETGPLException('No result given by the webservice', EC_RESULT_ERROR); | |
} | |
return $this->convertResultToArray($this->_soapResult); | |
} | |
/** | |
* Sets a proper header for authorization | |
* @access private | |
*/ | |
private function setHeader() { | |
$h = array( | |
'VersionRequest' => $this->_soapVersionRequest, | |
'Country' => $this->_soapCountry, | |
'Language' => $this->_soapLanguage, | |
'User' => array( | |
'name' => $this->_username, | |
'password' => $this->_password, | |
'signature' => $this->_signature, | |
'ex1' => '', | |
'ex2' => '' | |
) | |
); | |
$this->_soapHeader = new SoapHeader($this->_soapNS, $this->_soapHeaderName, $h); | |
} | |
/** | |
* Checks if credentials were set | |
* @see setCredentials() | |
* @access private | |
* @return bool | |
*/ | |
private function areCredentialsSet() { | |
if ($this->_username == NULL && $this->_password == NULL && $this->_signature == NULL) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Checks the date validity | |
* @access protected | |
* @param string $date Date to validate | |
* @return bool | |
*/ | |
protected function checkDate($date) { | |
if (preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $date)) { | |
list($y, $m, $d) = explode('-', $date); | |
return checkdate($m, $d, $y); | |
} | |
return false; | |
} | |
/** | |
* Converts result resource to associative array | |
* @access private | |
* @return array | |
*/ | |
private function convertResultToArray($soapResult) { | |
if (!is_object($soapResult)) { | |
throw new ETGPLException('Passed parameter is not an object', EC_GENERAL_ERROR); | |
} | |
$r = array_keys(get_object_vars($soapResult)); | |
$subobj = $soapResult->$r[0]; | |
$res = array(); | |
if (isset($subobj->_return)) { | |
$split = explode(';', $subobj->_return); | |
for ($i = 0; $i < count($split); $i++) { | |
if ($split[$i] != '') { | |
$values = explode('|', $split[$i]); | |
for ($j = 0; $j < count($values); $j++) { | |
$res[$i][$j] = $values[$j]; | |
} | |
} | |
} | |
} | |
return $res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment