12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php namespace System\Validation;
- use System\Str;
- abstract class Nullable_Rule extends Rule {
-
- public $allow_empty = false;
-
- public $allow_null = false;
-
- public function check($attribute, $attributes)
- {
-
-
-
-
-
- if ( ! array_key_exists($attribute, $attributes))
- {
- if ( ! $this->allow_null)
- {
- $this->error = 'presence_of';
- }
- return is_null($this->error);
- }
-
-
-
-
-
- elseif (Str::length((string) $attributes[$attribute]) == 0 and ! $this->allow_empty)
- {
- $this->error = 'presence_of';
- return false;
- }
- }
-
- public function not_required()
- {
- return $this->allow_empty()->allow_null();
- }
-
- public function allow_empty()
- {
- $this->allow_empty = true;
- return $this;
- }
-
- public function allow_null()
- {
- $this->allow_null = true;
- return $this;
- }
- }
|