Skip to content

Commit 8c9d6ab

Browse files
Add Preg::isMatch and Preg::isMatchAll (#4)
1 parent bb6cb84 commit 8c9d6ab

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ the fact that they can only be strings (for replace), ints (for match) or arrays
6464
Similarly, due to type safety requirements matching using PREG_OFFSET_CAPTURE is made available via
6565
`matchWithOffset` and `matchAllWithOffset`. You cannot pass the flag to `match`/`matchAll`.
6666

67+
Additionally the `Preg` class provides match methods that return `bool` rather than `int`, for stricter type safety
68+
when the number of pattern matches is not useful:
69+
70+
```php
71+
use Composer\Pcre\Preg;
72+
73+
if (Preg::isMatch('{fo+}', $string, $matches)) // bool
74+
if (Preg::isMatchAll('{fo+}', $string, $matches)) // bool
75+
```
76+
6777
If you would prefer a slightly more verbose usage, replacing by-ref arguments by result objects, you can use the `Regex` class:
6878

6979
```php

src/Preg.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,30 @@ public static function grep($pattern, array $array, $flags = 0)
214214

215215
return $result;
216216
}
217+
218+
/**
219+
* @param string $pattern
220+
* @param string $subject
221+
* @param array<string|null> $matches Set by method
222+
* @param int $flags PREG_UNMATCHED_AS_NULL, only available on PHP 7.2+
223+
* @param int $offset
224+
* @return bool
225+
*/
226+
public static function isMatch($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
227+
{
228+
return (bool) static::match($pattern, $subject, $matches, $flags, $offset);
229+
}
230+
231+
/**
232+
* @param string $pattern
233+
* @param string $subject
234+
* @param array<int|string, list<string|null>> $matches Set by method
235+
* @param int $flags PREG_UNMATCHED_AS_NULL, only available on PHP 7.2+
236+
* @param int $offset
237+
* @return bool
238+
*/
239+
public static function isMatchAll($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
240+
{
241+
return (bool) static::matchAll($pattern, $subject, $matches, $flags, $offset);
242+
}
217243
}

tests/PregTests/IsMatchAllTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of composer/pcre.
5+
*
6+
* (c) Composer <https://github.com/composer>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Composer\Pcre\PregTests;
13+
14+
use Composer\Pcre\BaseTestCase;
15+
use Composer\Pcre\Preg;
16+
17+
class IsMatchAllTest extends BaseTestCase
18+
{
19+
/**
20+
* This can be replaced with a setUp() method when appropriate
21+
*
22+
* @before
23+
* @return void
24+
*/
25+
public function registerFunctionName()
26+
{
27+
$this->pregFunction = 'preg_match_all()';
28+
}
29+
30+
/**
31+
* @return void
32+
*/
33+
public function testSuccess()
34+
{
35+
$result = Preg::isMatchAll('{[aei]}', 'abcdefghijklmnopqrstuvwxyz', $matches);
36+
$this->assertSame(true, $result);
37+
$this->assertSame(array(0 => array('a', 'e', 'i')), $matches);
38+
}
39+
40+
/**
41+
* @return void
42+
*/
43+
public function testSuccessNoRef()
44+
{
45+
$result = Preg::isMatchAll('{[aei]}', 'abcdefghijklmnopqrstuvwxyz');
46+
$this->assertSame(true, $result);
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function testFailure()
53+
{
54+
$result = Preg::isMatchAll('{abc}', 'def', $matches);
55+
$this->assertSame(false, $result);
56+
$this->assertSame(array(array()), $matches);
57+
}
58+
59+
/**
60+
* @return void
61+
*/
62+
public function testBadPatternThrowsIfWarningsAreNotThrowing()
63+
{
64+
$this->expectPcreException($pattern = '{[aei]');
65+
@Preg::isMatchAll($pattern, 'abcdefghijklmnopqrstuvwxyz');
66+
}
67+
68+
/**
69+
* @return void
70+
*/
71+
public function testBadPatternTriggersWarningByDefault()
72+
{
73+
$this->expectPcreWarning();
74+
Preg::isMatchAll('{[aei]', 'abcdefghijklmnopqrstuvwxyz');
75+
}
76+
}

tests/PregTests/IsMatchTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of composer/pcre.
5+
*
6+
* (c) Composer <https://github.com/composer>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Composer\Pcre\PregTests;
13+
14+
use Composer\Pcre\BaseTestCase;
15+
use Composer\Pcre\Preg;
16+
17+
class IsMatchTest extends BaseTestCase
18+
{
19+
/**
20+
* This can be replaced with a setUp() method when appropriate
21+
*
22+
* @before
23+
* @return void
24+
*/
25+
public function registerFunctionName()
26+
{
27+
$this->pregFunction = 'preg_match()';
28+
}
29+
30+
/**
31+
* @return void
32+
*/
33+
public function testSuccess()
34+
{
35+
$result = Preg::isMatch('{(?P<m>[io])}', 'abcdefghijklmnopqrstuvwxyz', $matches);
36+
$this->assertSame(true, $result);
37+
$this->assertSame(array(0 => 'i', 'm' => 'i', 1 => 'i'), $matches);
38+
}
39+
40+
/**
41+
* @return void
42+
*/
43+
public function testSuccessNoRef()
44+
{
45+
$result = Preg::isMatch('{(?P<m>[io])}', 'abcdefghijklmnopqrstuvwxyz');
46+
$this->assertSame(true, $result);
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function testFailure()
53+
{
54+
$result = Preg::isMatch('{abc}', 'def', $matches);
55+
$this->assertSame(false, $result);
56+
$this->assertSame(array(), $matches);
57+
}
58+
59+
/**
60+
* @return void
61+
*/
62+
public function testBadPatternThrowsIfWarningsAreNotThrowing()
63+
{
64+
$this->expectPcreException($pattern = '{(?P<m>[io])');
65+
@Preg::isMatch($pattern, 'abcdefghijklmnopqrstuvwxyz');
66+
}
67+
68+
/**
69+
* @return void
70+
*/
71+
public function testBadPatternTriggersWarningByDefault()
72+
{
73+
$this->expectPcreWarning();
74+
Preg::isMatch('{(?P<m>[io])', 'abcdefghijklmnopqrstuvwxyz');
75+
}
76+
}

0 commit comments

Comments
 (0)