Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StringHelper::matchAnyRegex() #137

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::matchAnyRegex()` 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

- matchAnyRegex
- 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 @@ -49,9 +49,9 @@
*
* @return int The number of bytes in the given string.
*/
public static function byteLength(string|null $input): int

Check warning on line 52 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @return int The number of bytes in the given string. */ - public static function byteLength(string|null $input) : int + protected static function byteLength(string|null $input) : int { return mb_strlen((string) $input, '8bit'); }
{
return mb_strlen((string)$input, '8bit');

Check warning on line 54 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ */ public static function byteLength(string|null $input) : int { - return mb_strlen((string) $input, '8bit'); + return strlen((string) $input); } /** * Returns the portion of string specified by the start and length parameters.
}

/**
Expand Down Expand Up @@ -729,6 +729,27 @@
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 matchAnyRegex(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/MatchAnyRegexTest.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 MatchAnyRegexTest 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::matchAnyRegex($string, $patterns, $flags);
$this->assertSame($expected, $result);
}

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

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