Skip to content

Commit

Permalink
Fix #51: Add an option withEnding() to WildcardPattern for match …
Browse files Browse the repository at this point in the history
…ending of testing string
  • Loading branch information
vjik committed Nov 13, 2020
1 parent decfe3d commit 89c5dff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 1.0.2 under development

- Enh #51: Add an option `withEnding()` to `WildcardPattern` for match ending of testing string (vjik)
- Bug #44: `NumericHelper::toOrdinal` throws an error for numbers with fractional part (vjik)

## 1.0.1 October 12, 2020
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -161,6 +161,7 @@ Several options are available. Call these before doing a `match()`:
- `withExactSlashes()` - makes `\` in a string to match `\` only in a pattern.
- `ignoreCase()` - case-insensitive match.
- `withExactLeadingPeriod()` - makes first `.` in a string match only `.` in a pattern.
- `withEnding()` - match ending of testing string.

When matching file paths, it is advised to use both `withExactSlashes()` and `withExactLeadingPeriod()`:

Expand Down
16 changes: 15 additions & 1 deletion src/WildcardPattern.php
Expand Up @@ -27,6 +27,7 @@ final class WildcardPattern
private bool $matchSlashesExactly = false;
private bool $matchLeadingPeriodExactly = false;
private bool $ignoreCase = false;
private bool $matchEnding = false;
private string $pattern;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ public function match(string $string): bool
}

$pattern = strtr(preg_quote($pattern, '#'), $replacements);
$pattern = '#^' . $pattern . '$#us';
$pattern = '#' . ($this->matchEnding ? '' : '^') . $pattern . '$#us';

if ($this->ignoreCase) {
$pattern .= 'i';
Expand Down Expand Up @@ -135,4 +136,17 @@ public function withExactLeadingPeriod(bool $flag = true): self
$new->matchLeadingPeriodExactly = $flag;
return $new;
}

/**
* Match ending only.
* By default wildcard pattern matches string exactly. By using this mode, beginning of the string could be anything.
* @param bool $flag
* @return self
*/
public function withEnding(bool $flag = true): self
{
$new = clone $this;
$new->matchEnding = $flag;
return $new;
}
}
16 changes: 16 additions & 0 deletions tests/WildcardPatternTest.php
Expand Up @@ -86,6 +86,13 @@ public function dataProviderMatchWildcard(): array
['begin\*\end', 'begin\end', false, ['escape' => false]],
['begin\*\end', 'begin\middle\end', true, ['filePath' => true, 'escape' => false]],
['begin\*\end', 'begin\two\steps\end', false, ['filePath' => true, 'escape' => false]],
// ending
['i/*.jpg', 'i/hello.jpg', true, ['ending' => true]],
['i/*.jpg', 'i/hello.jpg', true, ['ending' => true, 'filePath' => true]],
['i/*.jpg', 'i/h/hello.jpg', true, ['ending' => true]],
['i/*.jpg', 'i/h/hello.jpg', false, ['ending' => true, 'filePath' => true]],
['i/*.jpg', 'path/to/i/hello.jpg', true, ['ending' => true]],
['i/*.jpg', 'path/to/i/hello.jpg', true, ['ending' => true, 'filePath' => true]],
];
}

Expand Down Expand Up @@ -118,12 +125,20 @@ private function getWildcardPattern(string $pattern, array $options): WildcardPa
if (isset($options['leadingPeriod']) && $options['leadingPeriod'] === true) {
$wildcardPattern = $wildcardPattern->withExactLeadingPeriod();
}
if (isset($options['ending']) && $options['ending'] === true) {
$wildcardPattern = $wildcardPattern->withEnding();
}

return $wildcardPattern;
}

public function testDisableOptions(): void
{
$wildcardPattern = (new WildcardPattern('abc'))
->withEnding()
->withEnding(false);
$this->assertFalse($wildcardPattern->match('42abc'));

$wildcardPattern = (new WildcardPattern('\*42'))
->withoutEscape()
->withoutEscape(false);
Expand Down Expand Up @@ -152,5 +167,6 @@ public function testImmutability(): void
$this->assertNotSame($original, $original->ignoreCase());
$this->assertNotSame($original, $original->withExactSlashes());
$this->assertNotSame($original, $original->withoutEscape());
$this->assertNotSame($original, $original->withEnding());
}
}

0 comments on commit 89c5dff

Please sign in to comment.