CategoryPurposeCode::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 10
    public function __construct($code)
26
    {
27 10
        $code = (string) $code;
28 10
        if (!preg_match('/^[A-Z]{4}$/', $code)) {
29 5
            throw new InvalidArgumentException('The category purpose code is not valid.');
30
        }
31
32 5
        $this->code = $code;
33 5
    }
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