1 | <?php |
||
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) |
|
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) |
|
110 | } |