0
我正在努力與位掩碼(或是位域?)。我不知道如何去做。FilterIterator位掩碼(或位域)
我想創建一個DirectoryFilterIterator,它接受要過濾的標誌。我想我會使用這些位爲:
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
因爲DOT
也被認爲是DIR
我希望能夠這兩個也來區分。如果我是正確的我覺得這樣的事情應該有可能是這樣的:
DirectoryFilterIterator::DIR & ~DirectoryFilterIterator::DOT
換句話說,這應該過濾掉DIR
除非它是一個DOT
。但我完全陷入如何得到過濾工作(在accept
法):
class DirectoryFilterIterator
extends FilterIterator
{
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
protected $_filter;
public function __construct(DirectoryIterator $iterator, $filter = self::DIR)
{
parent::__construct($iterator);
$this->_filter = $filter;
}
public function accept()
{
$item = $this->getInnerIterator()->current();
return (
!(($this->_filter & self::DOT) == self::DOT && $item->isDot()) &&
!(($this->_filter & self::DIR) == self::DIR && $item->isDir()) &&
!(($this->_filter & self::FILE) == self::FILE && $item->isFile())
);
}
}
...尤其是因爲所有的否定我已經回事,我有點失去。我怎樣才能使這個工作正常?