Created
October 29, 2013 20:19
-
-
Save terion-name/7221826 to your computer and use it in GitHub Desktop.
RecursiveTreeIterator that accepts callback for flattening non-string items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use RecursiveTreeIterator, CachingIterator, RecursiveIterator, RecursiveIteratorIterator; | |
/** | |
* RecursiveTreeIterator that accepts callback for flattening non-string items | |
* | |
* Class RecursiveTreeCallbackIterator | |
*/ | |
class RecursiveTreeCallbackIterator extends RecursiveTreeIterator | |
{ | |
/** | |
* @var callable | |
*/ | |
private $callback; | |
/** | |
* @var int | |
*/ | |
private $rit_flags; | |
/** | |
* @param RecursiveIterator $it | |
* @param callable $callback | |
* @param int $rit_flags | |
* @param int $cit_flags | |
* @param int $mode | |
*/ | |
public function __construct( | |
RecursiveIterator $it, | |
callable $callback, | |
$rit_flags = RecursiveTreeIterator::BYPASS_KEY, | |
$cit_flags = CachingIterator::CATCH_GET_CHILD, | |
$mode = RecursiveIteratorIterator::SELF_FIRST | |
) { | |
$this->callback = $callback; | |
$this->rit_flags = $rit_flags; | |
parent::__construct($it, $rit_flags, $cit_flags, $mode); | |
} | |
/** | |
* @return string | |
*/ | |
function getEntry() | |
{ | |
return @(string)call_user_func($this->callback, RecursiveIteratorIterator::current()); | |
} | |
/** | |
* @return string | |
*/ | |
public function current() | |
{ | |
if ($this->rit_flags & parent::BYPASS_CURRENT) | |
{ | |
return call_user_func($this->callback, RecursiveIteratorIterator::current()); | |
} | |
else | |
{ | |
return $this->getPrefix() . $this->getEntry() . $this->getPostfix(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment