1 | <?php |
||
7 | abstract class Sms |
||
8 | { |
||
9 | protected $text; |
||
10 | protected $username; |
||
11 | protected $password; |
||
12 | protected $recipients = []; |
||
13 | private static $httpClient; |
||
14 | protected $sender; |
||
15 | protected $response; |
||
16 | protected $client; |
||
17 | protected $request; |
||
18 | |||
19 | /** |
||
20 | * @var \Exception |
||
21 | */ |
||
22 | public $httpError; |
||
23 | |||
24 | /**We want HTTP CLient instantiated |
||
25 | * only once in entire app lifecycle |
||
26 | * @return Client |
||
27 | */ |
||
28 | public static function getInstance() |
||
29 | { |
||
30 | if (! self::$httpClient) { |
||
31 | self::$httpClient = new Client(); |
||
32 | } |
||
33 | |||
34 | return self::$httpClient; |
||
35 | } |
||
36 | |||
37 | /** Define SMS recipient(s). |
||
38 | * @param $numbers string|array |
||
39 | * @return $this |
||
40 | */ |
||
41 | public function to($numbers) |
||
42 | { |
||
43 | $numbers = is_array($numbers) ? $numbers : func_get_args(); |
||
44 | |||
45 | if (count($numbers)) { |
||
46 | $this->setRecipients($numbers); |
||
47 | } |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | private function setRecipients($numbers) |
||
53 | { |
||
54 | foreach ($numbers as $number) { |
||
55 | $this->recipients[] = $this->numberFormat($number); |
||
56 | } |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | public function getRecipients(): array |
||
62 | { |
||
63 | return $this->recipients; |
||
64 | } |
||
65 | |||
66 | public function text($text = null) |
||
67 | { |
||
68 | if ($text) { |
||
69 | $this->setText($text); |
||
70 | } |
||
71 | |||
72 | return $this; |
||
73 | } |
||
74 | |||
75 | private function numberFormat($number) |
||
76 | { |
||
77 | $number = (string) $number; |
||
78 | $number = trim($number); |
||
79 | $number = preg_replace("/\s|\+|-/", '', $number); |
||
80 | |||
81 | return $number; |
||
82 | } |
||
83 | |||
84 | public function setText($text) |
||
85 | { |
||
86 | $this->text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', trim($text)); |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | public function getText() |
||
92 | { |
||
93 | return $this->text; |
||
94 | } |
||
95 | |||
96 | public function from($from) |
||
97 | { |
||
98 | $this->sender = $from; |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | public function getSender() |
||
104 | { |
||
105 | return $this->sender; |
||
106 | } |
||
107 | |||
108 | public function getResponse() |
||
112 | |||
113 | public function getException() |
||
117 | |||
118 | /** |
||
119 | * Determines if it has any recipients. |
||
120 | * |
||
121 | * @return bool [description] |
||
122 | */ |
||
123 | public function hasRecipients() |
||
124 | { |
||
125 | return property_exists($this, 'recipients') && ! empty($this->recipients); |
||
126 | } |
||
127 | } |
||
128 |