forked from z38/swiss-payment
-
Notifications
You must be signed in to change notification settings - Fork 6
/
IID.php
76 lines (66 loc) · 1.91 KB
/
IID.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
<?php
namespace Z38\SwissPayment;
use DOMDocument;
use InvalidArgumentException;
/**
* IID holds a Swiss institutional identification number (formerly known as BC number)
*/
class IID implements FinancialInstitutionInterface
{
/**
* @var string
*/
protected $iid;
/**
* Constructor
*
* @param string $iid
*
* @throws InvalidArgumentException When the IID does contain invalid characters or the length does not match.
*/
public function __construct($iid)
{
$iid = (string) $iid;
if (!preg_match('/^[0-9]{3,5}$/', $iid)) {
throw new InvalidArgumentException('IID is not properly formatted.');
}
$this->iid = str_pad($iid, 5, '0', STR_PAD_LEFT);
}
/**
* Extracts the IID from an IBAN
*
* @param IBAN $iban
*
* @return IID
*/
public static function fromIBAN(IBAN $iban)
{
if (!in_array($iban->getCountry(), ['CH', 'LI'])) {
throw new InvalidArgumentException('IID can only be extracted from Swiss and Lichtenstein IBANs.');
}
return new self(substr($iban->normalize(), 4, 5));
}
/**
* Returns a formatted representation of the IID
*
* @return string The formatted IID
*/
public function format()
{
return $this->iid;
}
/**
* {@inheritdoc}
*/
public function asDom(DOMDocument $doc)
{
$xml = $doc->createElement('FinInstnId');
$clearingSystem = $doc->createElement('ClrSysMmbId');
$clearingSystemId = $doc->createElement('ClrSysId');
$clearingSystemId->appendChild($doc->createElement('Cd', 'CHBCC'));
$clearingSystem->appendChild($clearingSystemId);
$clearingSystem->appendChild($doc->createElement('MmbId', ltrim($this->iid, '0'))); // strip zeroes for legacy systems
$xml->appendChild($clearingSystem);
return $xml;
}
}