1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\components\validation; |
3
|
|
|
/** |
4
|
|
|
* @author jc |
5
|
|
|
* @version 1.001 |
6
|
|
|
* Generates a JSON Rule for the validation of a field |
7
|
|
|
*/ |
8
|
|
|
class Rule implements \JsonSerializable{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $type; |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $prompt; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $value; |
22
|
|
|
|
23
|
|
|
public function __construct($type,$prompt=NULL,$value=NULL){ |
24
|
|
|
$this->type=$type; |
25
|
|
|
$this->prompt=$prompt; |
26
|
|
|
$this->value=$value; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getType() { |
30
|
|
|
return $this->type; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setType($type) { |
34
|
|
|
$this->type=$type; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getPrompt() { |
39
|
|
|
return $this->prompt; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setPrompt($prompt) { |
43
|
|
|
$this->prompt=$prompt; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getValue() { |
48
|
|
|
return $this->value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setValue($value) { |
52
|
|
|
$this->value=$value; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function jsonSerialize() { |
57
|
|
|
$result= ["type"=>$this->type]; |
58
|
|
|
if(isset($this->prompt)) |
59
|
|
|
$result["prompt"]=$this->prompt; |
60
|
|
|
if(isset($this->value)) |
61
|
|
|
$result["value"]=$this->value; |
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function match($name,$prompt=NULL){ |
66
|
|
|
return new Rule("match[".$name."]",$prompt); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function integer($min=0,$max=100,$prompt=NULL){ |
70
|
|
|
return new Rule("integer[{$min}..{$max}]",$prompt); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function decimal($prompt=NULL){ |
74
|
|
|
return new Rule("decimal",$prompt); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function number($prompt=NULL){ |
78
|
|
|
return new Rule("number",$prompt); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function is($value,$prompt=NULL){ |
82
|
|
|
return new Rule("is[".$value."]",$prompt); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function isExactly($value,$prompt=NULL){ |
86
|
|
|
return new Rule("isExactly[".$value."]",$prompt); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function not($value,$prompt=NULL){ |
90
|
|
|
return new Rule("not[".$value."]",$prompt); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function notExactly($value,$prompt=NULL){ |
94
|
|
|
return new Rule("notExactly[".$value."]",$prompt); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function contains($value,$prompt=NULL){ |
98
|
|
|
return new Rule("contains[".$value."]",$prompt); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function containsExactly($value,$prompt=NULL){ |
102
|
|
|
return new Rule("containsExactly[".$value."]",$prompt); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function doesntContain($value,$prompt=NULL){ |
106
|
|
|
return new Rule("doesntContain[".$value."]",$prompt); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function doesntContainExactly($value,$prompt=NULL){ |
110
|
|
|
return new Rule("doesntContainExactly[".$value."]",$prompt); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function minCount($value,$prompt=NULL){ |
114
|
|
|
return new Rule("minCount[".$value."]",$prompt); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function maxCount($value,$prompt=NULL){ |
118
|
|
|
return new Rule("maxCount[".$value."]",$prompt); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function exactCount($value,$prompt=NULL){ |
122
|
|
|
return new Rule("exactCount[".$value."]",$prompt); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function email($prompt=NULL){ |
126
|
|
|
return new Rule("email",$prompt); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |