1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Giansalex |
5
|
|
|
* Date: 22/07/2017 |
6
|
|
|
* Time: 15:40 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenter\Ws\Reader; |
10
|
|
|
|
11
|
|
|
use Greenter\Model\Response\CdrResponse; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DomCdrReader |
15
|
|
|
* @package Greenter\Ws\Reader |
16
|
|
|
*/ |
17
|
|
|
class DomCdrReader implements CdrReader |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Get Cdr using DomDocument. |
21
|
|
|
* |
22
|
|
|
* @param string $xml |
23
|
|
|
* @return CdrResponse |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
24 |
|
public function getCdrResponse($xml) |
27
|
|
|
{ |
28
|
24 |
|
$xpt = $this->getXpath($xml); |
29
|
|
|
|
30
|
24 |
|
$cdr = $this->getResponseByXpath($xpt); |
31
|
24 |
|
if (!$cdr) { |
32
|
2 |
|
throw new \Exception('Not found cdr response in xml'); |
33
|
|
|
} |
34
|
22 |
|
$cdr->setNotes($this->getNotes($xpt)); |
35
|
|
|
|
36
|
22 |
|
return $cdr; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get Xpath from xml content. |
41
|
|
|
* |
42
|
|
|
* @param string $xmlContent |
43
|
|
|
* @return \DOMXPath |
44
|
|
|
*/ |
45
|
24 |
|
private function getXpath($xmlContent) |
46
|
|
|
{ |
47
|
24 |
|
$doc = new \DOMDocument(); |
48
|
24 |
|
$doc->loadXML($xmlContent); |
49
|
24 |
|
$xpt = new \DOMXPath($doc); |
50
|
24 |
|
$xpt->registerNamespace('x', $doc->documentElement->namespaceURI); |
51
|
24 |
|
return $xpt; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \DOMXPath $xpath |
56
|
|
|
* @return CdrResponse |
57
|
|
|
*/ |
58
|
24 |
|
private function getResponseByXpath(\DOMXPath $xpath) |
59
|
|
|
{ |
60
|
24 |
|
$resp = $xpath->query('/x:ApplicationResponse/cac:DocumentResponse/cac:Response'); |
61
|
|
|
|
62
|
24 |
|
if ($resp->length !== 1) { |
63
|
2 |
|
return null; |
64
|
|
|
} |
65
|
22 |
|
$obj = $resp[0]; |
66
|
|
|
|
67
|
22 |
|
$cdr = new CdrResponse(); |
68
|
22 |
|
$cdr->setId($this->getValueByName($obj,'ReferenceID')) |
69
|
22 |
|
->setCode($this->getValueByName($obj,'ResponseCode')) |
70
|
22 |
|
->setDescription($this->getValueByName($obj,'Description')); |
71
|
|
|
|
72
|
22 |
|
return $cdr; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \DOMElement $node |
78
|
|
|
* @param string $name |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
22 |
|
private function getValueByName(\DOMElement $node, $name) |
82
|
|
|
{ |
83
|
22 |
|
$values = $node->getElementsByTagName($name); |
84
|
22 |
|
if ($values->length !== 1) { |
85
|
2 |
|
return ''; |
86
|
|
|
} |
87
|
22 |
|
return $values[0]->nodeValue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \DOMXPath $xpath |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
22 |
|
private function getNotes(\DOMXPath $xpath) |
95
|
|
|
{ |
96
|
22 |
|
$nodes = $xpath->query('/x:ApplicationResponse/cbc:Note'); |
97
|
22 |
|
$notes = []; |
98
|
22 |
|
if ($nodes->length === 0) { |
99
|
18 |
|
return $notes; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/**@var \DOMElement $node*/ |
103
|
4 |
|
foreach ($nodes as $node) |
104
|
|
|
{ |
105
|
4 |
|
$notes[] = $node->nodeValue; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
4 |
|
return $notes; |
109
|
|
|
} |
110
|
|
|
} |