Skip to content

Commit 0a99d5b

Browse files
committed
Added functionality to use wildcards in InPath
1 parent 2f67e4c commit 0a99d5b

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

src/Specification/InPath.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ public function __construct(Path $path)
4444
public function isSatisfiedBy(array $value)
4545
{
4646
if (isset($value['dirname'])) {
47-
$path = $this->removeDotSlash((string) $this->path);
47+
$path = $this->cleanPath((string) $this->path);
4848

49-
if (substr($value['dirname'], 0, strlen($path)) === $path) {
49+
$validChars = '[a-zA-Z0-9\\\/\.\<\>\,\|\:\(\)\&\;\#]';
50+
51+
$pattern = '(^(?!\/)'
52+
. str_replace(['?', '*'], [$validChars . '?', $validChars . '*'], $path)
53+
. $validChars . '*)';
54+
55+
if (preg_match($pattern, $value['dirname'] . '/')) {
5056
return true;
5157
}
5258
return false;
@@ -56,15 +62,21 @@ public function isSatisfiedBy(array $value)
5662

5763
/**
5864
* If a path is given with a leading ./ this will be removed
65+
* If a path doesn't have a trailing /, a slash will be added
5966
*
60-
* @param string $dirname
67+
* @param string $path
6168
* @return string
6269
*/
63-
private function removeDotSlash($dirname)
70+
private function cleanPath($path)
6471
{
65-
if (substr($dirname, 0, 2) === './') {
66-
$dirname = substr($dirname, 1);
72+
if (substr($path, 0, 2) === './') {
73+
$path = substr($path, 1);
6774
}
68-
return $dirname;
75+
76+
if (substr($path, -1) !== '/') {
77+
$path = $path . '/';
78+
}
79+
80+
return $path;
6981
}
7082
}

tests/unit/Specification/InPathTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,59 @@ class InPathTest extends \PHPUnit_Framework_TestCase
2929
*/
3030
public function setUp()
3131
{
32-
$this->fixture = new InPath(new Path('.hiddendir'));
32+
$this->fixture = new InPath(new Path('*dden?ir/n'));
3333
}
3434

3535
/**
3636
* @covers ::__construct
3737
* @covers ::isSatisfiedBy
3838
* @covers ::<private>
39+
* @dataProvider validDirnames
3940
* @uses Flyfinder\Path
4041
*/
41-
public function testIfSpecificationIsSatisfied()
42+
public function testIfSpecificationIsSatisfied($dirname)
4243
{
43-
$this->assertTrue($this->fixture->isSatisfiedBy(['dirname' => '.hiddendir/normaldir']));
44+
$this->assertTrue($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
45+
}
46+
47+
/**
48+
* Data provider for testIfSpecificationIsSatisfied. Contains a few valid directory names
49+
*
50+
* @return array
51+
*/
52+
public function validDirnames()
53+
{
54+
return [
55+
['.hiddendir/n'],
56+
['.hiddendir/n/'],
57+
['.hiddendir/n/somedir'],
58+
['.hiddendir/n/somedir.txt']
59+
];
4460
}
4561

4662
/**
4763
* @covers ::__construct
4864
* @covers ::isSatisfiedBy
4965
* @covers ::<private>
66+
* @dataProvider InvalidDirnames
5067
* @uses Flyfinder\Path
5168
*/
52-
public function testIfSpecificationIsNotSatisfied()
69+
public function testIfSpecificationIsNotSatisfied($dirname)
70+
{
71+
$this->assertFalse($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
72+
}
73+
74+
/**
75+
* Data provider for testIfSpecificationIsNotSatisfied. Contains a few valid directory names
76+
*
77+
* @return array
78+
*/
79+
public function InvalidDirnames()
5380
{
54-
$this->assertFalse($this->fixture->isSatisfiedBy(['dirname' => '/home']));
81+
return [
82+
['/hiddendir/n'],
83+
['.hiddendir/normaldir'],
84+
['.hiddendir.ext/n']
85+
];
5586
}
5687
}

0 commit comments

Comments
 (0)