1
|
|
|
<?php |
2
|
|
|
namespace Sender\Otp; |
3
|
|
|
|
4
|
|
|
use Sender\Deliver; |
5
|
|
|
use Sender\Validation; |
6
|
|
|
use Sender\Otp\OtpSend; |
7
|
|
|
use Sender\MobileNumber; |
8
|
|
|
use Sender\Traits\OtpBuildTrait; |
9
|
|
|
use Sender\Traits\SmsOtpCommonTrait; |
10
|
|
|
use Sender\Config\Config as ConfigClass; |
11
|
|
|
use Sender\ExceptionClass\ParameterException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This Class for build OTP |
15
|
|
|
* |
16
|
|
|
* @package Sender\OtpClass |
17
|
|
|
* @author VenkatS <[email protected]> |
18
|
|
|
* @link https://github.com/tesark/msg91-php |
19
|
|
|
* @license MIT |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class OtpBuildClass extends OtpDefineClass |
23
|
|
|
{ |
24
|
|
|
use SmsOtpCommonTrait; |
25
|
|
|
use OtpBuildTrait; |
26
|
|
|
/** |
27
|
|
|
* This function used for build Retype data |
28
|
|
|
* @param string $key |
29
|
|
|
* @param array $data |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
4 |
|
public function buildRetryType($key, $data) |
34
|
|
|
{ |
35
|
4 |
|
if ($this->setRetryType()) { |
36
|
4 |
|
$value = $this->getRetryType(); |
37
|
4 |
|
$data = $this->addDataArray($key, $value, $data, 'string'); |
38
|
|
|
} |
39
|
|
|
return $data; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* This function used for build Retryp data |
43
|
|
|
* @param string $key |
44
|
|
|
* @param array $data |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function buildOneTimePass($data) |
49
|
|
|
{ |
50
|
|
|
if ($this->setOneTimePass()) { |
51
|
|
|
$key = 'otp'; |
52
|
|
|
$value = $this->getOneTimePass(); |
53
|
|
|
$data = $this->addDataArray($key, $value, $data, 'int'); |
54
|
|
|
} |
55
|
|
|
return $data; |
56
|
|
|
} |
57
|
|
|
/** |
58
|
|
|
* This function used for build Resend and Verify Otp Atrributes atrributes |
59
|
|
|
* @param string $key |
60
|
|
|
* @param array $data |
61
|
|
|
* |
62
|
|
|
* @throws ParameterException missing parameters or return empty |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
4 |
|
public function buildResendAndVerifyOtpArrtibutes($key, $data) |
66
|
|
|
{ |
67
|
4 |
|
if ($this->isKeyExists($key, $data)) { |
68
|
|
|
switch ($key) { |
69
|
4 |
|
case 'retrytype': |
70
|
4 |
|
$data = $this->buildRetryType($key, $data); |
71
|
|
|
break; |
72
|
|
|
case 'oneTime': |
73
|
|
|
$data = $this->buildOneTimePass($data); |
74
|
|
|
break; |
75
|
|
|
default: |
76
|
|
|
$message = "parameter".$key."Missing"; |
77
|
|
|
throw ParameterException::missinglogic($message); |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return $data; |
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* This function used for build message data |
85
|
|
|
* @param string $key |
86
|
|
|
* @param array $data |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
3 |
|
public function buildMessage($key, $data) |
91
|
|
|
{ |
92
|
3 |
|
if ($this->setMessage()) { |
93
|
3 |
|
$value = $this->getMessage(); |
94
|
3 |
|
$data = $this->addDataArray($key, $value, $data, 'string'); |
95
|
|
|
} |
96
|
2 |
|
return $data; |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* This function used for build sender data |
100
|
|
|
* @param string $key |
101
|
|
|
* @param array $data |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
15 |
|
public function buildSender($key, $data) |
106
|
|
|
{ |
107
|
15 |
|
if ($this->setSender()) { |
108
|
15 |
|
$value = $this->getSender(); |
109
|
15 |
|
if ($this->isString($value)) { |
110
|
13 |
|
$data = $this->validLength($key, $value, $data, 'otp'); |
111
|
|
|
} else { |
112
|
2 |
|
throw ParameterException::invalidArrtibuteType($key, "string", $value); |
113
|
|
|
} |
114
|
|
|
} |
115
|
12 |
|
return $data; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* This function used for build otp data |
119
|
|
|
* @param string $key |
120
|
|
|
* @param array $data |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
2 |
|
public function buildOtp($key, $data) |
125
|
|
|
{ |
126
|
2 |
|
if ($this->setOtp()) { |
127
|
2 |
|
$value = $this->getOtp(); |
128
|
2 |
|
$data = $this->addDataArray($key, $value, $data, 'int'); |
129
|
|
|
} |
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
/** |
133
|
|
|
* Check OtpExpiry limit |
134
|
|
|
* @param string $key |
135
|
|
|
* @param int $value |
136
|
|
|
* |
137
|
|
|
*/ |
138
|
9 |
|
public function validOtpExpiry($key, $value) |
139
|
|
|
{ |
140
|
9 |
|
if ($value >= 1) { |
141
|
7 |
|
return $value; |
142
|
|
|
} else { |
143
|
2 |
|
$message = "otp expiry min 1 mintues default 1 day. you given $value"; |
144
|
2 |
|
throw ParameterException::invalidInput($key, "int", $value, $message); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* This function used for build otpExpiry data |
149
|
|
|
* @param string $key |
150
|
|
|
* @param array $data |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
9 |
|
public function buildOtpExpiry($key, $data) |
155
|
|
|
{ |
156
|
9 |
|
if ($this->setOtpExpiry()) { |
157
|
9 |
|
$value = $this->getOtpExpiry(); |
158
|
9 |
|
$value = $this->validOtpExpiry($key, $value); |
159
|
7 |
|
$data = $this->addDataArray($key, $value, $data, 'int'); |
160
|
|
|
} |
161
|
6 |
|
return $data; |
162
|
|
|
} |
163
|
|
|
/** |
164
|
|
|
* This function used for Check otpLength data |
165
|
|
|
* @param string $key |
166
|
|
|
* @param int $value |
167
|
|
|
* @param array $data |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
4 |
|
public function checkOtpLength($key, $value, $data) |
172
|
|
|
{ |
173
|
4 |
|
if ($value >= 4 && $value < 10) { |
174
|
2 |
|
$data = $this->addArray($key, $value, $data); |
175
|
2 |
|
return $data; |
176
|
|
|
} else { |
177
|
2 |
|
$message = "otp length min 4 to max 9. you given $value"; |
178
|
2 |
|
throw ParameterException::invalidInput($key, "int", $value, $message); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* This function used for build otpLength data |
183
|
|
|
* @param string $key |
184
|
|
|
* @param int $value |
185
|
|
|
* @param array $data |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
6 |
|
public function buildOtpLength($key, $data) |
190
|
|
|
{ |
191
|
6 |
|
if ($this->setOtpLength()) { |
192
|
6 |
|
$value = $this->getOtpLength(); |
193
|
6 |
|
if ($this->isInterger($value)) { |
194
|
4 |
|
$data = $this->checkOtpLength($key, $value, $data); |
195
|
2 |
|
return $data; |
196
|
|
|
} else { |
197
|
2 |
|
throw ParameterException::invalidArrtibuteType($key, "int", $value); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
/** |
202
|
|
|
* Add otp on the array |
203
|
|
|
* @param array $inputData |
204
|
|
|
* @param array $data |
205
|
|
|
* |
206
|
|
|
* @throws ParameterException missing parameters or return empty |
207
|
|
|
* @return array condition correct value add to the $data array |
208
|
|
|
*/ |
209
|
3 |
|
public function addMessage($inputData, $data) |
210
|
|
|
{ |
211
|
3 |
|
if ($this->isKeyExists('message', $inputData)) { |
212
|
3 |
|
$data = $this->buildMessage('message', $data); |
213
|
|
|
} |
214
|
2 |
|
return $data; |
215
|
|
|
} |
216
|
|
|
/** |
217
|
|
|
* Add sender on the Array |
218
|
|
|
* @param array $inputData |
219
|
|
|
* @param array $data |
220
|
|
|
* |
221
|
|
|
* @throws ParameterException missing parameters or return empty |
222
|
|
|
* @return array condition correct value add to the $data array |
223
|
|
|
*/ |
224
|
15 |
|
public function addSender($inputData, $data) |
225
|
|
|
{ |
226
|
15 |
|
if ($this->isKeyExists('sender', $inputData)) { |
227
|
15 |
|
$data = $this->buildSender('sender', $data); |
228
|
|
|
} |
229
|
12 |
|
return $data; |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Add otp on the array |
233
|
|
|
* @param array $inputData |
234
|
|
|
* @param array $data |
235
|
|
|
* |
236
|
|
|
* @throws ParameterException missing parameters or return empty |
237
|
|
|
* @return array condition correct value add to the $data array |
238
|
|
|
*/ |
239
|
2 |
|
public function addOtp($inputData, $data) |
240
|
|
|
{ |
241
|
2 |
|
if ($this->isKeyExists('otp', $inputData)) { |
242
|
2 |
|
$data = $this->buildOtp('otp', $data); |
243
|
|
|
} |
244
|
|
|
return $data; |
245
|
|
|
} |
246
|
|
|
/** |
247
|
|
|
* Add otp_expiry on the array |
248
|
|
|
* @param array $inputData |
249
|
|
|
* @param array $data |
250
|
|
|
* |
251
|
|
|
* @throws ParameterException missing parameters or return empty |
252
|
|
|
* @return array condition correct value add to the $data array |
253
|
|
|
*/ |
254
|
12 |
|
public function addOtpExpiry($inputData, $data) |
255
|
|
|
{ |
256
|
12 |
|
if ($this->isKeyExists('otp_expiry', $inputData)) { |
257
|
9 |
|
$data = $this->buildOtpExpiry('otp_expiry', $data); |
258
|
|
|
} |
259
|
9 |
|
return $data; |
260
|
|
|
} |
261
|
|
|
/** |
262
|
|
|
* Add otp_length on the array |
263
|
|
|
* @param array $inputData |
264
|
|
|
* @param array $data |
265
|
|
|
* |
266
|
|
|
* @throws ParameterException missing parameters or return empty |
267
|
|
|
* @return array condition correct value add to the $data array |
268
|
|
|
*/ |
269
|
9 |
|
public function addOtpLength($inputData, $data) |
270
|
|
|
{ |
271
|
9 |
|
if ($this->isKeyExists('otp_length', $inputData)) { |
272
|
6 |
|
$data = $this->buildOtpLength('otp_length', $data); |
273
|
|
|
} |
274
|
5 |
|
return $data; |
275
|
|
|
} |
276
|
|
|
/** |
277
|
|
|
* This function for buils Auth key |
278
|
|
|
* @param string $parameter |
279
|
|
|
* |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
20 |
|
public function hasAuthKey($parameter) |
283
|
|
|
{ |
284
|
20 |
|
if ($this->setAuthkey()) { |
285
|
20 |
|
$value = $this->getAuthkey(); |
286
|
20 |
|
if ($this->isString($value)) { |
287
|
20 |
|
return true; |
288
|
|
|
} else { |
289
|
|
|
throw ParameterException::invalidArrtibuteType($parameter, "string", $value); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
/** |
294
|
|
|
* This function for buils Mobile |
295
|
|
|
* @param string $parameter |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
*/ |
299
|
20 |
|
public function hasMobile($parameter) |
300
|
|
|
{ |
301
|
20 |
|
if ($this->setmobile()) { |
302
|
20 |
|
$value = $this->getmobile(); |
303
|
20 |
|
if ($this->isInterger($value)) { |
304
|
19 |
|
return true; |
305
|
|
|
} else { |
306
|
1 |
|
throw ParameterException::invalidArrtibuteType($parameter, "int", $value); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
/** |
311
|
|
|
* Add retry type |
312
|
|
|
* |
313
|
|
|
* @param array $data |
314
|
|
|
* |
315
|
|
|
* @throws ParameterException missing parameters or return empty |
316
|
|
|
* @return array Msg91 Json response |
317
|
|
|
*/ |
318
|
4 |
|
public function addRetryType($data) |
319
|
|
|
{ |
320
|
4 |
|
$data = $this->buildResendAndVerifyOtpArrtibutes('retrytype', $data); |
321
|
|
|
return $data; |
322
|
|
|
} |
323
|
|
|
/** |
324
|
|
|
* Add otp on the array |
325
|
|
|
* @param array $data |
326
|
|
|
* |
327
|
|
|
* @throws ParameterException missing parameters or return empty |
328
|
|
|
* @return array condition correct value add to the $data array |
329
|
|
|
*/ |
330
|
|
|
public function addOneTimePass($data) |
331
|
|
|
{ |
332
|
|
|
$data = $this->buildResendAndVerifyOtpArrtibutes('oneTime', $data); |
333
|
|
|
return $data; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|