Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: RunnerLee/validator
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.0
Choose a base ref
...
head repository: RunnerLee/validator
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Mar 4, 2019

  1. update

    RunnerLee committed Mar 4, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    57c0d10 View commit details
  2. update changelog

    RunnerLee committed Mar 4, 2019
    Copy the full SHA
    c8c8281 View commit details
Showing with 66 additions and 26 deletions.
  1. +5 −1 CHANGELOG.md
  2. +15 −5 src/Validator.php
  3. +20 −20 src/messages/en.php
  4. +26 −0 tests/ValidatorTest.php
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -9,4 +9,8 @@
### Changed
- 拆分 validate 及消息处理为 trait
- extend 的 callback 类作用域取消绑定到 Validator 示例. validate 函数参数增加传入 Validator 实例
- validate 从 protected 改为 publish
- validate 从 protected 改为 publish

## [v1.0.1] - 2019-03-04
### Added
- 增加支持通过魔术方法调用扩展规则
20 changes: 15 additions & 5 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

namespace Runner\Validator;

use BadMethodCallException;
use Runner\Validator\Concerns\MessagesAttributes;
use Runner\Validator\Concerns\ValidatesAttributes;

@@ -88,14 +89,13 @@ public function validate()
if ($this->hasField($field)) {
$value = $this->getField($field);
foreach ($rules as $rule => $parameters) {
if (!$this->runValidateRule($field, $value, $rule, $parameters)) {
if (!$this->runValidateRule($rule, $field, $value, $parameters)) {
$this->messages[$field][$rule] = $this->buildMessage($rule, $field, $parameters);
}
}
} elseif ($forceRules = array_intersect(self::$forceRules, array_keys($rules))) {
$value = null;
foreach ($forceRules as $rule) {
if (!$this->runValidateRule($field, null, $rule, $rules[$rule])) {
if (!$this->runValidateRule($rule, $field, null, $rules[$rule])) {
$this->messages[$field][$rule] = $this->buildMessage($rule, $field, $rules[$rule]);
}
}
@@ -143,7 +143,6 @@ protected function parseRules(array $ruleGroups)
$this->ruleGroups[$field][$rule] = ('' === $parameters ? [] : explode(',', $parameters));
}
}
unset($map);
}

/**
@@ -213,10 +212,21 @@ protected function getField($field)
*
* @return bool
*/
protected function runValidateRule($field, $value, $rule, array $parameters = [])
protected function runValidateRule($rule, $field, $value, array $parameters = [])
{
$callback = array_key_exists($rule, self::$extensions) ? self::$extensions[$rule] : [$this, "validate{$rule}"];

return (bool) call_user_func($callback, $field, $value, $parameters, $this);
}

public function __call($method, $arguments)
{
$rule = self::formatRuleName(substr($method, 8));

if (!isset(self::$extensions[$rule])) {
throw new BadMethodCallException(sprintf('Method %s::%s does not exists', static::class, $method));
}

return $this->runValidateRule($rule, ...$arguments);
}
}
40 changes: 20 additions & 20 deletions src/messages/en.php
Original file line number Diff line number Diff line change
@@ -6,25 +6,25 @@
*/

return [
'Required' => 'is required',
'Accepted' => 'must be accepted',
'Numeric' => 'must be numeric',
'Integer' => 'must be an integer',
'Float' => 'must be a float',
'Size' => "'s size must be %s",
'Min' => 'must be at least %d',
'Max' => 'must be no more than %d',
'In' => 'contains invalid value',
'Ip' => 'is not a valid IP address',
'Email' => 'is not a valid email address',
'Url' => 'is not a valid URL',
'Regex' => 'contains invalid characters',
'Date' => 'is not a valid date',
'Required' => 'is required',
'Accepted' => 'must be accepted',
'Numeric' => 'must be numeric',
'Integer' => 'must be an integer',
'Float' => 'must be a float',
'Size' => "'s size must be %s",
'Min' => 'must be at least %d',
'Max' => 'must be no more than %d',
'In' => 'contains invalid value',
'Ip' => 'is not a valid IP address',
'Email' => 'is not a valid email address',
'Url' => 'is not a valid URL',
'Regex' => 'contains invalid characters',
'Date' => 'is not a valid date',
'DateFormat' => "must be date with format '%s'",
'Boolean' => 'must be a boolean',
'Range' => 'must be between %d and %d characters',
'Confirm' => 'must be same as %s field',
'Array' => 'must be an array',
'String' => 'must be a string',
'Json' => 'must be a json string',
'Boolean' => 'must be a boolean',
'Range' => 'must be between %d and %d characters',
'Confirm' => 'must be same as %s field',
'Array' => 'must be an array',
'String' => 'must be a string',
'Json' => 'must be a json string',
];
26 changes: 26 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -89,6 +89,32 @@ function () {
);
}

public function testAddExtension()
{
Validator::addExtension('runner', function () {
return true;
});

$data = [
'a' => 'string',
];

$validator = new Validator($data, [
'a' => 'runner'
]);

$this->assertSame(true, $validator->validate());

$this->assertSame(true, $validator->validateRunner('a', 'a', []));

$validator = new Validator($data, [
'a' => 'demo',
]);

$this->expectException(BadMethodCallException::class);
$validator->validate();
}

public function testReplaceMessage()
{
$validator = new Validator(