ISRParticipant::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4
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 11
    public function __construct($number)
26
    {
27 11
        if (preg_match('/^(0[13])-([0-9]{1,6})-([0-9])$/', $number, $dashMatches)) {
28 6
            $this->number = sprintf('%s%06s%s', $dashMatches[1], $dashMatches[2], $dashMatches[3]);
29 11
        } elseif (preg_match('/^0[13][0-9]{7}$/', $number)) {
30 1
            $this->number = $number;
31 1
        } else {
32 4
            throw new InvalidArgumentException('ISR participant number is not properly formatted.');
33
        }
34 7
        if (!PostalAccount::validateCheckDigit($this->number)) {
35 1
            throw new InvalidArgumentException('ISR participant number has an invalid check digit.');
36
        }
37 6
    }
38
39
    /**
40
     * Format the participant number
41
     *
42
     * @return string The formatted participant number
43
     */
44 1
    public function format()
45
    {
46 1
        return sprintf('%s-%s-%s', substr($this->number, 0, 2), ltrim(substr($this->number, 2, 6), '0'), substr($this->number, 8));
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function asDom(DOMDocument $doc)
53
    {
54 2
        $root = $doc->createElement('Id');
55 2
        $other = $doc->createElement('Othr');
56 2
        $other->appendChild($doc->createElement('Id', $this->number));
57 2
        $root->appendChild($other);
58
59 2
        return $root;
60
    }
61
}
62