mirror of
https://github.com/OpenXE-org/OpenXE.git
synced 2025-02-13 13:10:09 +01:00
45 lines
1.0 KiB
PHP
45 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace Rakit\Validation\Rules;
|
|
|
|
use Rakit\Validation\Rule;
|
|
use InvalidArgumentException;
|
|
use Closure;
|
|
|
|
class Callback extends Rule
|
|
{
|
|
|
|
protected $message = "The :attribute is not valid";
|
|
|
|
protected $fillable_params = ['callback'];
|
|
|
|
public function setCallback(Closure $callback)
|
|
{
|
|
return $this->setParameter('callback', $callback);
|
|
}
|
|
|
|
public function check($value)
|
|
{
|
|
$this->requireParameters($this->fillable_params);
|
|
|
|
$callback = $this->parameter('callback');
|
|
if (false === $callback instanceof Closure) {
|
|
$key = $this->attribute->getKey();
|
|
throw new InvalidArgumentException("Callback rule for '{$key}' is not callable.");
|
|
}
|
|
|
|
$callback = $callback->bindTo($this);
|
|
$invalidMessage = $callback($value);
|
|
|
|
if (is_string($invalidMessage)) {
|
|
$this->setMessage($invalidMessage);
|
|
return false;
|
|
} elseif(false === $invalidMessage) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|