-
Notifications
You must be signed in to change notification settings - Fork 28
/
class.SEPAMessage.php
165 lines (141 loc) · 4.27 KB
/
class.SEPAMessage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* This class represents the overall SEPA message and is used for XML generation and validation.
*
* @author Johannes Feichtner <[email protected]>
* @copyright http://www.web-wack.at web wack creations
* @license http://creativecommons.org/licenses/by-nc/3.0/ CC Attribution-NonCommercial 3.0 license
* For commercial use please contact [email protected]
*/
class SEPAMessage
{
/**
* The used ISO 20022 message format.
*
* @var string
*/
private $messageDefintion = '';
/**
* GroupHeader object. (Tag: <GrpHdr>)
*
* @var SEPAGroupHeader object
*/
private $groupHeader = null;
/**
* A container for all PaymentInfo objects.
*
* @var SEPAPaymentInfo[] array
*/
private $paymentInfos = array();
/**
* The constructor sets the used message format.
*
* @param string $messageDefinition
*/
public function __construct($messageDefinition)
{
$this->messageDefintion = $messageDefinition;
}
/**
* Getter for the group header object.
*
* @return SEPAGroupHeader object
*/
public function getGroupHeader()
{
if (is_null($this->groupHeader))
$this->groupHeader = new SEPAGroupHeader();
return $this->groupHeader;
}
/**
* Setter for the group header object.
*
* @param SEPAGroupHeader $groupHeader
*/
public function setGroupHeader(SEPAGroupHeader $groupHeader)
{
$this->groupHeader = $groupHeader;
}
/**
* Adds a PaymentInfo object to the collection.
*
* @param SEPAPaymentInfo $paymentInfo
*/
public function addPaymentInfo(SEPAPaymentInfo $paymentInfo)
{
$this->paymentInfos[] = $paymentInfo;
$nbOfTxs = $this->getGroupHeader()->getNumberOfTransactions() + $paymentInfo->getNumberOfTransactions();
$ctrlSum = $this->getGroupHeader()->getControlSum() + $paymentInfo->getControlSum();
$this->getGroupHeader()->setNumberOfTransactions($nbOfTxs);
$this->getGroupHeader()->setControlSum($ctrlSum);
}
/**
* Returns the message as SimpleXMLElement object.
*
* @return SimpleXMLElement object
*/
public function getXML()
{
// Initialize the actual message and add the group header
$message = new SimpleXMLElement("<CstmrDrctDbtInitn></CstmrDrctDbtInitn>");
$this->addSubtree($message, $this->getGroupHeader()->getXmlGroupHeader());
// Add all payment blocks
for ($i = 0; $i < count($this->paymentInfos); $i++)
$this->addSubtree($message, $this->paymentInfos[$i]->getXmlPaymentInfo());
$message->GrpHdr->NbOfTxs = $this->getGroupHeader()->getNumberOfTransactions();
$message->GrpHdr->CtrlSum = $this->getGroupHeader()->getControlSum();
// Finally add the XML structure
$doc = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="' . $this->messageDefintion . '" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</Document>');
$this->addSubtree($doc, $message);
return $doc;
}
/**
* Returns a printable and formatted XML message.
*
* @return string
*/
public function printXML()
{
@header('Content-type: text/xml');
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($this->getXML()->asXML());
return $dom->saveXML();
}
/**
* Validate the generated XML over the .xsd specification.
*
* @param string $schemePath
* @return bool
*/
public function validateXML($schemePath)
{
$dom = new DOMDocument();
$dom->loadXML($this->getXML()->asXML());
libxml_use_internal_errors(true);
if (!$dom->schemaValidate($schemePath))
{
echo "XML validation failed!\n";
$errors = libxml_get_errors();
for ($i = 0; $i < count($errors); $i++)
echo "Error " . $errors[$i]->code . ": " . $errors[$i]->message . "\n";
return false;
}
return true;
}
/**
* Creates new DOM elements from two given SimpleXMLElement objects.
*
* @param SimpleXMLElement $xmlTarget
* @param SimpleXMLElement $xmlOrigin
*/
private function addSubtree(SimpleXMLElement $xmlTarget, SimpleXMLElement $xmlOrigin)
{
$domTarget = dom_import_simplexml($xmlTarget);
$domOrigin = dom_import_simplexml($xmlOrigin);
$domTarget->appendChild($domTarget->ownerDocument->importNode($domOrigin, true));
}
}