Skip to content

Commit 218c804

Browse files
authored
Merge pull request #24 from Vandervir/master
Regex rule - unexpected warning with comma character. Fixes #23
2 parents 82ad1be + 204372c commit 218c804

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/Validation.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@ protected function parseRule($rule)
344344
{
345345
$exp = explode(':', $rule, 2);
346346
$rulename = $exp[0];
347-
$params = isset($exp[1])? explode(',', $exp[1]) : [];
347+
if ($rulename !== 'regex') {
348+
$params = isset($exp[1])? explode(',', $exp[1]) : [];
349+
} else {
350+
$params = [$exp[1]];
351+
}
352+
348353
return [$rulename, $params];
349354
}
350355

tests/ValidatonTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Rakit\Validation\Validation;
4+
use Rakit\Validation\Validator;
5+
6+
class ValidatonTest extends PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @param string $rules
10+
* @param array $expectedResult
11+
*
12+
* @dataProvider parseRuleProvider
13+
*/
14+
public function testParseRule($rules, $expectedResult)
15+
{
16+
$class = new ReflectionClass(Validation::class);
17+
$method = $class->getMethod('parseRule');
18+
$method->setAccessible(true);
19+
20+
$validation = new Validation(new Validator(), [], []);
21+
22+
$result = $method->invokeArgs($validation, [$rules]);
23+
$this->assertSame($expectedResult, $result);
24+
}
25+
26+
/**
27+
* @return array
28+
*/
29+
public function parseRuleProvider()
30+
{
31+
return [
32+
[
33+
'email',
34+
[
35+
'email',
36+
[],
37+
],
38+
],
39+
[
40+
'min:6',
41+
[
42+
'min',
43+
['6'],
44+
],
45+
],
46+
[
47+
'uploaded_file:0,500K,png,jpeg',
48+
[
49+
'uploaded_file',
50+
['0', '500K', 'png', 'jpeg'],
51+
],
52+
],
53+
[
54+
'same:password',
55+
[
56+
'same',
57+
['password'],
58+
],
59+
],
60+
[
61+
'regex:/^([a-zA-Z\,]*)$/',
62+
[
63+
'regex',
64+
['/^([a-zA-Z\,]*)$/'],
65+
],
66+
],
67+
];
68+
}
69+
}

0 commit comments

Comments
 (0)