blob: 611a45e99fe6dec22a08a96ef739dc44d6de7191 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?php
namespace ipl\Validator;
/**
* Validator that uses a callback for the actual validation
*
* # Example Usage
* ```
* $dedup = new CallbackValidator(function ($value, CallbackValidator $validator) {
* if (already_exists_in_database($value)) {
* $validator->addMessage('Record already exists in database');
*
* return false;
* }
*
* return true;
* });
*
* $dedup->isValid($id);
* ```
*/
class CallbackValidator extends BaseValidator
{
/** @var callable Validation callback */
protected $callback;
/**
* Create a new callback validator
*
* @param callable $callback Validation callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}
public function isValid($value)
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();
return call_user_func($this->callback, $value, $this);
}
}
|