Skip to content

Commit

Permalink
Add StringHelper::isStringMatchingAnyPattern()
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 18, 2025
1 parent 0e09941 commit 041788a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.4.1 under development

- New #137: Add `StringHelper::isStringMatchingAnyPattern()` method as a facade for `CombinedRegexp` (@vjik)
- Enh #128: Add more specific psalm type for result of `StringHelper::base64UrlEncode()` method (@vjik)

## 2.4.0 December 22, 2023
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Overall the helper has the following method groups.

### Other

- isStringMatchingAnyPattern
- parsePath
- split

Expand Down
21 changes: 21 additions & 0 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,27 @@ public static function findBetweenLast(string $string, string $start, ?string $e
return mb_substr($string, $startPos, $endPos - $startPos);
}

/**
* Checks if a given string matches any of the provided patterns.
*
* Note that patterns should be provided without delimiters on both sides. For example, `te(s|x)t`.
*
* @see https://www.php.net/manual/reference.pcre.pattern.syntax.php
* @see https://www.php.net/manual/reference.pcre.pattern.modifiers.php
*
* @param string $string The string to match against the patterns.
* @param string[] $patterns Regular expressions without delimiters on both sides.
* @param string $flags Flags to apply to all regular expressions.
*/
public static function isStringMatchingAnyPattern(string $string, array $patterns, string $flags = ''): bool
{
if (empty($patterns)) {
return false;
}

return (new CombinedRegexp($patterns, $flags))->matches($string);
}

/**
* Ensure the input string is a valid UTF-8 string.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/StringHelper/IsStringMatchingAnyPatternTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Strings\Tests\StringHelper;

use PHPUnit\Framework\TestCase;
use Yiisoft\Strings\StringHelper;

final class IsStringMatchingAnyPatternTest extends TestCase
{
public static function dataBase(): array
{
return [
[true, 'test', ['te[sx]t', 'm(o|a)n']],
[false, 'hello', ['te[sx]t', 'm(o|a)n']],
[false, 'Man', ['te[sx]t', 'm(o|a)n']],
[true, 'Man', ['te[sx]t', 'm(o|a)n'], 'i'],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, string $string, array $patterns, string $flags = ''): void
{
$result = StringHelper::isStringMatchingAnyPattern($string, $patterns, $flags);
$this->assertSame($expected, $result);
}

/**
* @testWith [true, "test"]
* [false, "hello"]
*/
public function testWithoutFlags(bool $expected, string $string): void
{
$result = StringHelper::isStringMatchingAnyPattern($string, ['te[sx]t', 'm(o|a)n']);
$this->assertSame($expected, $result);
}

public function testWithoutPatterns(): void
{
$result = StringHelper::isStringMatchingAnyPattern('test', []);
$this->assertFalse($result);
}
}

0 comments on commit 041788a

Please sign in to comment.