From a3942df5a76dd9ee91e59105774d36df039c5c2a Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Tue, 16 Apr 2019 17:39:04 +0200 Subject: [PATCH] Improve regex delimiters, fixes #71 --- src/Constraint/Format.php | 10 +--------- src/Helper.php | 19 ++----------------- src/Schema.php | 2 ++ tests/src/PHPUnit/Misc/PreparePatternTest.php | 8 ++++---- 4 files changed, 9 insertions(+), 30 deletions(-) diff --git a/src/Constraint/Format.php b/src/Constraint/Format.php index 3db4fdf..a37ef6f 100644 --- a/src/Constraint/Format.php +++ b/src/Constraint/Format.php @@ -108,15 +108,7 @@ public static function regexError($data) return 'Invalid regex: \A is not supported'; } - - $d = null; - foreach (array('/', '_', '~', '#', '!', '%', '`', '=') as $delimiter) { - if (strpos($data, $delimiter) === false) { - $d = $delimiter; - break; - } - } - return @preg_match($d . $data . $d, '') === false ? 'Invalid regex: ' . $data : null; + return @preg_match('{' . $data . '}', '') === false ? 'Invalid regex: ' . $data : null; } public static function jsonPointerError($data, $isRelative = false) diff --git a/src/Helper.php b/src/Helper.php index f80ad17..a3d2a80 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -7,26 +7,11 @@ class Helper { /** * @param string $jsonPattern - * @return bool|string - * @throws InvalidValue + * @return string */ public static function toPregPattern($jsonPattern) { - static $delimiters = array('/', '#', '+', '~', '%'); - - $pattern = false; - foreach ($delimiters as $delimiter) { - if (strpos($jsonPattern, $delimiter) === false) { - $pattern = $delimiter . $jsonPattern . $delimiter . 'u'; - break; - } - } - - if (false === $pattern) { - throw new InvalidValue('Failed to prepare preg pattern'); - } - - return $pattern; + return '{' . $jsonPattern . '}u'; } /** diff --git a/src/Schema.php b/src/Schema.php index 6c6267b..99a1dc9 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -1310,6 +1310,8 @@ public function makeObjectItem(Context $options = null) } /** + * Resolves boolean schema into Schema instance + * * @param mixed $schema * @return mixed|Schema */ diff --git a/tests/src/PHPUnit/Misc/PreparePatternTest.php b/tests/src/PHPUnit/Misc/PreparePatternTest.php index a23a4e0..8bcf6fa 100644 --- a/tests/src/PHPUnit/Misc/PreparePatternTest.php +++ b/tests/src/PHPUnit/Misc/PreparePatternTest.php @@ -3,13 +3,13 @@ namespace Swaggest\JsonSchema\Tests\PHPUnit\Misc; use Swaggest\JsonSchema\Helper; -use Swaggest\JsonSchema\InvalidValue; class PreparePatternTest extends \PHPUnit_Framework_TestCase { - public function testFailedToPreparePattern() + public function testPreparePatternForEmail() { - $this->setExpectedException(get_class(new InvalidValue()), 'Failed to prepare preg pattern'); - Helper::toPregPattern('/#+~%'); + $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])?)*$'); + $this->assertEquals(1, preg_match($pattern, 'name@host.com')); + $this->assertEquals(0, preg_match($pattern, "malformed-email")); } } \ No newline at end of file