Skip to content

Commit 9669458

Browse files
committed
merge master code to branch php5
2 parents 1851daf + 423a04b commit 9669458

32 files changed

+3012
-1359
lines changed

README.md

Lines changed: 301 additions & 156 deletions
Large diffs are not rendered by default.

api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# methods API

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "inhere/php-validate",
33
"type": "library",
44
"description": "a simple validate library of the php",
5-
"keywords": ["library","validate"],
5+
"keywords": ["library", "validate"],
66
"homepage": "http://github.com/inhere/php-validate",
77
"license": "MIT",
88
"authors": [
@@ -17,7 +17,7 @@
1717
},
1818
"autoload": {
1919
"psr-4": {
20-
"inhere\\validate\\" : "src/"
20+
"Inhere\\Validate\\" : "src/"
2121
}
2222
},
2323
"suggest": {

examples/DataModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
class DataModel
99
{
10-
use \inhere\validate\ValidationTrait;
10+
use \Inhere\Validate\ValidationTrait;
1111

1212
protected $data = [];
1313

examples/PageRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Class PageRequest
1111
*/
12-
class PageRequest extends \inhere\validate\Validation
12+
class PageRequest extends \Inhere\Validate\Validation
1313
{
1414
public function rules()
1515
{
@@ -26,7 +26,7 @@ public function rules()
2626
];
2727
}
2828

29-
public function attrTrans()
29+
public function translates()
3030
{
3131
return [
3232
'userId' => '用户Id',

examples/field.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
require __DIR__ . '/simple-loader.php';
4+
5+
$data = [
6+
// 'userId' => 234,
7+
'userId' => 'is not an integer',
8+
'tagId' => '234535',
9+
// 'freeTime' => '1456767657', // filed not exists
10+
'note' => '',
11+
'name' => 'Ajohn',
12+
'existsField' => 'test',
13+
'passwd' => 'password',
14+
'repasswd' => 'repassword',
15+
'insertTime' => '1456767657',
16+
'goods' => [
17+
'apple' => 34,
18+
'pear' => 50,
19+
],
20+
];
21+
22+
$rules = [
23+
['userId', 'required|int'],
24+
['tagId', 'size:0,50'],
25+
];
26+
27+
echo "\n----------------------------\n use FieldValidation\n----------------------------\n\n";
28+
29+
$v = \Inhere\Validate\FieldValidation::make($data, $rules)
30+
->setTranslates([
31+
'goods.pear' => '梨子'
32+
])
33+
->setMessages([
34+
'freeTime.required' => 'freeTime is required!!!!'
35+
])
36+
->validate([], false);
37+
38+
print_r($v->getErrors());

examples/filter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-11-24
6+
* Time: 18:13
7+
*/

examples/index.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
<?php
22

3-
spl_autoload_register(function($class)
4-
{
5-
// e.g. "inhere\validate\ValidationTrait"
6-
if (strpos($class,'\\')) {
7-
$file = dirname(__DIR__) . '/src/' . trim(strrchr($class,'\\'),'\\'). '.php';
8-
} else {
9-
$file = __DIR__ . '/' . $class. '.php';
10-
}
11-
12-
if (is_file($file)) {
13-
include $file;
14-
}
15-
});
3+
require __DIR__ . '/simple-loader.php';
164

175
$data = [
186
// 'userId' => 234,
@@ -23,7 +11,7 @@
2311
'name' => 'Ajohn',
2412
'existsField' => 'test',
2513
'passwd' => 'password',
26-
'repasswd' => 'repassword',
14+
'repasswd' => 'password',
2715
'insertTime' => '1456767657',
2816
'goods' => [
2917
'apple' => 34,
@@ -32,7 +20,7 @@
3220
];
3321

3422
$rules = [
35-
['tagId,userId,freeTime', 'required', 'msg' => '{attr} is required!'],// set message
23+
['tagId,userId,freeTime', 'required'],// set message
3624
['tagId,userId,freeTime', 'number'],
3725
['note', 'email', 'skipOnEmpty' => false], // set skipOnEmpty is false.
3826
['insertTime', 'email', 'scene' => 'otherScene' ],// set scene. will is not validate it on default.
@@ -75,17 +63,23 @@
7563
//$model = new DataModel($_POST,$rules);
7664
$model = new DataModel;
7765
$model->setData($data)->setRules($rules);
66+
$model->setMessages([
67+
'freeTime.required' => 'freeTime is required!!!!'
68+
]);
7869
$model->validate();
7970

8071
print_r($model->getErrors());
8172

8273
echo "\n----------------------------\n use Validation\n----------------------------\n\n";
8374

84-
$valid = \inhere\validate\Validation::make($data, $rules)
85-
->setAttrTrans([
75+
$v = \Inhere\Validate\Validation::make($data, $rules)
76+
->setTranslates([
8677
'goods.pear' => '梨子'
8778
])
79+
->setMessages([
80+
'freeTime.required' => 'freeTime is required!!!!'
81+
])
8882
->validate([], false);
8983

90-
print_r($valid->getErrors());
84+
print_r($v->getErrors());
9185

examples/simple-loader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
error_reporting(E_ALL);
4+
date_default_timezone_set("Asia/Shanghai");
5+
6+
spl_autoload_register(function($class)
7+
{
8+
$prefix = 'Inhere\\Validate\\';
9+
10+
// e.g. "Inhere\Validate\ValidationTrait"
11+
if (strpos($class, $prefix) !== false) {
12+
$file = dirname(__DIR__) . '/src/' . str_replace('\\', '/', substr($class, strlen($prefix))). '.php';
13+
} else {
14+
$file = __DIR__ . '/' . $class. '.php';
15+
}
16+
17+
if (is_file($file)) {
18+
include $file;
19+
}
20+
});

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="./tests/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Validate-Filter Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">./examples</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/AbstractValidation.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: inhere
6+
* Date: 2017-11-07
7+
* Time: 10:04
8+
*/
9+
10+
namespace Inhere\Validate;
11+
12+
/**
13+
* Class AbstractValidation
14+
* - one field to many rules. like Laravel framework
15+
* ```php
16+
* [
17+
* ['field1', 'rule1, rule2, ...', ...],
18+
* ['field2', 'rule1, rule3, ...', ...],
19+
* ]
20+
* ```
21+
* @package Inhere\Validate
22+
*/
23+
abstract class AbstractValidation implements ValidationInterface
24+
{
25+
use ValidationTrait {
26+
//set as traitSet;
27+
get as traitGet;
28+
}
29+
/**
30+
* @var array
31+
*/
32+
protected $data = [];
33+
34+
/**
35+
* @param array $data
36+
* @param array $rules
37+
* @param array $translates
38+
* @param string $scene
39+
* @param bool $startValidate 立即开始验证
40+
* @throws \InvalidArgumentException
41+
* @throws \RuntimeException
42+
*/
43+
public function __construct(array $data = [], array $rules = [], array $translates = [], $scene = '', $startValidate = false)
44+
{
45+
$this->data = $data;
46+
$this->setRules($rules)->setScene($scene)->setTranslates($translates);
47+
if ($startValidate) {
48+
$this->validate();
49+
}
50+
}
51+
52+
/**
53+
* @param array $data
54+
* @param array $rules
55+
* @param array $translates
56+
* @param string $scene
57+
* @param bool $startValidate 立即开始验证
58+
* @return static
59+
* @throws \InvalidArgumentException
60+
* @throws \RuntimeException
61+
*/
62+
public static function make(array $data, array $rules = [], array $translates = [], $scene = '', $startValidate = false)
63+
{
64+
return new static($data, $rules, $translates, $scene, $startValidate);
65+
}
66+
}

src/FieldValidation.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: inhere
6+
* Date: 2017-11-07
7+
* Time: 10:04
8+
*/
9+
10+
namespace Inhere\Validate;
11+
12+
/**
13+
* Class FieldValidation
14+
* - one field to many rules. like Laravel framework
15+
* ```php
16+
* $vd = FieldValidation::make($data, $rules, ...);
17+
* $vd->validate();
18+
* ```
19+
* @package Inhere\Validate
20+
*/
21+
class FieldValidation extends AbstractValidation
22+
{
23+
/**
24+
* @return array
25+
*/
26+
public function rules()
27+
{
28+
return [];
29+
}
30+
31+
/**
32+
* @return \Generator
33+
* @throws \InvalidArgumentException
34+
*/
35+
protected function collectRules()
36+
{
37+
$scene = $this->scene;
38+
// 循环规则, 搜集当前场景可用的规则
39+
foreach ($this->getRules() as $rule) {
40+
// check field
41+
if (!isset($rule[0]) || !$rule[0]) {
42+
throw new \InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0].');
43+
}
44+
// check validators
45+
if (!isset($rule[1]) || !$rule[1]) {
46+
throw new \InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
47+
}
48+
// an rule for special scene.
49+
if (!empty($rule['on'])) {
50+
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
51+
if ($scene && !\in_array($scene, $sceneList, true)) {
52+
continue;
53+
}
54+
unset($rule['on']);
55+
$this->_usedRules[] = $rule;
56+
}
57+
$field = array_shift($rule);
58+
if (\is_object($rule[0])) {
59+
(yield $field => $rule);
60+
} else {
61+
// 'required|string:5,10;' OR 'required|in:5,10'
62+
$rules = \is_array($rule[0]) ? $rule[0] : array_map('trim', explode('|', $rule[0]));
63+
foreach ($rules as $aRule) {
64+
$rule = $this->parseRule($aRule, $rule);
65+
(yield $field => $rule);
66+
}
67+
}
68+
}
69+
70+
yield [];
71+
}
72+
73+
/**
74+
* @param string $rule
75+
* @param array $row
76+
* @return array
77+
*/
78+
protected function parseRule($rule, $row)
79+
{
80+
$rule = trim($rule, ': ');
81+
if (false === strpos($rule, ':')) {
82+
$row[0] = $rule;
83+
84+
return $row;
85+
}
86+
list($name, $args) = explode(':', $rule, 2);
87+
$args = trim($args, ', ');
88+
$row[0] = $name;
89+
switch ($name) {
90+
case 'in':
91+
case 'enum':
92+
case 'ontIn':
93+
$row[] = array_map('trim', explode(',', $args));
94+
break;
95+
case 'size':
96+
case 'range':
97+
case 'string':
98+
case 'between':
99+
if (strpos($args, ',')) {
100+
list($row['min'], $row['max']) = array_map('trim', explode(',', $args, 2));
101+
} else {
102+
$row['min'] = $args;
103+
}
104+
break;
105+
default:
106+
$args = strpos($args, ',') ? array_map('trim', explode(',', $args)) : [$args];
107+
$row = array_merge($row, $args);
108+
break;
109+
}
110+
111+
return $row;
112+
}
113+
}

0 commit comments

Comments
 (0)