-
Notifications
You must be signed in to change notification settings - Fork 19
/
ServiceClient.php
82 lines (69 loc) · 2.13 KB
/
ServiceClient.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
<?php
namespace MaxMind\MinFraud;
use MaxMind\Exception\InvalidInputException;
use MaxMind\WebService\Client;
use Respect\Validation\Exceptions\ValidationException;
abstract class ServiceClient
{
const VERSION = 'v1.13.0';
protected $client;
protected static $host = 'minfraud.maxmind.com';
protected static $basePath = '/minfraud/v2.0/';
protected $validateInput = true;
public function __construct(
$accountId,
$licenseKey,
$options = []
) {
if (!isset($options['host'])) {
$options['host'] = self::$host;
}
$options['userAgent'] = $this->userAgent();
$this->client = new Client($accountId, $licenseKey, $options);
if (isset($options['validateInput'])) {
$this->validateInput = $options['validateInput'];
}
}
/**
* @return string the prefix for the User-Agent header
*/
protected function userAgent()
{
return 'minFraud-API/' . self::VERSION;
}
/**
* @param string $className The name of the class (but not the namespace)
* @param array $values The values to validate
*
* @throws InvalidInputException when $values does not validate
*
* @return array The cleaned values
*/
protected function cleanAndValidate($className, $values)
{
$values = $this->clean($values);
if (!$this->validateInput) {
return $values;
}
$class = '\\MaxMind\\MinFraud\\Validation\\Rules\\' . $className;
$validator = new $class();
try {
$validator->check($values);
} catch (ValidationException $exception) {
throw new InvalidInputException($exception->getMessage(), $exception->getCode());
}
return $values;
}
protected function clean($array)
{
$cleaned = [];
foreach ($array as $key => $value) {
if (\is_array($value)) {
$cleaned[$key] = $this->clean($array[$key]);
} elseif ($array[$key] !== null) {
$cleaned[$key] = $array[$key];
}
}
return $cleaned;
}
}