-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidator.php
More file actions
57 lines (48 loc) · 1.14 KB
/
Validator.php
File metadata and controls
57 lines (48 loc) · 1.14 KB
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
<?php
namespace Rickard2\Luhnar;
use Exception;
/**
* Class Validator
*
* @package Rickard2\Luhnar
*/
class Validator
{
/**
* @var array
*/
protected $countryClassMap = array(
'se' => '\Rickard2\Luhnar\Validator\Sweden',
'fi' => '\Rickard2\Luhnar\Validator\Finland',
'nl' => '\Rickard2\Luhnar\Validator\Netherlands',
);
/**
* @param string $input
* @param string $country
*
* @return bool
* @throws \Exception
*/
public function validate($input, $country)
{
$country = trim(strtolower($country));
if (!$this->supportsCountry($country)) {
throw new Exception('Unsupported country: ' . $country);
}
if (!$input) {
return true;
}
/** @var \Rickard2\Luhnar\Validator\Validator $validator */
$validator = new $this->countryClassMap[$country];
return $validator->validate($input);
}
/**
* @param string $country
*
* @return bool
*/
protected function supportsCountry($country)
{
return array_key_exists($country, $this->countryClassMap);
}
}