forked from z38/swiss-payment
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ISRParticipant.php
61 lines (54 loc) · 1.62 KB
/
ISRParticipant.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
<?php
namespace Z38\SwissPayment;
use DOMDocument;
use InvalidArgumentException;
/**
* ISRParticipant holds an ISR participation number
*/
class ISRParticipant implements AccountInterface
{
/**
* @var string
*/
protected $number;
/**
* Constructor
*
* @param string $number
*
* @throws InvalidArgumentException When the participation number is not valid.
*/
public function __construct($number)
{
if (preg_match('/^(0[13])-([0-9]{1,6})-([0-9])$/', $number, $dashMatches)) {
$this->number = sprintf('%s%06s%s', $dashMatches[1], $dashMatches[2], $dashMatches[3]);
} elseif (preg_match('/^0[13][0-9]{7}$/', $number)) {
$this->number = $number;
} else {
throw new InvalidArgumentException('ISR participant number is not properly formatted.');
}
if (!PostalAccount::validateCheckDigit($this->number)) {
throw new InvalidArgumentException('ISR participant number has an invalid check digit.');
}
}
/**
* Format the participant number
*
* @return string The formatted participant number
*/
public function format()
{
return sprintf('%s-%s-%s', substr($this->number, 0, 2), ltrim(substr($this->number, 2, 6), '0'), substr($this->number, 8));
}
/**
* {@inheritdoc}
*/
public function asDom(DOMDocument $doc)
{
$root = $doc->createElement('Id');
$other = $doc->createElement('Othr');
$other->appendChild($doc->createElement('Id', $this->number));
$root->appendChild($other);
return $root;
}
}