1
|
|
|
<?php |
2
|
|
|
namespace JsonTable\Validate; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Format validator abstract. |
6
|
|
|
* |
7
|
|
|
* @package JSON table |
8
|
|
|
*/ |
9
|
|
|
abstract class AbstractFormatValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @access protected |
13
|
|
|
* @var string The type of validation being done. eg "string", "number". |
14
|
|
|
*/ |
15
|
|
|
protected $type; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @access protected |
19
|
|
|
* @var mixed The input being validated. |
20
|
|
|
*/ |
21
|
|
|
protected $input; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Construct the validator. |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* |
29
|
|
|
* @param string $type The type of validation being validated. |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct($type) |
32
|
|
|
{ |
33
|
1 |
|
$this->type = (string) $type; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the input to validate. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* |
42
|
|
|
* @param mixed $input The input to validate. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
1 |
|
public function setInput($input) |
47
|
|
|
{ |
48
|
1 |
|
$this->input = $input; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check that the input matches the specified format. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* |
57
|
|
|
* @param string $format The format to validate against. |
58
|
|
|
* |
59
|
|
|
* @return boolean Is the data valid. |
60
|
|
|
* |
61
|
|
|
* @throws \Exception if the method to validate the format couldn't be found. |
62
|
|
|
*/ |
63
|
1 |
|
public function validateFormat($format) |
64
|
|
|
{ |
65
|
1 |
|
if ('' === $this->input) { |
66
|
1 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$methodDetails = $this->getFormatMethodDetails($format); |
70
|
|
|
|
71
|
1 |
|
if (!method_exists($this, $methodDetails['name'])) { |
72
|
|
|
throw new \Exception("Could not find a method to validate the $format format."); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$lb_valid = $this->$methodDetails['name']($methodDetails['parameter']); |
76
|
|
|
|
77
|
1 |
|
return $lb_valid; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the name of the method and the parameter to pass to it for the specified format. |
83
|
|
|
* |
84
|
|
|
* @access private |
85
|
|
|
* |
86
|
|
|
* @param string $format The format to validate against. |
87
|
|
|
* |
88
|
|
|
* @return array $methodDetails The name and parameter for the format. |
89
|
|
|
*/ |
90
|
1 |
|
private function getFormatMethodDetails($format) |
91
|
|
|
{ |
92
|
1 |
|
$methodDetails = []; |
93
|
1 |
|
$methodDetails['name'] = 'format' . ucwords($format); |
94
|
1 |
|
$methodDetails['parameter'] = null; |
95
|
|
|
|
96
|
1 |
|
if ('datetime' === $this->type && 'default' !== $format) { |
97
|
1 |
|
$methodDetails['name'] = "formatDate"; |
98
|
1 |
|
$methodDetails['parameter'] = $format; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
return $methodDetails; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|