Skip to content

Commit def5ca5

Browse files
authored
Merge pull request #72 from swaggest/regex-delim
Improve regex delimiters, fixes #71
2 parents d404217 + a3942df commit def5ca5

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

src/Constraint/Format.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,7 @@ public static function regexError($data)
108108
return 'Invalid regex: \A is not supported';
109109
}
110110

111-
112-
$d = null;
113-
foreach (array('/', '_', '~', '#', '!', '%', '`', '=') as $delimiter) {
114-
if (strpos($data, $delimiter) === false) {
115-
$d = $delimiter;
116-
break;
117-
}
118-
}
119-
return @preg_match($d . $data . $d, '') === false ? 'Invalid regex: ' . $data : null;
111+
return @preg_match('{' . $data . '}', '') === false ? 'Invalid regex: ' . $data : null;
120112
}
121113

122114
public static function jsonPointerError($data, $isRelative = false)

src/Helper.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,11 @@ class Helper
77
{
88
/**
99
* @param string $jsonPattern
10-
* @return bool|string
11-
* @throws InvalidValue
10+
* @return string
1211
*/
1312
public static function toPregPattern($jsonPattern)
1413
{
15-
static $delimiters = array('/', '#', '+', '~', '%');
16-
17-
$pattern = false;
18-
foreach ($delimiters as $delimiter) {
19-
if (strpos($jsonPattern, $delimiter) === false) {
20-
$pattern = $delimiter . $jsonPattern . $delimiter . 'u';
21-
break;
22-
}
23-
}
24-
25-
if (false === $pattern) {
26-
throw new InvalidValue('Failed to prepare preg pattern');
27-
}
28-
29-
return $pattern;
14+
return '{' . $jsonPattern . '}u';
3015
}
3116

3217
/**

src/Schema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,8 @@ public function makeObjectItem(Context $options = null)
13101310
}
13111311

13121312
/**
1313+
* Resolves boolean schema into Schema instance
1314+
*
13131315
* @param mixed $schema
13141316
* @return mixed|Schema
13151317
*/

tests/src/PHPUnit/Misc/PreparePatternTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Swaggest\JsonSchema\Tests\PHPUnit\Misc;
44

55
use Swaggest\JsonSchema\Helper;
6-
use Swaggest\JsonSchema\InvalidValue;
76

87
class PreparePatternTest extends \PHPUnit_Framework_TestCase
98
{
10-
public function testFailedToPreparePattern()
9+
public function testPreparePatternForEmail()
1110
{
12-
$this->setExpectedException(get_class(new InvalidValue()), 'Failed to prepare preg pattern');
13-
Helper::toPregPattern('/#+~%');
11+
$pattern = Helper::toPregPattern('^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$');
12+
$this->assertEquals(1, preg_match($pattern, '[email protected]'));
13+
$this->assertEquals(0, preg_match($pattern, "malformed-email"));
1414
}
1515
}

0 commit comments

Comments
 (0)