本文整理匯總了PHP中Iterator::setMaxDepth方法的典型用法代碼示例。如果您正苦於以下問題:PHP Iterator::setMaxDepth方法的具體用法?PHP Iterator::setMaxDepth怎麽用?PHP Iterator::setMaxDepth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Iterator
的用法示例。
在下文中一共展示了Iterator::setMaxDepth方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __construct
/**
* Constructs the iterator
* @param string $path The folder path
* @param int $depth The depth to recourse
* @param callable[] $filters An array of filters used to restrict the resulting list of files or folders
*/
public function __construct($path, $depth = null, array $filters = array())
{
//TODO: verify path exists and filters are callable
$this->path = (string) $path;
//create the iterator
$flags = \FilesystemIterator::SKIP_DOTS;
if ($depth === 0) {
$this->iterator = new \EmptyIterator();
} else {
if ($depth === 1) {
$this->iterator = new \FilesystemIterator($path, $flags);
} else {
$this->iterator = new \RecursiveDirectoryIterator($path, $flags);
$this->iterator = new \RecursiveIteratorIterator($this->iterator, \RecursiveIteratorIterator::SELF_FIRST);
if ($depth) {
$this->iterator->setMaxDepth($depth - 1);
}
}
}
//decorate iterator with filters
if (count($filters)) {
$this->iterator = new \CallbackFilterIterator($this->iterator, function ($path) use($filters) {
foreach ($filters as $filter) {
if (!$filter($path)) {
return false;
}
}
return true;
});
}
}