Skip to content

Commit cd2a4b1

Browse files
author
Tomasz Ferfecki
committed
Regex rule - unexpected warning with comma character
1 parent 82ad1be commit cd2a4b1

File tree

2 files changed

+77
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)