Skip to content

Commit

Permalink
Updated anchor options to an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Dec 6, 2024
1 parent fe40e28 commit a3e4159
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ Pattern::make('foo/bar.txt')->toRegex(); // Returns '#^foo/bar\.txt$#'
Pattern::make('file.{yml,yaml}')->toRegex(); // Returns '#^file\.(yml|yaml)$#'
```

You can control regular expression line anchors via the `$options` parameter.
You can control regular expression line anchors via the `$anchors` parameter.

```php
Pattern::make('foo')->toRegex(Glob::NO_ANCHORS); // Returns '#foo#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR); // Returns '#^foo#'
Pattern::make('foo')->toRegex(Glob::END_ANCHOR); // Returns '#foo$#'
Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS); // Returns '#^foo$#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR | Glob::END_ANCHOR); // Returns '#^foo$#'
use PHLAK\Splat\Anchors;
use PHLAK\Splat\Pattern;

Pattern::make('foo')->toRegex(Anchors::NONE); // Returns '#foo#'
Pattern::make('foo')->toRegex(Anchors::START); // Returns '#^foo#'
Pattern::make('foo')->toRegex(Anchors::END); // Returns '#foo$#'
Pattern::make('foo')->toRegex(Anchors::BOTH); // Returns '#^foo$#'
```

### Pattern Character Escaping
Expand Down
18 changes: 18 additions & 0 deletions src/Anchors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PHLAK\Splat;

enum Anchors
{
/** Do not add start or end anchors */
case NONE;

/** Add start anchor (i.e. '/^.../') */
case START;

/** Add end anchor (i.e. '/...$/') */
case END;

/** Add start and end anchors (i.e. '/^...$/') */
case BOTH;
}
6 changes: 3 additions & 3 deletions src/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function matchStart(string|Pattern $pattern, string $string): bool
$pattern = new Pattern($pattern);
}

return (bool) preg_match($pattern->toRegex(Pattern::START_ANCHOR), $string);
return (bool) preg_match($pattern->toRegex(Anchors::START), $string);
}

/** Test if a string ends with the glob pattern. */
Expand All @@ -51,7 +51,7 @@ public static function matchEnd(string|Pattern $pattern, string $string): bool
$pattern = new Pattern($pattern);
}

return (bool) preg_match($pattern->toRegex(Pattern::END_ANCHOR), $string);
return (bool) preg_match($pattern->toRegex(Anchors::END), $string);
}

/** Test if any part of a string matches the glob pattern. */
Expand All @@ -61,7 +61,7 @@ public static function matchWithin(string|Pattern $pattern, string $string): boo
$pattern = new Pattern($pattern);
}

return (bool) preg_match($pattern->toRegex(Pattern::NO_ANCHORS), $string);
return (bool) preg_match($pattern->toRegex(Anchors::NONE), $string);
}

/**
Expand Down
26 changes: 7 additions & 19 deletions src/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@

class Pattern
{
/** @const Do not add start or end anchors */
public const NO_ANCHORS = 0;

/** @const Add start anchor (i.e. '/^.../') */
public const START_ANCHOR = 1;

/** @const Add end anchor (i.e. '/...$/') */
public const END_ANCHOR = 2;

/** @const Add start and end anchors (i.e. '/^...$/') */
public const BOTH_ANCHORS = self::START_ANCHOR | self::END_ANCHOR;

/** @var string The directory separator */
protected static $directorySeparator = DIRECTORY_SEPARATOR;

/** @var array<int, string> Memoization cache */
/** @var array<string, string> Memoization cache */
private $cache = [];

/** Create a new object. */
Expand Down Expand Up @@ -56,10 +44,10 @@ public static function escape(string $string): string
}

/** Convert the pattern a regular expression. */
public function toRegex(int $options = self::BOTH_ANCHORS): string
public function toRegex(Anchors $anchors = Anchors::BOTH): string
{
if (isset($this->cache[$options])) {
return $this->cache[$options];
if (isset($this->cache[$anchors->name])) {
return $this->cache[$anchors->name];
}

$pattern = '';
Expand Down Expand Up @@ -183,14 +171,14 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
}
}

if ($options & self::START_ANCHOR) {
if (in_array($anchors, [Anchors::BOTH, Anchors::START])) {
$pattern = '^' . $pattern;
}

if ($options & self::END_ANCHOR) {
if (in_array($anchors, [Anchors::BOTH, Anchors::END])) {
$pattern = $pattern . '$';
}

return $this->cache[$options] = sprintf('#%s#', $pattern);
return $this->cache[$anchors->name] = sprintf('#%s#', $pattern);
}
}
10 changes: 5 additions & 5 deletions tests/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use PHLAK\Splat\Anchors;
use PHLAK\Splat\Pattern;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -87,11 +88,10 @@ public function it_can_convert_a_glob_pattern_with_lookaheads(): void
#[Test]
public function regular_expression_start_and_end_anchors_are_configurable(): void
{
$this->assertEquals('#foo#', Pattern::make('foo')->toRegex(Pattern::NO_ANCHORS));
$this->assertEquals('#^foo#', Pattern::make('foo')->toRegex(Pattern::START_ANCHOR));
$this->assertEquals('#foo$#', Pattern::make('foo')->toRegex(Pattern::END_ANCHOR));
$this->assertEquals('#^foo$#', Pattern::make('foo')->toRegex(Pattern::BOTH_ANCHORS));
$this->assertEquals('#^foo$#', Pattern::make('foo')->toRegex(Pattern::START_ANCHOR | Pattern::END_ANCHOR));
$this->assertEquals('#foo#', Pattern::make('foo')->toRegex(Anchors::NONE));
$this->assertEquals('#^foo#', Pattern::make('foo')->toRegex(Anchors::START));
$this->assertEquals('#foo$#', Pattern::make('foo')->toRegex(Anchors::END));
$this->assertEquals('#^foo$#', Pattern::make('foo')->toRegex(Anchors::BOTH));
}

#[Test]
Expand Down

0 comments on commit a3e4159

Please sign in to comment.