initialize(); } return self::$instance; } /** * Zwraca wyjątek SOAP o ile wystąpił podczas żądania * * @return SoapFault|null */ public function getSoapFault() { return $this->soapFault; } public function checkVat($vat_number) { $info = $this->getVatInfo($vat_number); return $info !== false && $info->isValid(); } public function getVatInfo($vat_number) { if (!$this->client) { return false; } $this->soapFault = null; list($cc, $vn) = self::parseVatNumber($vat_number); try { $result = $this->client->checkVat(array('countryCode' => $cc, 'vatNumber' => $vn)); $response = new stTaxViesResponse($result); } catch(SoapFault $e) { return $this->handleException($e); } if ($this->logger) { $this->logMessage($response); } return $response; } protected function handleException(SoapFault $e) { if ($this->logger) { $this->logMessage($e->getMessage(), SF_LOG_ERR); } $this->soapFault = $e; return false; } protected function initialize() { if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) { $this->logger = sfLogger::getInstance(); } try { $this->client = new SoapClient(dirname(__FILE__).'/../data/checkVatService.wsdl'); } catch(SoapFault $e) { $this->handleException($e); } return $this; } protected function logMessage($message, $level = SF_LOG_INFO) { $this->logger->log('{stTaxVies} ' . $message, $level); } } class stTaxViesResponse { protected $result; public function __toString() { return json_encode($this->result); } public function __construct(stdClass $result) { $this->result = $result; } public function getCountryCode() { return $this->result->countryCode; } public function getVatNumber() { return $this->result->vatNumber; } public function isValid() { return $this->result->valid; } public function getName() { return $this->result->name; } public function getAddress() { return $this->result->address; } }