-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCoinpaymentsValidator.php
565 lines (541 loc) · 18.4 KB
/
CoinpaymentsValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
* Class CoinpaymentsValidator
*
* Validates fields passed to the CoinpaymentsCurlRequest::execute function.
* Ensures required fields have been passed, that mutually exclusive fields are used alone
* and that optional fields, if passed, exist for the given command.
* Checks some values to see if they are within a permitted range.
* Checks the type of field values passed to make sure they are what the command expects.
*/
class CoinpaymentsValidator
{
protected $command;
protected $fields;
/**
* @var array $commands_reference An array of the optional and required fields
* for each API command, including their value's types, specific permitted values
* and any mutually exclusive rules.
*/
protected $commands_reference = [
'get_basic_info' => [],
'rates' => [
'optional' => [
'accepted' => [
'type' => 'integer',
'permitted' => [0, 1]
],
'short' => [
'type' => 'integer',
'permitted' => [0, 1]
]
]
],
'balances' => [
'optional' => [
'all' => [
'type' => 'integer',
'permitted' => [0, 1]
]
]
],
'get_deposit_address' => [
'required' => [
'currency' => [
'type' => 'string'
]
]
],
'create_transaction' => [
'required' => [
'amount' => [
'type' => 'integer'
],
'currency1' => [
'type' => 'string'
],
'currency2' => [
'type' => 'string'
],
'buyer_email' => [
'type' => 'email'
]
],
'optional' => [
'address' => [
'type' => 'string'
],
'buyer_name' => [
'type' => 'string'
],
'item_name' => [
'type' => 'string'
],
'item_number' => [
'type' => 'string'
],
'invoice' => [
'type' => 'string'
],
'custom' => [
'type' => 'string'
],
'ipn_url' => [
'type' => 'url'
],
'success_url' => [
'type' => 'url'
],
'cancel_url' => [
'type' => 'url'
]
]
],
'get_callback_address' => [
'required' => [
'currency' => [
'type' => 'string'
]
],
'optional' => [
'ipn_url' => [
'type' => 'url'
],
'label' => [
'type' => 'string'
]
]
],
'get_tx_info' => [
'required' => [
'txid' => [
'type' => 'string'
]
],
'optional' => [
'full' => [
'type' => 'integer',
'permitted' => [0, 1]
]
]
],
'get_tx_info_multi' => [
'required' => [
'txid' => [
'type' => 'string'
]
]
],
'get_tx_ids' => [
'optional' => [
'limit' => [
'type' => 'integer'
],
'start' => [
'type' => 'integer'
],
'newer' => [
'type' => 'integer'
],
'all' => [
'type' => 'integer',
'permitted' => [0, 1]
]
]
],
'create_transfer' => [
'required' => [
'amount' => [
'type' => 'integer'
],
'currency' => [
'type' => 'string'
]
],
'one_of' => [
'merchant' => [
'type' => 'string'
],
'pbntag' => [
'type' => 'string'
]
],
'optional' => [
'auto_confirm' => [
'type' => 'integer',
'permitted' => [0, 1]
]
]
],
'create_withdrawal' => [
'required' => [
'amount' => [
'type' => 'integer'
],
'currency' => [
'type' => 'string'
]
],
'one_of' => [
'address' => [
'type' => 'string'
],
'pbntag' => [
'type' => 'string'
]
],
'optional' => [
'add_tx_fee' => [
'type' => 'integer',
'permitted' => [0, 1]
],
'currency2' => [
'type' => 'string'
],
'dest_tag' => [
'type' => 'string'
],
'ipn_url' => [
'type' => 'url'
],
'auto_confirm' => [
'type' => 'integer',
'permitted' => [0, 1]
],
'note' => [
'type' => 'string'
]
]
],
'create_mass_withdrawal' => [
'required' => [
'wd' => [
'type' => 'array'
],
]
],
'convert' => [
'required' => [
'amount' => [
'type' => 'integer'
],
'from' => [
'type' => 'string'
],
'to' => [
'type' => 'string'
]
],
'optional' => [
'address' => [
'type' => 'string'
],
'dest_tag' => [
'type' => 'string'
]
]
],
'convert_limits' => [
'required' => [
'from' => [
'type' => 'string'
],
'to' => [
'type' => 'string'
]
]
],
'get_withdrawal_history' => [
'optional' => [
'limit' => [
'type' => 'integer'
],
'start' => [
'type' => 'integer'
],
'newer' => [
'type' => 'integer'
]
]
],
'get_withdrawal_info' => [
'required' => [
'id' => [
'type' => 'string'
]
]
],
'get_conversion_info' => [
'required' => [
'id' => [
'type' => 'string'
]
]
],
'get_pbn_info' => [
'required' => [
'pbntag' => [
'type' => 'string'
]
]
],
'get_pbn_list' => [],
'update_pbn_tag' => [
'required' => [
'tagid' => [
'type' => 'string'
]
],
'optional' => [
'name' => [
'type' => 'string'
],
'email' => [
'type' => 'email'
],
'url' => [
'type' => 'url'
],
'image' => [
'type' => 'string'
]
]
],
'claim_pbn_tag' => [
'required' => [
'tagid' => [
'type' => 'string'
],
'name' => [
'type' => 'string'
]
]
],
];
public function __construct($command, $fields)
{
$this->command = $command;
$this->fields = $fields;
}
/**
* function validate
*
* The actual validation function run before the curl execute method.
*
* @return bool|string
* @throws Exception
*/
public function validate()
{
$valid_command = $this->validateCommand();
try {
if ($valid_command === TRUE) {
$valid_fields = $this->validateFields();
try {
if ($valid_fields === TRUE) {
return TRUE;
} else {
throw new Exception($valid_fields);
}
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
} else {
throw new Exception('Invalid command name!');
}
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
}
/**
* function validateCommand
*
* Validates the name of the command.
*
* @return bool|string
* @throws Exception
*/
private function validateCommand()
{
try {
if (array_key_exists($this->command, $this->commands_reference)) {
return TRUE;
}
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
}
/**
* function validateFields
*
* Validates the existence of fields, respective to their field groups.
*
* @return bool True on success of validated fields.
* @throws Exception
*/
private function validateFields()
{
// Set top level field groups
$field_groups = ['required', 'one_of', 'optional'];
// Compile array of accepted fields
$accepted_fields = [];
foreach ($field_groups as $field_group) {
// Check if the field group exists for the command
if (array_key_exists($field_group, $this->commands_reference[$this->command])) {
// For each field group that exists, add the fields within to the accepted fields array
foreach ($this->commands_reference[$this->command][$field_group] as $field_key => $field_value) {
$accepted_fields[] = $field_key;
}
}
}
// Validate all passed fields are valid
foreach ($this->fields as $field_key => $field_value) {
// Throw an error if an invalid field was passed
if (!in_array($field_key, $accepted_fields)) {
throw new Exception('The field "' . $field_key . '" was passed but is not a valid field for the "' . $this->command . '" command!');
}
}
// Check required fields
if (array_key_exists('required', $this->commands_reference[$this->command])) {
foreach ($this->commands_reference[$this->command]['required'] as $required_field_key => $required_field_value) {
if (!array_key_exists($required_field_key, $this->fields)) {
throw new Exception('The required field "' . $required_field_key . '" was not passed!');
} else {
$field_type = $this->commands_reference[$this->command]['required'][$required_field_key]['type'];
$is_valid_type = $this->validateFieldType($required_field_key, $this->fields[$required_field_key], $field_type, 'required');
if ($is_valid_type != TRUE) {
throw new Exception($is_valid_type);
}
}
}
}
// Check one_of (mutually exclusive) fields
if (array_key_exists('one_of', $this->commands_reference[$this->command])) {
$count_one_of = 0;
$expected_one_of = [];
$expected_one_of_message = '';
$one_of_field_key = '';
$passed_one_of_key = '';
// Compile an array of all passed fields in the one_of group
foreach ($this->commands_reference[$this->command]['one_of'] as $one_of_field_key => $one_of_field_value) {
$expected_one_of[] = $one_of_field_key;
if (array_key_exists($one_of_field_key, $this->fields)) {
$count_one_of++;
$passed_one_of_key = $one_of_field_key;
}
}
// Set a message defining the possible one_of fields, if there was more or less than one passed
if ($count_one_of != 1) {
$expected_one_of_message = implode(' | ', $expected_one_of);
}
// Throw an error if less than or more than 1 field was passed
if ($count_one_of < 1) {
throw new Exception('At least one of the following fields must be passed: [ ' . $expected_one_of_message . ' ]');
} elseif ($count_one_of > 1) {
throw new Exception('No more than one of the following fields can be passed: [ ' . $expected_one_of_message . ' ]');
} else {
$field_type = $this->commands_reference[$this->command]['one_of'][$one_of_field_key]['type'];
$is_valid_type = $this->validateFieldType($one_of_field_key, $this->fields[$passed_one_of_key], $field_type, 'one_of');
if ($is_valid_type != TRUE) {
throw new Exception($is_valid_type);
}
}
}
// Check if optional fields exist
if (array_key_exists('optional', $this->commands_reference[$this->command])) {
$optional_fields_to_check = [];
// Build array of the optional fields passed
foreach ($this->fields as $field_key => $field_value) {
if (array_key_exists($field_key, $this->commands_reference[$this->command]['optional'])) {
$optional_fields_to_check[] = $field_key;
}
}
// Loop through optional fields passed and validate their value's type
foreach ($optional_fields_to_check as $optional_field) {
$field_type = $this->commands_reference[$this->command]['optional'][$optional_field]['type'];
$is_valid_type = $this->validateFieldType($optional_field, $this->fields[$optional_field], $field_type, 'optional');
if ($is_valid_type != TRUE) {
throw new Exception($is_valid_type);
}
}
}
return TRUE;
}
/**
* function validateFieldType
*
* Validates the data type for a field's passed value.
*
* @param string $field_key The name/key for the field.
* @param mixed $field_value The value for the field being checked.
* @param string $expected_type The expected type from the $commands_reference.
* @param string $field_group The group the field belongs to within the $commands_reference.
* @return bool True if field value's type is valid.
* @throws Exception
*/
private function validateFieldType($field_key, $field_value, $expected_type, $field_group)
{
$actual_type = FALSE;
switch ($expected_type) {
case 'string':
if (!is_string($field_value)) {
$actual_type = gettype($field_value);
}
break;
case 'integer':
if (!is_numeric($field_value)) {
$actual_type = gettype($field_value);
}
break;
case 'url':
if (filter_var($field_value, FILTER_VALIDATE_URL) === FALSE) {
$actual_type = gettype($field_value);
}
break;
case 'email':
if (filter_var($field_value, FILTER_VALIDATE_EMAIL) === FALSE) {
$actual_type = gettype($field_value);
}
break;
case 'array':
if (!is_array($field_value)) {
$actual_type = gettype($field_value);
}
break;
default:
throw new Exception('Expected type "' . $expected_type . '" is not valid for the given command.');
}
if (is_array($field_value)) {
$field_value = '[ArrayData]';
}
if ($actual_type != FALSE) {
throw new Exception('Field "' . $field_key . '" passed with value of "' . $field_value . '" and data type of "' . $actual_type . '", but expected type is "' . $expected_type . '".');
}
if (array_key_exists('permitted', $this->commands_reference[$this->command][$field_group][$field_key])) {
$permitted_check = $this->validateFieldPermittedValue($field_value, $this->commands_reference[$this->command][$field_group][$field_key]['permitted']);
if (!$permitted_check) {
$permitted_message = implode(' | ', $this->commands_reference[$this->command][$field_group][$field_key]['permitted']);
throw new Exception('Permitted values for the field "' . $field_key . '" are [ ' . $permitted_message . ' ] but the value passed was: ' . $field_value);
}
}
return TRUE;
}
/**
* function validateFieldPermittedValue
*
* Validates the passed field value is in a permitted range of values.
*
* @param mixed $field_value The value of the field to check against permitted values.
* @param array $permitted An array containing the permitted values.
* @return bool True if field value is in permitted values array, false if it's not.
*/
private function validateFieldPermittedValue($field_value, $permitted)
{
// Check the passed field value is within the permitted values
if (!in_array($field_value, $permitted)) {
return FALSE;
} else {
return TRUE;
}
}
}