Completed
Push — scrutinizer ( 42ac6a...7bc18e )
by z38
02:40
created

AbstractMessage::buildDom()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Z38\SwissPayment\Message;
4
5
/**
6
 * AbstractMessages eases message creation using DOM
7
 */
8
abstract class AbstractMessage implements MessageInterface
9
{
10
    const SCHEMA_LOCATION = 'http://www.six-interbank-clearing.com/de/%s';
11
12
    /**
13
     * Builds the DOM of the actual message
14
     *
15
     * @param \DOMDocument $doc
16
     *
17
     * @return \DOMElement
18
     */
19
    abstract protected function buildDom(\DOMDocument $doc);
20
21
    /**
22
     * Gets the name of the schema
23
     *
24
     * @return string
25
     */
26
    abstract protected function getSchemaName();
27
28
    /**
29
     * Builds a DOM document of the message
30
     *
31
     * @return \DOMDocument
32
     */
33 2
    public function asDom()
34
    {
35 2
        $schema = $this->getSchemaName();
36 2
        $ns = sprintf(self::SCHEMA_LOCATION, $schema);
37
38 2
        $doc = new \DOMDocument('1.0', 'UTF-8');
39 2
        $doc->formatOutput = true;
40 2
        $root = $doc->createElement('Document');
41 2
        $root->setAttribute('xmlns', $ns);
42 2
        $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
43 2
        $root->setAttribute('xsi:schemaLocation', sprintf('%s %s', $ns, $schema));
44 2
        $root->appendChild($this->buildDom($doc));
45 2
        $doc->appendChild($root);
46
47 2
        return $doc;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function asXml()
54
    {
55 2
        return $this->asDom()->saveXML();
56
    }
57
58
    /**
59
     * Returns the name of the software used to create the message
60
     *
61
     * @return string
62
     */
63 2
    public function getSoftwareName()
64
    {
65 2
        return 'Z38_SwissPayment';
66
    }
67
68
    /**
69
     * Returns the version of the software used to create the message
70
     *
71
     * @return string
72
     */
73 2
    public function getSoftwareVersion()
74
    {
75 2
        return 'dev-master';
76
    }
77
78
    /**
79
     * Creates a DOM element which contains details about the software used to create the message
80
     *
81
     * @param \DOMDocument $doc
82
     *
83
     * @return \DOMElement
84
     */
85 2
    protected function buildContactDetails(\DOMDocument $doc)
86
    {
87 2
        $root = $doc->createElement('CtctDtls');
88
89 2
        $root->appendChild($doc->createElement('Nm', $this->getSoftwareName()));
90 2
        $root->appendChild($doc->createElement('Othr', $this->getSoftwareVersion()));
91
92 2
        return $root;
93
    }
94
}
95