Skip to content

Commit

Permalink
Added flag Glob::MATCH_KEYS for Glob::filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Dec 29, 2015
1 parent 46067f6 commit a89cea7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

* 4.1.0 (2015-12-29)

* added flag `Glob::MATCH_KEYS` for `Glob::filter()`

* 4.0.0 (2015-12-28)

* switched to a better-performing algorithm for `Glob::toRegEx()`
Expand Down
39 changes: 33 additions & 6 deletions src/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
*/
final class Glob
{
/**
* Flag: Match the keys instead of the values in {@link Glob::filter()}
*/
const MATCH_KEYS = 1;

/**
* Globs the file system paths matching the glob.
*
Expand Down Expand Up @@ -168,19 +173,41 @@ public static function match($path, $glob, $flags = 0)
public static function filter(array $paths, $glob, $flags = 0)
{
if (!self::isDynamic($glob)) {
if (false !== $key = array_search($glob, $paths)) {
return array($key => $glob);
if ($flags & self::MATCH_KEYS) {
return isset($paths[$glob]) ? array($glob => $paths[$glob]) : array();
}

return array();
$key = array_search($glob, $paths);

return false !== $key ? array($key => $glob) : array();
}

$staticPrefix = self::getStaticPrefix($glob, $flags);
$regExp = self::toRegEx($glob, $flags);

return array_filter($paths, function ($path) use ($staticPrefix, $regExp) {
$filter = function ($path) use ($staticPrefix, $regExp) {
return 0 === strpos($path, $staticPrefix) && preg_match($regExp, $path);
});
};

if (PHP_VERSION_ID >= 50600) {
$filterFlags = ($flags & self::MATCH_KEYS) ? ARRAY_FILTER_USE_KEY : 0;

return array_filter($paths, $filter, $filterFlags);
}

// No support yet for the third argument of array_filter()
if ($flags & self::MATCH_KEYS) {
$result = array();

foreach ($paths as $path => $value) {
if ($filter($path)) {
$result[$path] = $value;
}
}

return $result;
}

return array_filter($paths, $filter);
}

/**
Expand Down
61 changes: 60 additions & 1 deletion tests/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ public function testFilter()
$filtered = array();

// The keys remain the same in the filtered array
$i = 0;
$i = 42;

foreach ($this->provideDoubleWildcardMatches() as $input) {
$paths[$i] = $input[0];
Expand Down Expand Up @@ -911,4 +911,63 @@ public function testFilterFailsIfNotAbsolute()
{
Glob::filter(array('/foo/bar.css'), '*.css');
}

public function testFilterKeys()
{
$paths = array();
$filtered = array();

// The values remain the same in the filtered array
$i = 42;

foreach ($this->provideDoubleWildcardMatches() as $input) {
$paths[$input[0]] = $i;

if ($input[1]) {
$filtered[$input[0]] = $i;
}

++$i;
}

$this->assertSame($filtered, Glob::filter($paths, '/foo/**/*.js~', Glob::MATCH_KEYS));
}

public function testFilterKeysWithoutWildcard()
{
$paths = array(
'/foo' => 2,
'/foo/bar.js' => 3,
);

$this->assertSame(array('/foo/bar.js' => 3), Glob::filter($paths, '/foo/bar.js', Glob::MATCH_KEYS));
$this->assertSame(array(), Glob::filter($paths, '/foo/bar.js~', Glob::MATCH_KEYS));
}

public function testFilterKeysEscaped()
{
$paths = array(
'/foo' => 3,
'/foo*.js' => 4,
'/foo/bar.js' => 5,
'/foo/bar*.js' => 6,
'/foo/bar\\*.js' => 7,
'/foo/bar\\baz.js' => 8,
);

$this->assertSame(array(
'/foo*.js' => 4,
'/foo/bar*.js' => 6,
'/foo/bar\\*.js' => 7,
), Glob::filter($paths, '/**/*\\*.js', Glob::MATCH_KEYS));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage *.css
*/
public function testFilterKeysFailsIfNotAbsolute()
{
Glob::filter(array('/foo/bar.css' => 42), '*.css', Glob::MATCH_KEYS);
}
}

0 comments on commit a89cea7

Please sign in to comment.