|
1
|
|
|
<?php |
|
2
|
|
|
namespace JsonTable\Validate\Format; |
|
3
|
|
|
|
|
4
|
|
|
use \JsonTable\Validate\AbstractFormatValidator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Lexical string validator. |
|
8
|
|
|
* |
|
9
|
|
|
* @package JSON table |
|
10
|
|
|
*/ |
|
11
|
|
|
class StringValidator extends AbstractFormatValidator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Validate that the input is a valid string. |
|
15
|
|
|
* |
|
16
|
|
|
* @return boolean Whether the input is valid. |
|
17
|
|
|
*/ |
|
18
|
72 |
|
protected function formatDefault() |
|
19
|
|
|
{ |
|
20
|
72 |
|
return is_string($this->input); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Validate that the input is a valid email address. |
|
26
|
|
|
* |
|
27
|
|
|
* @return boolean Whether the input is valid. |
|
28
|
|
|
*/ |
|
29
|
72 |
|
protected function formatEmail() |
|
30
|
|
|
{ |
|
31
|
72 |
|
return (false !== filter_var($this->input, FILTER_VALIDATE_EMAIL)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Validate that the input is a valid URI. |
|
37
|
|
|
* Although the specification for a URI states |
|
38
|
|
|
* that it must have a scheme i.e. "http://" @see http://www.faqs.org/rfcs/rfc2396.html |
|
39
|
|
|
* |
|
40
|
|
|
* This validator allows the input to miss this off so an input |
|
41
|
|
|
* of "www.example.com" will be passed as valid. |
|
42
|
|
|
* |
|
43
|
|
|
* @return boolean Whether the input is valid. |
|
44
|
|
|
*/ |
|
45
|
72 |
|
protected function formatUri() |
|
46
|
|
|
{ |
|
47
|
72 |
|
if ($urlParts = parse_url($this->input)) { |
|
48
|
72 |
|
if (!isset($urlParts['scheme'])) { |
|
49
|
72 |
|
$this->input = "http://$this->input"; |
|
50
|
72 |
|
} |
|
51
|
72 |
|
} |
|
52
|
|
|
|
|
53
|
72 |
|
return (false !== filter_var($this->input, FILTER_VALIDATE_URL)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Validate that the input is a valid binary string. |
|
59
|
|
|
* As PHP treats all stings as binary, this is currently just a check that the input is a string. |
|
60
|
|
|
* TODO: Find a better way of validating that the string is a binary. |
|
61
|
|
|
* |
|
62
|
|
|
* @return boolean Whether the input is valid. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function formatBinary() |
|
65
|
|
|
{ |
|
66
|
|
|
return is_string($this->input); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|