Completed
Push — master ( 0a5beb...6bf385 )
by z38
11:34
created

ISRParticipant::asDom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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