Completed
Push — feature-purposes ( ae1ada )
by z38
02:44
created

CategoryPurposeCode::asDom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Z38\SwissPayment\PaymentInformation;
4
5
use DOMDocument;
6
use InvalidArgumentException;
7
8
/**
9
 * CategoryPurposeCode contains a category purpose code from the External Code Sets
10
 */
11
class CategoryPurposeCode
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $code;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param string $code
22
     *
23
     * @throws InvalidArgumentException When the code is not valid
24
     */
25 9
    public function __construct($code)
26
    {
27 9
        $code = (string) $code;
28 9
        if (!preg_match('/^[A-Z]{4}$/', $code)) {
29 5
            throw new InvalidArgumentException('The category purpose code is not valid.');
30
        }
31
32 4
        $this->code = $code;
33 4
    }
34
35
    /**
36
     * Returns a XML representation of this purpose
37
     *
38
     * @param \DOMDocument $doc
39
     *
40
     * @return \DOMElement The built DOM element
41
     */
42 3
    public function asDom(DOMDocument $doc)
43
    {
44 3
        return $doc->createElement('Cd', $this->code);
45
    }
46
}
47