1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* ISRParticipant holds an ISR participation number |
10
|
|
|
*/ |
11
|
|
|
class ISRParticipant implements AccountInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $number; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor |
20
|
|
|
* |
21
|
|
|
* @param string $number |
22
|
|
|
* |
23
|
|
|
* @throws \InvalidArgumentException When the participation number is not valid. |
24
|
|
|
*/ |
25
|
10 |
|
public function __construct($number) |
26
|
|
|
{ |
27
|
10 |
|
if (preg_match('/^([0-9]{2})-([0-9]{1,6})-([0-9])$/', $number, $dashMatches)) { |
28
|
5 |
|
$this->number = sprintf('%s%06s%s', $dashMatches[1], $dashMatches[2], $dashMatches[3]); |
29
|
10 |
|
} elseif (preg_match('/^[0-9]{9}$/', $number)) { |
30
|
1 |
|
$this->number = $number; |
31
|
1 |
|
} else { |
32
|
4 |
|
throw new InvalidArgumentException('ISR participant number is not properly formatted.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
6 |
|
if (substr($this->number, 0, 2) !== '01' |
36
|
6 |
|
&& substr($this->number, 0, 2) !== '03') { |
37
|
|
|
throw new InvalidArgumentException('ISR participant number must start by 01 [CHF] or 03 [EURO].'); |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
if (PostalAccount::calculateCheckDigit(substr($this->number, 0, 8)) !== (int) substr($this->number, -1)) { |
41
|
1 |
|
throw new InvalidArgumentException('Postal account number has an invalid check digit.'); |
42
|
|
|
} |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Format the participant number |
47
|
|
|
* |
48
|
|
|
* @return string The formatted participant number |
49
|
|
|
*/ |
50
|
1 |
|
public function format() |
51
|
|
|
{ |
52
|
1 |
|
return sprintf('%s-%s-%s', substr($this->number, 0, 2), ltrim(substr($this->number, 2, 6), '0'), substr($this->number, 8)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
2 |
|
public function asDom(DOMDocument $doc) |
59
|
|
|
{ |
60
|
2 |
|
$root = $doc->createElement('Id'); |
61
|
2 |
|
$other = $doc->createElement('Othr'); |
62
|
2 |
|
$other->appendChild($doc->createElement('Id', $this->number)); |
63
|
2 |
|
$root->appendChild($other); |
64
|
|
|
|
65
|
2 |
|
return $root; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|