Debian Codegraph @

vendor/wikimedia/wrappedstring/src/WrappedStringList.php

raw
1 <?php
2 /**
3 * Copyright 2016 Timo Tijhof
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * @file
25 */
26
27 namespace Wikimedia;
28
29 class WrappedStringList {
30 /** @var string */
31 protected $sep;
32
33 /** @var (string|WrappedString|WrappedStringList)[] */
34 protected $wraps;
35
36 /**
37 * @param string $sep
38 * @param (string|WrappedString|WrappedStringList)[] $wraps
39 */
40 public function __construct( $sep, array $wraps ) {
41 $this->sep = $sep;
42 $this->wraps = $wraps;
43 }
44
45 /**
46 * @param (string|WrappedString|WrappedStringList)[] $wraps
47 * @return WrappedStringList Combined list
48 */
49 protected function extend( array $wraps ) {
50 $list = clone $this;
51 $list->wraps = array_merge( $list->wraps, $wraps );
52 return $list;
53 }
54
55 /**
56 * Merge consecutive lists with the same separator.
57 *
58 * Does not modify the given array or any of the objects in it.
59 *
60 * @param (string|WrappedString|WrappedStringList)[] $lists
61 * @param string $outerSep Separator that the caller intends to use when joining the strings
62 * @return string[] Compacted list to be treated as strings
63 * (may contain WrappedString and WrappedStringList objects)
64 */
65 protected static function compact( array $lists, $outerSep ) {
66 $consolidated = [];
67 foreach ( $lists as $list ) {
68 if ( !$list instanceof WrappedStringList ) {
69 // Probably WrappedString or regular string,
70 // Not mergable as a list, but may be merged as a string
71 // later by WrappedString::compact.
72 $consolidated[] = $list;
73 continue;
74 }
75 if ( $list->sep === $outerSep ) {
76 $consolidated = array_merge(
77 $consolidated,
78 self::compact( $list->wraps, $outerSep )
79 );
80 } else {
81 $consolidated[] = $list;
82 }
83 }
84
85 return WrappedString::compact( $consolidated );
86 }
87
88 /**
89 * Join a several wrapped strings with a separator between each.
90 *
91 * @param string $sep
92 * @param (string|WrappedString|WrappedStringList)[] $lists
93 * @return string
94 */
95 public static function join( $sep, array $lists ) {
96 return implode( $sep, self::compact( $lists, $sep ) );
97 }
98
99 /** @return string */
100 public function __toString() {
101 return self::join( $this->sep, [ $this ] );
102 }
103 }