|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
|
15
|
|
|
|
|
16
|
|
|
use function class_exists; |
|
17
|
|
|
use function mb_strtolower; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
use function ucfirst; |
|
20
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
21
|
|
|
use Respect\Validation\Validatable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Abstract class for helping on creating rules that use a rule inside to validate. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Henrique Moody <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class CountryBasedWrapper extends AbstractRule |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Validatable |
|
32
|
|
|
*/ |
|
33
|
|
|
private $validatable; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $countryCode; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Initializes the rule. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $countryCode |
|
44
|
|
|
* |
|
45
|
|
|
* @throws ComponentException When country is not supported. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(string $countryCode) |
|
48
|
|
|
{ |
|
49
|
|
|
$className = $this->getClassName(ucfirst(mb_strtolower($countryCode))); |
|
50
|
|
|
if (!class_exists($className)) { |
|
51
|
|
|
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->countryCode = $countryCode; |
|
55
|
|
|
$this->validatable = new $className; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function assert($input) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->validatable->assert($input); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function check($input) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->validatable->check($input); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function validate($input) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->validatable->validate($input); |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setName($name) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->validatable->setName($name); |
|
87
|
|
|
|
|
88
|
|
|
return parent::setName($name); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the class name based on the identifier. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $identifier |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
abstract protected function getClassName(string $identifier): string; |
|
98
|
|
|
} |
|
99
|
|
|
|