1
|
|
|
<?php |
2
|
|
|
namespace DexBarrett\ClockworkSms\Builders\Xml; |
3
|
|
|
|
4
|
|
|
use SimpleXMLElement; |
5
|
|
|
|
6
|
|
|
class MessageResponse extends XmlResponse |
7
|
|
|
{ |
8
|
|
|
protected $ignoredMessageKeys = ['errno', 'errdesc', 'messageid']; |
9
|
|
|
protected $smsKeys = ['To' => 'to']; |
10
|
|
|
|
11
|
|
|
public function parseResponse($xmlData) |
12
|
|
|
{ |
13
|
|
|
$response = []; |
14
|
|
|
|
15
|
|
|
$messages = $xmlData->xpath('//SMS_Resp'); |
16
|
|
|
|
17
|
|
|
foreach ($messages as $message) { |
18
|
|
|
$messageData = []; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
$errorNumber = $message->xpath('ErrNo'); |
22
|
|
|
$errorDesc = $message->xpath('ErrDesc'); |
23
|
|
|
$messageID = $message->xpath('MessageID'); |
24
|
|
|
|
25
|
|
|
$messageData['success'] = (count($errorNumber) == 0); |
26
|
|
|
|
27
|
|
|
if (count($errorNumber) > 0) { |
28
|
|
|
$messageData['error_code'] = (string)$errorNumber[0]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (count($errorDesc) > 0) { |
32
|
|
|
$messageData['error_message'] = (string)$errorDesc[0]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (count($messageID) > 0) { |
36
|
|
|
$messageData['id'] = (string)$messageID[0]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
$smsData = array_filter( |
41
|
|
|
(array)$message, |
42
|
|
|
[$this, 'isNotInIgnoredKeys'], |
43
|
|
|
ARRAY_FILTER_USE_BOTH |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$messageData['sms'] = $this->replaceKeys($smsData); |
47
|
|
|
|
48
|
|
|
$response[] = $messageData; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function isNotInIgnoredKeys($nodeValue, $nodeName) |
55
|
|
|
{ |
56
|
|
|
return ! in_array(strtolower($nodeName), $this->ignoredMessageKeys); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function replaceKeys($smsData) |
60
|
|
|
{ |
61
|
|
|
$newSmsData = []; |
62
|
|
|
|
63
|
|
|
foreach ($smsData as $key => $value) { |
64
|
|
|
$newSmsData[$this->getSmsKey($key)] = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $newSmsData; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getSmsKey($key) |
71
|
|
|
{ |
72
|
|
|
if (array_key_exists($key, $this->smsKeys)) { |
73
|
|
|
return $this->smsKeys[$key]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $key; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|