1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erdemkeren\JetSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ShortMessage. |
7
|
|
|
*/ |
8
|
|
|
class ShortMessage |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The short message body. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $body; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The receivers. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $receivers; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ShortMessage constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $body |
28
|
|
|
* @param string|array $receivers |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct($receivers, $body) |
31
|
|
|
{ |
32
|
6 |
|
$this->body = $body; |
33
|
6 |
|
$this->receivers = is_array($receivers) |
34
|
6 |
|
? array_map('trim', $receivers) |
35
|
6 |
|
: [trim($receivers)]; |
36
|
6 |
|
} |
37
|
|
|
|
38
|
6 |
|
public function body() |
39
|
|
|
{ |
40
|
6 |
|
return $this->body; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function hasManyReceivers() |
44
|
|
|
{ |
45
|
1 |
|
return count($this->receivers()) > 1; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function receivers() |
49
|
|
|
{ |
50
|
2 |
|
return $this->receivers; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
public function receiversString($glue = null) |
54
|
|
|
{ |
55
|
6 |
|
return implode($glue, $this->receivers); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function toArray() |
59
|
|
|
{ |
60
|
1 |
|
return array_filter([ |
61
|
1 |
|
'Msisdns' => $this->receiversString('|'), |
62
|
1 |
|
'Messages' => $this->body(), |
63
|
1 |
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
View Code Duplication |
public function toSingleMessageXml() |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
$text = str_replace("'", "'", htmlentities($this->body())); |
69
|
1 |
|
$gsmNo = $this->receiversString(','); |
70
|
|
|
|
71
|
1 |
|
return "<text>{$text}</text><message><gsmnos>{$gsmNo}</gsmnos></message>"; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the xml representation of the short message. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
public function toMultipleMessagesXml() |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
$text = str_replace("'", "'", htmlentities($this->body())); |
82
|
1 |
|
$gsmNo = $this->receiversString(','); |
83
|
|
|
|
84
|
1 |
|
return "<message><gsmno>{$gsmNo}</gsmno><text>{$text}</text></message>"; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.