sep = $sep; $this->wraps = $wraps; } /** * @param (string|WrappedString|WrappedStringList)[] $wraps * @return WrappedStringList Combined list */ protected function extend( array $wraps ) { $list = clone $this; $list->wraps = array_merge( $list->wraps, $wraps ); return $list; } /** * Merge consecutive lists with the same separator. * * Does not modify the given array or any of the objects in it. * * @param (string|WrappedString|WrappedStringList)[] $lists * @param string $outerSep Separator that the caller intends to use when joining the strings * @return string[] Compacted list to be treated as strings * (may contain WrappedString and WrappedStringList objects) */ protected static function compact( array $lists, $outerSep ) { $consolidated = []; foreach ( $lists as $list ) { if ( !$list instanceof WrappedStringList ) { // Probably WrappedString or regular string, // Not mergable as a list, but may be merged as a string // later by WrappedString::compact. $consolidated[] = $list; continue; } if ( $list->sep === $outerSep ) { $consolidated = array_merge( $consolidated, self::compact( $list->wraps, $outerSep ) ); } else { $consolidated[] = $list; } } return WrappedString::compact( $consolidated ); } /** * Join a several wrapped strings with a separator between each. * * @param string $sep * @param (string|WrappedString|WrappedStringList)[] $lists * @return string */ public static function join( $sep, array $lists ) { return implode( $sep, self::compact( $lists, $sep ) ); } /** @return string */ public function __toString() { return self::join( $this->sep, [ $this ] ); } }