mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
Add directory filter
This commit is contained in:
parent
91176a6bf5
commit
f47297b76a
4 changed files with 112 additions and 16 deletions
60
src/DirectoryFilter.php
Normal file
60
src/DirectoryFilter.php
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Licensed under the MIT license:
|
||||||
|
* http://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\Streams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper allows filtering of directories
|
||||||
|
*
|
||||||
|
* The filter callback will be called for each entry in the folder
|
||||||
|
* when the callback return false the entry will be filtered out
|
||||||
|
*/
|
||||||
|
class DirectoryFilter extends DirectoryWrapper {
|
||||||
|
/**
|
||||||
|
* @var callable
|
||||||
|
*/
|
||||||
|
private $filter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param array $options
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function dir_opendir($path, $options) {
|
||||||
|
$context = $this->loadContext('filter');
|
||||||
|
$this->filter = $context['filter'];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function dir_readdir() {
|
||||||
|
$file = readdir($this->source);
|
||||||
|
$filter = $this->filter;
|
||||||
|
// keep reading untill we have an accepted entry or we're at the end of the folder
|
||||||
|
while ($file !== false && $filter($file) === false) {
|
||||||
|
$file = readdir($this->source);
|
||||||
|
}
|
||||||
|
return $file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param resource $source
|
||||||
|
* @param callable $filter
|
||||||
|
* @return resource
|
||||||
|
*/
|
||||||
|
public static function wrap($source, callable $filter) {
|
||||||
|
$options = array(
|
||||||
|
'filter' => array(
|
||||||
|
'source' => $source,
|
||||||
|
'filter' => $filter
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return self::wrapWithOptions($options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -73,22 +73,6 @@ class DirectoryWrapper implements Directory {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a directory handle from the provided array or iterator
|
|
||||||
*
|
|
||||||
* @param resource $source
|
|
||||||
* @return resource
|
|
||||||
*
|
|
||||||
* @throws \BadMethodCallException
|
|
||||||
*/
|
|
||||||
public static function wrap($source) {
|
|
||||||
$options = array(
|
|
||||||
'dir' => array(
|
|
||||||
'source' => $source)
|
|
||||||
);
|
|
||||||
return self::wrapWithOptions($options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $options the options for the context to wrap the stream with
|
* @param string $options the options for the context to wrap the stream with
|
||||||
* @return resource
|
* @return resource
|
||||||
|
|
|
||||||
44
tests/DirectoryFilter.php
Normal file
44
tests/DirectoryFilter.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Licensed under the MIT license:
|
||||||
|
* http://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\Streams\Tests;
|
||||||
|
|
||||||
|
class DirectoryFilter extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testFilterAcceptAll() {
|
||||||
|
$this->filter(['a', 'b', 'c'],
|
||||||
|
function () {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
['a', 'b', 'c']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFilterRejectAll() {
|
||||||
|
$this->filter(['a', 'b', 'c'],
|
||||||
|
function () {
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFilterRejectLong() {
|
||||||
|
$this->filter(['a', 'bb', 'c'],
|
||||||
|
function ($file) {
|
||||||
|
return strlen($file) < 2;
|
||||||
|
},
|
||||||
|
['a', 'c']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function filter(array $files, callable $filter, array $expected) {
|
||||||
|
$source = \Icewind\Streams\IteratorDirectory::wrap($files);
|
||||||
|
$filtered = \Icewind\Streams\DirectoryFilter::wrap($source, $filter);
|
||||||
|
$result = [];
|
||||||
|
while (($file = readdir($filtered)) !== false) {
|
||||||
|
$result[] = $file;
|
||||||
|
}
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,14 @@
|
||||||
namespace Icewind\Streams\Tests;
|
namespace Icewind\Streams\Tests;
|
||||||
|
|
||||||
class DirectoryWrapperDummy extends \Icewind\Streams\DirectoryWrapper {
|
class DirectoryWrapperDummy extends \Icewind\Streams\DirectoryWrapper {
|
||||||
|
public static function wrap($source) {
|
||||||
|
$options = array(
|
||||||
|
'dir' => array(
|
||||||
|
'source' => $source)
|
||||||
|
);
|
||||||
|
return self::wrapWithOptions($options);
|
||||||
|
}
|
||||||
|
|
||||||
public function dir_readdir() {
|
public function dir_readdir() {
|
||||||
$file = parent::dir_readdir();
|
$file = parent::dir_readdir();
|
||||||
if ($file !== false) {
|
if ($file !== false) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue