Skip to content

Commit

Permalink
up: update some logic support deep path after wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2020
1 parent e900a44 commit e75ec76
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 100 deletions.
14 changes: 7 additions & 7 deletions src/AbstractValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class AbstractValidation implements ValidationInterface
* @param array $rules
* @param array $translates
* @param string $scene
* @param bool $startValidate 立即开始验证
* @param bool $startValidate Start verification now
*
* @throws InvalidArgumentException
*/
Expand All @@ -57,9 +57,9 @@ public function __construct(
* @param string $scene
* @param bool $startValidate
*
* @return AbstractValidation
* @return self
*/
public static function quick(array $data, string $scene = '', bool $startValidate = false): AbstractValidation
public static function quick(array $data, string $scene = '', bool $startValidate = false): self
{
return new static($data, [], [], $scene, $startValidate);
}
Expand All @@ -80,7 +80,7 @@ public static function make(
array $translates = [],
string $scene = '',
bool $startValidate = false
) {
): self {
return new static($data, $rules, $translates, $scene, $startValidate);
}

Expand All @@ -95,13 +95,13 @@ public static function make(
* @return static
* @throws InvalidArgumentException
*/
public static function makeAndValidate(array $data, array $rules = [], array $translates = [], string $scene = '')
public static function makeAndValidate(array $data, array $rules = [], array $translates = [], string $scene = ''): self
{
return new static($data, $rules, $translates, $scene, true);
}

/**
* Create and start verification immediately
* Create and start verification immediately. alias of makeAndValidate()
*
* @param array $data
* @param array $rules
Expand All @@ -111,7 +111,7 @@ public static function makeAndValidate(array $data, array $rules = [], array $tr
* @return static
* @throws InvalidArgumentException
*/
public static function check(array $data, array $rules = [], array $translates = [], string $scene = '')
public static function check(array $data, array $rules = [], array $translates = [], string $scene = ''): self
{
return new static($data, $rules, $translates, $scene, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/FieldValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class FieldValidation extends AbstractValidation
{
use MultipleRulesTrait;

/*
/* examples:
public function rules()
{
return [
['field', 'required|string:5,10|...', ...],
['field0', ['required', 'string:5,10'], ...],
['field1', 'rule1|rule2|...', ...],
['field2', 'rule1|rule3|...', ...],
['field3', function($val) {}, ...],
];
}
*/
Expand Down
28 changes: 27 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function function_exists;
use function gettype;
use function html_entity_decode;
use function in_array;
use function is_array;
use function is_int;
use function is_object;
Expand Down Expand Up @@ -207,6 +208,31 @@ public static function prettifyFieldName(string $field): string
return strpos($str, '_') ? str_replace('_', ' ', $str) : $str;
}

/**
* @param string $scene Current scene value
* @param string|array $ruleOn
*
* @return bool
*/
public static function ruleIsAvailable(string $scene, $ruleOn): bool
{
// - rule is not limit scene
if (!$ruleOn) {
return true;
}

// - $ruleOn is not empty

// current scene is empty
if (!$scene) {
return false;
}

$scenes = is_string($ruleOn) ? Filters::explode($ruleOn) : (array)$ruleOn;

return in_array($scene, $scenes, true);
}

/**
* getValueOfArray 支持以 '.' 分割进行子级值获取 eg: 'goods.apple'
*
Expand Down Expand Up @@ -256,7 +282,7 @@ public static function call($cb, ...$args)
// className::method
if (strpos($cb, '::') > 0) {
$cb = explode('::', $cb, 2);
// function
// function
} elseif (function_exists($cb)) {
return $cb(...$args);
}
Expand Down
20 changes: 7 additions & 13 deletions src/Traits/MultipleRulesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@

use Generator;
use Inhere\Validate\Filter\Filters;
use Inhere\Validate\Helper;
use InvalidArgumentException;
use function array_map;
use function array_merge;
use function array_shift;
use function explode;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function strpos;
use function trim;

/**
* Trait MultipleRulesTrait - allow add multiple rules like Laravel.
* Trait MultipleRulesTrait
* - allow add multiple rules like Laravel.
*
* @package Inhere\Validate\Traits
*/
Expand Down Expand Up @@ -62,17 +62,12 @@ protected function collectRules(): ?Generator

// check validators
if (!isset($rule[1]) || !$rule[1]) {
throw new InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1]');
throw new InvalidArgumentException('Please setting the validator(s) for validate field! position: rule[1]');
}

// an rule for special scene.
if (!empty($rule['on'])) {
if (!$scene) {
continue;
}

$sceneList = is_string($rule['on']) ? Filters::explode($rule['on']) : (array)$rule['on'];
if (!in_array($scene, $sceneList, true)) {
// rule only allow use to special scene.
if (isset($rule['on'])) {
if (!Helper::ruleIsAvailable($scene, $rule['on'])) {
continue;
}

Expand Down Expand Up @@ -106,7 +101,6 @@ protected function collectRules(): ?Generator
protected function parseRule(string $rule, array $row): array
{
$rule = trim($rule, ': ');

if (false === strpos($rule, ':')) {
$row[0] = $rule;
return $row;
Expand Down
37 changes: 32 additions & 5 deletions src/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Inhere\Validate;

use function array_merge;
use function is_int;
use function is_numeric;

/**
* Class Valid - Simple Data Validator
Expand Down Expand Up @@ -54,21 +56,46 @@ public function getData(): array
*/
public static function getInt(string $field, int $min = null, int $max = null, int $default = null): int
{
return 0;
if (!isset(self::$data[$field])) {
if ($default === null) {
throw new ValidateException($field, 'is required and must be int');
}

return $default;
}

$val = self::$data[$field];
if (is_numeric($val)) {
$val = (int)$val;
}

if (!is_int($val)) {
throw new ValidateException($field, 'must be int value');
}

// check min and max value
if ($min !== null && $val < $min) {
throw new ValidateException($field, "must be greater or equal to $min");
}
if ($max !== null && $val > $max) {
throw new ValidateException($field, "must be less than or equal to $max");
}

return $val;
}

public static function getInts(string $field, int $min = null, int $max = null, int $default = 0): int
public static function getInts(string $field, int $min = null, int $max = null, array $default = null): array
{
return 0;
return [];
}

public static function getString(string $field, int $minLen = null, int $maxLen = null, string $default = null): int
{
return 0;
}

public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): int
public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): array
{
return 0;
return [];
}
}
29 changes: 29 additions & 0 deletions src/ValidateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,33 @@
*/
class ValidateException extends RuntimeException
{
/**
* @var string
*/
public $field;

/**
* @param string $field
* @param string $message
*
* @return static
*/
public static function create(string $field, string $message): self
{
return new self($field, $message);
}

/**
* Class constructor.
*
* @param string $field
* @param string $message
*/
public function __construct(string $field, string $message)
{
parent::__construct($field . ' ' . $message, 500);

// save field
$this->field = $field;
}
}
10 changes: 4 additions & 6 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
* ['userId', 'number'],
* ['name', 'regexp' ,'/^[a-z]\w{2,12}$/'],
* ])->validate();
* $vd->fail();// bool
*
* $vd->isFail();// bool
* $vd->firstError(); // get first error message.
* $vd->passed();// bool
* $vd->isOk();// bool
*/
class Validation extends AbstractValidation
{
/**
* @return array
*/
/*
/* examples:
public function rules()
{
return [
Expand Down
Loading

0 comments on commit e75ec76

Please sign in to comment.