diff options
Diffstat (limited to 'fusonic/linq/src/Fusonic/Linq/Iterator/OfTypeIterator.php')
-rw-r--r-- | fusonic/linq/src/Fusonic/Linq/Iterator/OfTypeIterator.php | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/fusonic/linq/src/Fusonic/Linq/Iterator/OfTypeIterator.php b/fusonic/linq/src/Fusonic/Linq/Iterator/OfTypeIterator.php new file mode 100644 index 00000000..46a59482 --- /dev/null +++ b/fusonic/linq/src/Fusonic/Linq/Iterator/OfTypeIterator.php @@ -0,0 +1,94 @@ +<?php +/* + * This file is part of Fusonic-linq. + * https://github.com/fusonic/fusonic-linq + * + * (c) Burgy Benjamin <benjamin.burgy@gmail.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fusonic\Linq\Iterator; + +use FilterIterator; +use Fusonic\Linq\Helper; +use Iterator; + +/** + * Iterator for filtering the Linq query with a specified <b>type</b>. + * @package Fusonic\Linq\Iterator + */ +final class OfTypeIterator + extends + \FilterIterator +{ + /** + * @var callable $acceptCallback + */ + private $acceptCallback; + + /** + * Initializes an instance of <b>OfTypeIterator</b>. + * + * @param Iterator $iterator + * @param string $type + */ + public function __construct(Iterator $iterator, $type) + { + parent::__construct($iterator); + + switch (strtolower($type)) + { + case 'int': + case 'integer': + $this->acceptCallback = function ($current) + { + return is_int($current); + }; + break; + case 'float': + case 'double': + $this->acceptCallback = function ($current) + { + return is_float($current); + }; + break; + case 'string': + $this->acceptCallback = function ($current) + { + return is_string($current); + }; + break; + case 'bool': + case 'boolean': + $this->acceptCallback = function ($current) + { + return is_bool($current); + }; + break; + + default: + $this->acceptCallback = function ($current) use ($type) + { + return $current instanceof $type; + }; + } + } + + /** + * (PHP 5 >= 5.1.0)<br/> + * Check whether the current element of the iterator is acceptable + * @link http://php.net/manual/en/filteriterator.accept.php + * @return bool true if the current element is acceptable, otherwise false. + */ + public function accept() + { + /** @var mixed $current */ + $current = $this->current(); + /** @var callable $func */ + $func = $this->acceptCallback; + + return $func($current); + } +}
\ No newline at end of file |