Created
February 22, 2013 16:39
-
-
Save rufinus/5014752 to your computer and use it in GitHub Desktop.
ZF2 UID Validation
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 | |
namespace cwdCommon\Validator; | |
use Zend\Validator\AbstractValidator; | |
class UID extends AbstractValidator | |
{ | |
const INVALID = 'invalid'; | |
const OTHER = 'other'; | |
protected $messageTemplates = array( | |
self::INVALID => "'%value%' is not a valid UID in the European Union", | |
self::OTHER => 'checkVatService is currently not reachable' | |
); | |
public function isValid($value) | |
{ | |
$this->setValue($value); | |
$cc = substr($value, 0, 2); | |
$vn = substr($value, 2); | |
try{ | |
//$client = new \SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"); | |
$client = new \SoapClient("data/checkVatService.wsdl"); | |
if($client) { | |
$params = array('countryCode' => $cc, 'vatNumber' => $vn); | |
$result = $client->checkVat($params); | |
if($result->valid) { | |
return true; | |
}else{ | |
$this->error(self::INVALID); | |
return false; | |
} | |
} | |
}catch(\SoapFault $e) { | |
$this->error(self::OTHER); | |
//echo $e->faultstring; | |
}catch(\Exception $e) { | |
$this->error(self::OTHER); | |
//echo $e->getMessage(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment