1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 16:22:37 +01:00
Files
archived-php-src/ext/spl/internal/recursivetreeiterator.inc
Peter Kokot 1c850bfcca Sync leading and final newlines in source code files
This patch adds missing newlines, trims multiple redundant final
newlines into a single one, and trims redundant leading newlines.

According to POSIX, a line is a sequence of zero or more non-' <newline>'
characters plus a terminating '<newline>' character. [1] Files should
normally have at least one final newline character.

C89 [2] and later standards [3] mention a final newline:
"A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character."

Although it is not mandatory for all files to have a final newline
fixed, a more consistent and homogeneous approach brings less of commit
differences issues and a better development experience in certain text
editors and IDEs.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
[2] https://port70.net/~nsz/c/c89/c89-draft.html#2.1.1.2
[3] https://port70.net/~nsz/c/c99/n1256.html#5.1.1.2
2018-10-14 12:55:24 +02:00

133 lines
3.4 KiB
PHP

<?php
/** @file recursivetreeiterator.inc
* @ingroup SPL
* @brief class RecursiveTreeIterator
* @author Marcus Boerger, Johannes Schlueter
* @date 2005 - 2009
*
* SPL - Standard PHP Library
*/
/** @ingroup SPL
* @brief RecursiveIteratorIterator to generate ASCII graphic trees for the
* entries in a RecursiveIterator
* @author Marcus Boerger, Johannes Schlueter
* @version 1.1
* @since PHP 5.3
*/
class RecursiveTreeIterator extends RecursiveIteratorIterator
{
const BYPASS_CURRENT = 0x00000004;
const BYPASS_KEY = 0x00000008;
private $rit_flags;
/**
* @param it iterator to use as inner iterator
* @param rit_flags flags passed to RecursiveIteratoIterator (parent)
* @param cit_flags flags passed to RecursiveCachingIterator (for hasNext)
* @param mode mode passed to RecursiveIteratoIterator (parent)
*/
function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
{
parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
$this->rit_flags = $rit_flags;
}
private $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
/** Prefix used to start elements. */
const PREFIX_LEFT = 0;
/** Prefix used if $level < depth and hasNext($level) == true. */
const PREFIX_MID_HAS_NEXT = 1;
/** Prefix used if $level < depth and hasNext($level) == false. */
const PREFIX_MID_LAST = 2;
/** Prefix used if $level == depth and hasNext($level) == true. */
const PREFIX_END_HAS_NEXT = 3;
/** Prefix used if $level == depth and hasNext($level) == false. */
const PREFIX_END_LAST = 4;
/** Prefix used right in front of the current element. */
const PREFIX_RIGHT = 5;
/**
* Set prefix part as used in getPrefix() and stored in $prefix.
* @param $part any PREFIX_* const.
* @param $value new prefix string for specified part.
* @throws OutOfRangeException if 0 > $part or $part > 5.
*/
function setPrefixPart($part, $value)
{
if (0 > $part || $part > 5) {
throw new OutOfRangeException();
}
$this->prefix[$part] = (string)$value;
}
/** @return string to place in front of current element
*/
function getPrefix()
{
$tree = '';
for ($level = 0; $level < $this->getDepth(); $level++)
{
$tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[1] : $this->prefix[2];
}
$tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[3] : $this->prefix[4];
return $this->prefix[0] . $tree . $this->prefix[5];
}
/** @return string presentation build for current element
*/
function getEntry()
{
return @(string)parent::current();
}
/** @return string to place after the current element
*/
function getPostfix()
{
return '';
}
/** @return the current element prefixed and postfixed
*/
function current()
{
if ($this->rit_flags & self::BYPASS_CURRENT)
{
return parent::current();
}
else
{
return $this->getPrefix() . $this->getEntry() . $this->getPostfix();
}
}
/** @return the current key prefixed and postfixed
*/
function key()
{
if ($this->rit_flags & self::BYPASS_KEY)
{
return parent::key();
}
else
{
return $this->getPrefix() . parent::key() . $this->getPostfix();
}
}
/** Aggregates the inner iterator
*/
function __call($func, $params)
{
return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
}
?>