nyroFwk  0.2
valid.class.php
Go to the documentation of this file.
1 <?php
10 class valid extends object {
11 
17  protected $errors = array();
18 
24  public function getValue() {
25  return $this->cfg->value;
26  }
27 
33  public function setValue(&$value) {
34  $this->cfg->set('value', $value);
35  }
36 
44  public function addRule($type, $prm=null, $msg=null) {
45  if (!is_array($prm) && !is_callable($prm))
46  $prm = array($prm);
47  $this->cfg->setInArray('rules', $type, $prm);
48  if (!is_null($msg))
49  $this->setMessage($type, $msg);
50  }
51 
58  public function hasRule($type) {
59  return $this->cfg->checkInArray('rules', $type);
60  }
61 
67  public function delRule($type) {
68  $this->cfg->delInArray('rules', $type);
69  }
70 
76  public function getRules() {
77  return $this->cfg->rules;
78  }
79 
85  public function isValid() {
86  $this->errors = array();
87  $valid = true;
88  $noNeedRequired = $this->cfg->noNeedRequired;
89  $val = $this->cfg->validEltArray && is_array($this->cfg->value) ? $this->cfg->value : array($this->cfg->value);
90  foreach($this->cfg->rules as $rule=>$prm) {
91  if (!is_numeric($rule)) {
92  foreach($val as $v) {
93  if ((in_array($rule, $noNeedRequired)) || !empty($v))
94  $valid = $this->{'is'.ucfirst($rule)}($v, $prm) && $valid;
95  }
96  }
97  }
98  return $valid;
99  }
100 
108  public function isRequired($val, $prm=null) {
109  if (empty($val)) {
110  $this->errors[] = sprintf($this->getMessage('required'), $this->cfg->label);
111  return false;
112  }
113  return true;
114  }
115 
123  public function isNumeric($val, $prm=null) {
124  if (!is_numeric($val)) {
125  $this->errors[] = sprintf($this->getMessage('numeric'), $this->cfg->label);
126  return false;
127  }
128  return true;
129  }
130 
138  public function isInt($val, $prm=null) {
139  if (!is_numeric($val) || $val != (int)$val) {
140  $this->errors[] = sprintf($this->getMessage('int'), $this->cfg->label);
141  return false;
142  }
143  return true;
144  }
145 
153  public function isDifferent($val, $prm=null) {
154  if ($val == $prm[0]) {
155  $this->errors[] = sprintf($this->getMessage('different'), $this->cfg->label, $prm[0]);
156  return false;
157  }
158  return true;
159  }
160 
168  public function isIn($val, $prm=null) {
169  $ret = true;
170  $val = is_array($val)? $val : array($val);
171  $val = array_filter($val);
172  if (!empty($val))
173  foreach($val as $v) {
174  if (!in_array($v, $prm)) {
175  $this->errors[] = sprintf($this->getMessage('in'), $v, $this->cfg->label);
176  $ret = false;
177  }
178  }
179  return $ret;
180  }
181 
189  public function isEqual($val, $prm=null) {
190  $ret = true;
191  if ($prm[0] instanceof form_abstract) {
192  if ($val != $prm[0]->getRawValue()) {
193  $this->errors[] = sprintf($this->getMessage('equalInput'), $this->cfg->label, $prm[0]->label);
194  $ret = false;
195  }
196  } else {
197  if ($val != $prm[0]) {
198  $this->errors[] = sprintf($this->getMessage('equal'), $v, $this->cfg->label);
199  $ret = false;
200  }
201  }
202  return $ret;
203  }
204 
214  public function isGroupedFields($val, $prm=null) {
215  $nbFilled = 0;
216  foreach($prm['fields'] as $f) {
217  $val = $prm['form']->getValue($f);
218  if (!empty($val))
219  $nbFilled++;
220  }
221  $ret = $nbFilled == 0 || $nbFilled == count($prm['fields']);
222  if (!$ret) {
223  // not valid
224  $this->errors[] = sprintf($this->getMessage('groupedFields'), $this->cfg->label);
225  return false;
226  }
227  return true;
228  }
229 
239  public function isAtLeastOneField($val, $prm=null) {
240  $nbFilled = 0;
241  foreach($prm['fields'] as $f) {
242  $val = $prm['form']->getValue($f);
243  if (!empty($val))
244  $nbFilled++;
245  }
246  if ($nbFilled == 0) {
247  // not valid
248  $this->errors[] = sprintf($this->getMessage('atLeastOneField'), $this->cfg->label);
249  return false;
250  }
251  return true;
252  }
253 
261  public function isCallback($val, $prm=null) {
262  $tmp = call_user_func($prm, $val);
263  if ($tmp !== true && !is_null($tmp)) {
264  $tmp = is_string($tmp) ? $tmp : 'callback';
265  $msg = $this->getMessage($tmp);
266  $this->errors[] = sprintf($msg ? $msg : $tmp, $this->cfg->label);
267  return false;
268  }
269  return true;
270  }
271 
279  public function isUrl($val, $prm=null) {
280  if (!filter_var($val, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
281  $this->errors[] = sprintf($this->getMessage('url'), $this->cfg->label);
282  return false;
283  }
284  return true;
285  }
286 
294  public function isEmail($val, $prm=null) {
295  if (!filter_var($val, FILTER_VALIDATE_EMAIL)) {
296  $this->errors[] = sprintf($this->getMessage('email'), $this->cfg->label);
297  return false;
298  }
299  return true;
300  }
301 
312  public function isDbUnique($val, array $prm) {
313  if (array_key_exists('value', $prm) && $val == $prm['value'])
314  return true;
315 
316  $table = $prm['table'] instanceof db_table? $prm['table'] : db::get('table', $prm['table']);
317  $nb = $table->count(array(
318  'where'=>array($prm['field']=>$val),
319  'whereOp'=>'LIKE'
320  ));
321  if ($nb > 0) {
322  $this->errors[] = sprintf($this->getMessage('dbUnique'), $val, $this->cfg->label);
323  return false;
324  }
325  return true;
326  }
327 
337  public function isDbExists($val, array $prm) {
338  $table = $prm['table'] instanceof db_table? $prm['table'] : db::get('table', $prm['table']);
339  $nb = $table->count(array(
340  'where'=>array($prm['field']=>$val),
341  'whereOp'=>'LIKE'
342  ));
343  if ($nb == 0) {
344  $this->errors[] = sprintf($this->getMessage('dbExists'), $val, $this->cfg->label);
345  return false;
346  }
347  return true;
348  }
349 
356  protected function getMessage($name) {
357  return utils::htmlOut($this->cfg->getInArray('messages', $name));
358  }
359 
366  public function setMessage($name, $msg) {
367  $this->cfg->setInArray('messages', $name, $msg);
368  }
369 
375  public function getErrors() {
376  return $this->errors;
377  }
378 }
static htmlOut($val, $key=false)
isEmail($val, $prm=null)
isDbExists($val, array $prm)
setMessage($name, $msg)
isIn($val, $prm=null)
getValue()
Definition: valid.class.php:24
isNumeric($val, $prm=null)
isUrl($val, $prm=null)
isValid()
Definition: valid.class.php:85
isRequired($val, $prm=null)
hasRule($type)
Definition: valid.class.php:58
getMessage($name)
delRule($type)
Definition: valid.class.php:67
isDifferent($val, $prm=null)
getRules()
Definition: valid.class.php:76
isEqual($val, $prm=null)
isInt($val, $prm=null)
addRule($type, $prm=null, $msg=null)
Definition: valid.class.php:44
setValue(&$value)
Definition: valid.class.php:33
isAtLeastOneField($val, $prm=null)
static get($type, $table, array $prm=array())
Definition: db.class.php:87
isCallback($val, $prm=null)
isGroupedFields($val, $prm=null)
$f
Definition: list.php:6
getErrors()
isDbUnique($val, array $prm)
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13