Swat/SwatString.php
public static function toList(
        $iterator,
        $conjunction = 'and',
        $delimiter = ', ',
        $display_final_delimiter = true
    ) {
        if (is_array($iterator)) {
            $iterator = new ArrayIterator($iterator);
        }

        if (!($iterator instanceof Iterator)) {
            throw new SwatException('Value is not an Iterator or array');
        }

        if (count($iterator) === 1) {
            $iterator->rewind();
            $list = $iterator->current();
        } else {
            $count = 0;
            $list = '';

            foreach ($iterator as $value) {
                if ($count != 0) {
                    if ($count == count($iterator) - 1) {
                        $list .=
                            $display_final_delimiter && count($iterator) > 2
                                ? $delimiter
                                : ' ';

                        if ($conjunction != '') {
                            $list .= $conjunction . ' ';
                        }
                    } else {
                        $list .= $delimiter;
                    }
                }

                $list .= $value;
                $count++;
            }
        }

        return $list;
    }
public static function toList(
        $iterator,
        $conjunction = 'and',
        $delimiter = ', ',
        $display_final_delimiter = true
    ) {
        if (is_array($iterator)) {
            $iterator = new ArrayIterator($iterator);
        }

        if (!($iterator instanceof Iterator)) {
            throw new SwatException('Value is not an Iterator or array');
        }

        if (count($iterator) === 1) {
            $iterator->rewind();
            $list = $iterator->current();
        } else {
            $count = 0;
            $list = '';

            foreach ($iterator as $value) {
                if ($count != 0) {
                    if ($count == count($iterator) - 1) {
                        $list .=
                            $display_final_delimiter && count($iterator) > 2
                                ? $delimiter
                                : ' ';

                        if ($conjunction != '') {
                            $list .= $conjunction . ' ';
                        }
                    } else {
                        $list .= $delimiter;
                    }
                }

                $list .= $value;
                $count++;
            }
        }

        return $list;
    }
public static function toList ( $ iterator , $ conjunction = 'and' , $ delimiter = ', ' , $ display_final_delimiter = true ) { if ( is_array ( $ iterator ) ) { $ iterator = new ArrayIterator ( $ iterator ) ; } if ( ! ( $ iterator instanceof Iterator ) ) { throw new SwatException ( 'Value is not an Iterator or array' ) ; } if ( count ( $ iterator ) === 1 ) { $ iterator -> rewind ( ) ; $ list = $ iterator -> current ( ) ; } else { $ count = 0 ; $ list = '' ; foreach ( $ iterator as $ value ) { if ( $ count != 0 ) { if ( $ count == count ( $ iterator ) - 1 ) { $ list .= $ display_final_delimiter && count ( $ iterator ) > 2 ? $ delimiter : ' ' ; if ( $ conjunction != '' ) { $ list .= $ conjunction . ' ' ; } } else { $ list .= $ delimiter ; } } $ list .= $ value ; $ count ++ ; } } return $ list ; }
Convert an iterable object or array into a human-readable, delimited
list.

@param array|Iterator $iterator the object to convert to a list.
@param string $conjunction the list's conjunction. Usually 'and' or
'or'.
@param string $delimiter the list delimiter. If list items should
additionally be padded with a space, the delimiter
should also include the space.
@param boolean $display_final_delimiter whether or not the final list
item should be separated from the list with a
delimiter.

@return string The formatted list.

@throws SwatException if the iterator value is not an array or Iterator

@todo Think about using a mask to make this as flexible as possible for
different locales.
Convert an iterable object or array into a human - readable delimited list .