| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Validator; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use LogicException; |
| 7 |
use ReflectionClass; |
| 8 |
use BadMethodCallException; |
| 9 |
use InvalidArgumentException; |
| 10 |
use FluentBoards\Framework\Support\Str; |
| 11 |
use FluentBoards\Framework\Foundation\App; |
| 12 |
use FluentBoards\Framework\Validator\Rules\In; |
| 13 |
use FluentBoards\Framework\Validator\Rules\NotIn; |
| 14 |
use FluentBoards\Framework\Validator\Rules\Unique; |
| 15 |
use FluentBoards\Framework\Validator\Rules\Exists; |
| 16 |
use FluentBoards\Framework\Validator\Rules\RequiredIf; |
| 17 |
use FluentBoards\Framework\Validator\Rules\Dimensions; |
| 18 |
use FluentBoards\Framework\Validator\Rules\ConditionalRules; |
| 19 |
use FluentBoards\Framework\Support\ArrayableInterface; |
| 20 |
|
| 21 |
class Rule |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Create a new conditional rule set. |
| 25 |
* |
| 26 |
* @param callable|bool $condition |
| 27 |
* @param array|string $rules |
| 28 |
* @param array|string $defaultRules |
| 29 |
* @return \FluentBoards\Framework\Validator\Rules\ConditionalRules |
| 30 |
*/ |
| 31 |
public static function when($condition, $rules, $defaultRules = []) |
| 32 |
{ |
| 33 |
return new ConditionalRules($condition, $rules, $defaultRules); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get a dimensions constraint builder instance. |
| 38 |
* |
| 39 |
* @param array $constraints |
| 40 |
* @return \FluentBoards\Framework\Validator\Rules\Dimensions |
| 41 |
*/ |
| 42 |
public static function dimensions(array $constraints = []) |
| 43 |
{ |
| 44 |
return new Dimensions($constraints); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get an exists constraint builder instance. |
| 49 |
* |
| 50 |
* @param string $table |
| 51 |
* @param string $column |
| 52 |
* @return \FluentBoards\Framework\Validator\Rules\Exists |
| 53 |
*/ |
| 54 |
public static function exists($table, $column = 'NULL') |
| 55 |
{ |
| 56 |
return new Exists($table, $column); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get an in constraint builder instance. |
| 61 |
* |
| 62 |
* @param \Illuminate\Contracts\Support\Arrayable|array|string $values |
| 63 |
* @return \FluentBoards\Framework\Validator\Rules\In |
| 64 |
*/ |
| 65 |
public static function in($values) |
| 66 |
{ |
| 67 |
if ($values instanceof ArrayableInterface) { |
| 68 |
$values = $values->toArray(); |
| 69 |
} |
| 70 |
|
| 71 |
return new In(is_array($values) ? $values : func_get_args()); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get a not_in constraint builder instance. |
| 76 |
* |
| 77 |
* @param \Illuminate\Contracts\Support\Arrayable|array|string $values |
| 78 |
* @return \FluentBoards\Framework\Validator\Rules\NotIn |
| 79 |
*/ |
| 80 |
public static function notIn($values) |
| 81 |
{ |
| 82 |
if ($values instanceof ArrayableInterface) { |
| 83 |
$values = $values->toArray(); |
| 84 |
} |
| 85 |
|
| 86 |
return new NotIn(is_array($values) ? $values : func_get_args()); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get a required_if constraint builder instance. |
| 91 |
* |
| 92 |
* @param callable|bool $callback |
| 93 |
* @return \FluentBoards\Framework\Validator\Rules\RequiredIf |
| 94 |
*/ |
| 95 |
public static function requiredIf($callback) |
| 96 |
{ |
| 97 |
return new RequiredIf($callback); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get a unique constraint builder instance. |
| 102 |
* |
| 103 |
* @param string $table |
| 104 |
* @param string $column |
| 105 |
* @return \FluentBoards\Framework\Validator\Rules\Unique |
| 106 |
*/ |
| 107 |
public static function unique($table, $column = 'NULL') |
| 108 |
{ |
| 109 |
return new Unique($table, $column); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add a custom rule. |
| 114 |
* |
| 115 |
* @param string $rule |
| 116 |
* @param callable $callback |
| 117 |
* @return null |
| 118 |
* @throws InvalidArgumentException|LogicException |
| 119 |
*/ |
| 120 |
public static function add($rule, $callback = null) |
| 121 |
{ |
| 122 |
if (is_null($callback)) { |
| 123 |
|
| 124 |
$callback = $rule; |
| 125 |
|
| 126 |
$rule = $msg = null; |
| 127 |
|
| 128 |
if (is_string($callback)) { |
| 129 |
|
| 130 |
$rule = explode('\\', $callback); |
| 131 |
|
| 132 |
$rule = Str::snake(end($rule)); |
| 133 |
|
| 134 |
} elseif (is_object($callback)) { |
| 135 |
|
| 136 |
if ($callback instanceof Closure) { |
| 137 |
$msg = 'A rule name is required for a closure based rule'; |
| 138 |
} elseif ((new ReflectionClass($callback))->isAnonymous()) { |
| 139 |
$msg = 'A rule name is required for an anonymous class based rule'; |
| 140 |
} else { |
| 141 |
$rule = get_class($callback); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if($msg && !$rule) throw new InvalidArgumentException($msg, 500); |
| 146 |
} |
| 147 |
|
| 148 |
$classExists = false; |
| 149 |
|
| 150 |
if (is_string($callback)) { |
| 151 |
$classExists = class_exists($callback); |
| 152 |
} |
| 153 |
|
| 154 |
$methodExists = $classExists && method_exists($callback, '__invoke'); |
| 155 |
|
| 156 |
if (!($classExists && $methodExists) && !is_callable($callback)) { |
| 157 |
$m = 'The given callback is not callable'; |
| 158 |
|
| 159 |
if (is_object($callback) || ($classExists && !$methodExists)) { |
| 160 |
$m .= ' and must implement the __invoke magic method.'; |
| 161 |
} |
| 162 |
|
| 163 |
throw new LogicException($m, 500); |
| 164 |
} |
| 165 |
|
| 166 |
App::make('validator')->extend($rule, $callback); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Handle dynamic calls |
| 171 |
* |
| 172 |
* @param string $method |
| 173 |
* @param array $params |
| 174 |
* @return bool/true |
| 175 |
* @throws BadMethodCallException |
| 176 |
*/ |
| 177 |
public static function __callStatic($method, $params) |
| 178 |
{ |
| 179 |
$method = Str::studly($method); |
| 180 |
|
| 181 |
if ($customRules = App::make('validator')->getExtentions()) { |
| 182 |
|
| 183 |
if (in_array($method, array_keys($customRules))) { |
| 184 |
return Str::snake($method) . ':' . implode(',', $params); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
throw new BadMethodCallException( |
| 189 |
'Call to undefined method '. __CLASS__ . ':'. $method, 500 |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
|