forked from herdani/vat-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vatValidation.class.php
98 lines (81 loc) · 2.33 KB
/
vatValidation.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
class vatValidation
{
const WSDL = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl";
private $_client = null;
private $options = array(
'debug' => false,
);
private $_valid = false;
private $_data = array();
public function __construct($options = array()) {
foreach($options as $option => $value) {
$this->_options[$option] = $value;
}
if(!class_exists('SoapClient')) {
throw new Exception('The Soap library has to be installed and enabled');
}
try {
$this->_client = new SoapClient(self::WSDL, array('trace' => true) );
} catch(Exception $e) {
$this->trace('Vat Translation Error', $e->getMessage());
}
}
public function check($countryCode, $vatNumber) {
$rs = $this->_client->checkVat( array('countryCode' => $countryCode, 'vatNumber' => $vatNumber) );
if($this->isDebug()) {
$this->trace('Web Service result', $this->_client->__getLastResponse());
}
if($rs->valid) {
$this->_valid = true;
list($denomination,$name) = explode(" " ,$rs->name,2);
$this->_data = array(
'denomination' => $denomination,
'name' => $this->cleanUpString($name),
'address' => $this->cleanUpString($rs->address),
);
return true;
} else {
$this->_valid = false;
$this->_data = array();
return false;
}
}
public function isValid() {
return $this->_valid;
}
public function getDenomination() {
return $this->_data['denomination'];
}
public function getName() {
return $this->_data['name'];
}
public function getAddress() {
return $this->_data['address'];
}
public function isDebug() {
return ($this->_options['debug'] === true);
}
private function trace($title,$body) {
echo '<h2>TRACE: '.$title.'</h2><pre>'. htmlentities($body).'</pre>';
}
private function cleanUpString($string) {
for($i=0;$i<100;$i++)
{
$newString = str_replace(" "," ",$string);
if($newString === $string) {
break;
} else {
$string = $newString;
}
}
$newString = "";
$words = explode(" ",$string);
foreach($words as $k=>$w)
{
$newString .= ucfirst(strtolower($w))." ";
}
return $newString;
}
}
?>