mirror of
https://github.com/n3w/helpers-cli-input.git
synced 2025-12-19 12:43:23 +00:00
Added InputTypeFilterIterator class
This commit is contained in:
parent
9f5c5c7d4d
commit
90db5c2047
1 changed files with 39 additions and 0 deletions
39
src/Input/InputTypeFilterIterator.php
Normal file
39
src/Input/InputTypeFilterIterator.php
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pointybeard\Helpers\Cli\Input;
|
||||||
|
|
||||||
|
class InputTypeFilterIterator extends \FilterIterator {
|
||||||
|
|
||||||
|
private $types;
|
||||||
|
private $mode;
|
||||||
|
|
||||||
|
public const FILTER_INCLUDE = 0x0001;
|
||||||
|
public const FILTER_EXCLUDE = 0x0002;
|
||||||
|
|
||||||
|
public function __construct(\Iterator $iterator, array $types=[], int $mode=self::FILTER_INCLUDE)
|
||||||
|
{
|
||||||
|
parent::__construct($iterator);
|
||||||
|
|
||||||
|
$this->types = array_map('strtolower', $types);
|
||||||
|
$this->mode = $mode;
|
||||||
|
|
||||||
|
}
|
||||||
|
public function accept()
|
||||||
|
{
|
||||||
|
$input = $this->getInnerIterator()->current();
|
||||||
|
|
||||||
|
switch($this->mode) {
|
||||||
|
case self::FILTER_EXCLUDE:
|
||||||
|
return !in_array($input->getType(), $this->types);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case self::FILTER_INCLUDE:
|
||||||
|
default:
|
||||||
|
return in_array($input->getType(), $this->types);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue