1
|
|
|
<?php |
2
|
|
|
namespace Genkgo\Camt; |
3
|
|
|
|
4
|
|
|
use DateTimeImmutable; |
5
|
|
|
use DOMDocument; |
6
|
|
|
use Genkgo\Camt\DecoderInterface; |
7
|
|
|
use Genkgo\Camt\Exception\InvalidMessageException; |
8
|
|
|
use Genkgo\Camt\Util\StringToUnits; |
9
|
|
|
use Money\Currency; |
10
|
|
|
use Money\Money; |
11
|
|
|
use SimpleXMLElement; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Decoder |
15
|
|
|
* @package Genkgo\Camt |
16
|
|
|
*/ |
17
|
|
|
class Decoder implements DecoderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var SimpleXMLElement[] |
21
|
|
|
*/ |
22
|
|
|
private $document; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Decoder\Message |
26
|
|
|
*/ |
27
|
|
|
private $messageDecoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Path to the schema definition |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $schemeDefinitionPath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Decoder\Message $messageDecoder |
37
|
|
|
* @param string $schemeDefinitionPath |
38
|
|
|
*/ |
39
|
14 |
|
public function __construct(Decoder\Message $messageDecoder, $schemeDefinitionPath) |
40
|
13 |
|
{ |
41
|
14 |
|
$this->messageDecoder = $messageDecoder; |
42
|
14 |
|
$this->schemeDefinitionPath = $schemeDefinitionPath; |
43
|
14 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param DOMDocument $document |
47
|
|
|
* @throws InvalidMessageException |
48
|
|
|
*/ |
49
|
14 |
|
private function validate(DOMDocument $document) |
50
|
|
|
{ |
51
|
14 |
|
libxml_use_internal_errors(true); |
52
|
14 |
|
$valid = $document->schemaValidate(dirname(__DIR__).$this->schemeDefinitionPath); |
53
|
14 |
|
$errors = libxml_get_errors(); |
54
|
14 |
|
libxml_clear_errors(); |
55
|
|
|
|
56
|
14 |
|
if (!$valid) { |
57
|
1 |
|
$messages = []; |
58
|
1 |
|
foreach ($errors as $error) { |
59
|
1 |
|
$messages[] = $error->message; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
$errorMessage = implode("\n", $messages); |
63
|
1 |
|
throw new InvalidMessageException("Provided XML is not valid according to the XSD:\n{$errorMessage}"); |
64
|
|
|
} |
65
|
13 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param DOMDocument $document |
69
|
|
|
* @return Message |
70
|
|
|
* @throws InvalidMessageException |
71
|
|
|
*/ |
72
|
14 |
|
public function decode(DOMDocument $document) |
73
|
|
|
{ |
74
|
14 |
|
$this->validate($document); |
75
|
13 |
|
$this->document = simplexml_import_dom($document); |
|
|
|
|
76
|
|
|
|
77
|
13 |
|
$message = new DTO\Message(); |
78
|
13 |
|
$this->messageDecoder->addGroupHeader($message, $this->document); |
79
|
13 |
|
$this->messageDecoder->addRecords($message, $this->document); |
80
|
|
|
|
81
|
13 |
|
return $message; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..