Skip to content

Instantly share code, notes, and snippets.

@rufinus
Created February 22, 2013 16:39
Show Gist options
  • Save rufinus/5014752 to your computer and use it in GitHub Desktop.
Save rufinus/5014752 to your computer and use it in GitHub Desktop.
ZF2 UID Validation
<?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