Completed
Push — master ( 9281ef...7089ac )
by Jean-Christophe
03:28
created

Rule::is()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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
	/**
66
	 * A field should match the value of another validation field, for example to confirm passwords
67
	 * @param string $name
68
	 * @param string $prompt
69
	 * @return \Ajax\semantic\components\validation\Rule
70
	 */
71
	public static function match($name,$prompt=NULL){
72
		return new Rule("match[".$name."]",$prompt);
73
	}
74
75
	/**
76
	 * A field should be different than another specified field
77
	 * @param string $name
78
	 * @param string $prompt
79
	 * @return \Ajax\semantic\components\validation\Rule
80
	 */
81
	public static function different($name,$prompt=NULL){
82
		return new Rule("different[".$name."]",$prompt);
83
	}
84
85
	/**
86
	 * A field is an integer value, or matches an integer range
87
	 * @param int|NULL $min
88
	 * @param int|NULL $max
89
	 * @param string $prompt
90
	 * @return \Ajax\semantic\components\validation\Rule
91
	 */
92
	public static function integer($min=NULL,$max=NULL,$prompt=NULL){
93
		if(\is_int($min) && \is_int($max))
94
			return new Rule("integer[{$min}..{$max}]",$prompt);
95
		return new Rule("integer",$prompt);
96
	}
97
98
	public static function decimal($prompt=NULL){
99
		return new Rule("decimal",$prompt);
100
	}
101
102
	public static function number($prompt=NULL){
103
		return new Rule("number",$prompt);
104
	}
105
106
	public static function is($value,$prompt=NULL){
107
		return new Rule("is[".$value."]",$prompt);
108
	}
109
110
	public static function isExactly($value,$prompt=NULL){
111
		return new Rule("isExactly[".$value."]",$prompt);
112
	}
113
114
	public static function not($value,$prompt=NULL){
115
		return new Rule("not[".$value."]",$prompt);
116
	}
117
118
	public static function notExactly($value,$prompt=NULL){
119
		return new Rule("notExactly[".$value."]",$prompt);
120
	}
121
122
	public static function contains($value,$prompt=NULL){
123
		return new Rule("contains[".$value."]",$prompt);
124
	}
125
126
	public static function containsExactly($value,$prompt=NULL){
127
		return new Rule("containsExactly[".$value."]",$prompt);
128
	}
129
130
	public static function doesntContain($value,$prompt=NULL){
131
		return new Rule("doesntContain[".$value."]",$prompt);
132
	}
133
134
	public static function doesntContainExactly($value,$prompt=NULL){
135
		return new Rule("doesntContainExactly[".$value."]",$prompt);
136
	}
137
138
	public static function minCount($value,$prompt=NULL){
139
		return new Rule("minCount[".$value."]",$prompt);
140
	}
141
142
	public static function maxCount($value,$prompt=NULL){
143
		return new Rule("maxCount[".$value."]",$prompt);
144
	}
145
146
	public static function exactCount($value,$prompt=NULL){
147
		return new Rule("exactCount[".$value."]",$prompt);
148
	}
149
150
	public static function email($prompt=NULL){
151
		return new Rule("email",$prompt);
152
	}
153
154
	public static function url($prompt=NULL){
155
		return new Rule("url",$prompt);
156
	}
157
158
	public static function regExp($value,$prompt=NULL){
159
		return new Rule("regExp",$prompt,$value);
160
	}
161
162
	public static function custom($name,$jsFunction){
163
		return "$.fn.form.settings.rules.".$name." =".$jsFunction ;
164
	}
165
166
}