From 6dd1f173ef64883f46d8b82648bbe501549d4995 Mon Sep 17 00:00:00 2001 From: Ashoka de Wit Date: Mon, 22 Oct 2018 19:02:36 +0800 Subject: [PATCH] Implement replacer that uses the pattern replacer trait It is not desirable for other packages to use the replacer trait. Therefor it is now wrapped by a replacer which has its own interface. --- src/PatternReplacer.php | 48 ++++++++++++++++++++++++ src/PatternReplacerInterface.php | 25 +++++++++++++ tests/PatternReplacerTest.php | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/PatternReplacer.php create mode 100644 src/PatternReplacerInterface.php create mode 100644 tests/PatternReplacerTest.php diff --git a/src/PatternReplacer.php b/src/PatternReplacer.php new file mode 100644 index 0000000..75f6ef4 --- /dev/null +++ b/src/PatternReplacer.php @@ -0,0 +1,48 @@ +separator = $separator; + } + + /** + * Get a replacement path for a matched path. + * + * @param string $match + * @param string $pattern + * @param string $replacement + * + * @return string + */ + public function replace( + string $match, + string $pattern, + string $replacement + ): string { + return $this->replaceByPattern( + $pattern, + $match, + $replacement, + $this->separator + ); + } +} diff --git a/src/PatternReplacerInterface.php b/src/PatternReplacerInterface.php new file mode 100644 index 0000000..d744807 --- /dev/null +++ b/src/PatternReplacerInterface.php @@ -0,0 +1,25 @@ +assertSame( + $expected, + $subject->replace($match, $pattern, $replacement) + ); + } + + /** + * @return array + */ + public function dataProvider(): array + { + return [ + ['foo.*', 'foo.bar', '$0', '.', 'foo.bar'], + ['foo.*', 'foo.bar', 'qux.$1', '.', 'qux.bar'], + ['foo.ba*', 'foo.bar', 'qux.ba$1', '.', 'qux.bar'], + ['foo.ba*', 'foo.bar', 'qux.$1', '.', 'qux.r'], + ['foo.ba?', 'foo.bar', 'qux.$1', '.', 'qux.r'], + ['foo.ba[rz]', 'foo.bar', 'qux.$1', '.', 'qux.r'], + ['foo.ba[!ad]', 'foo.bar', 'qux.$1', '.', 'qux.r'], + ['fo*.*', 'foo.bar', '$2.$1.baz', '.', 'bar.o.baz'], + ['fo*#*', 'foo#bar', '$2#$1#baz', '#', 'bar#o#baz'], + ['fo*/*', 'foo/bar', '$2/$1/baz', '/', 'bar/o/baz'], + ]; + } +}