Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
WrappedStringList | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
compact | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
join | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | * Merge consecutive lists with the same separator. |
47 | * |
48 | * Does not modify the given array or any of the objects in it. |
49 | * |
50 | * @param (string|WrappedString|WrappedStringList)[] $lists |
51 | * @param string $outerSep Separator that the caller intends to use when joining the strings |
52 | * @return string[] Compacted list to be treated as strings |
53 | * (may contain WrappedString and WrappedStringList objects) |
54 | */ |
55 | protected static function compact( array $lists, $outerSep ) { |
56 | $consolidated = []; |
57 | foreach ( $lists as $list ) { |
58 | if ( !$list instanceof WrappedStringList ) { |
59 | // Probably WrappedString or regular string, |
60 | // Not mergable as a list, but may be merged as a string |
61 | // later by WrappedString::compact. |
62 | $consolidated[] = $list; |
63 | continue; |
64 | } |
65 | if ( $list->sep === $outerSep ) { |
66 | $consolidated = array_merge( |
67 | $consolidated, |
68 | self::compact( $list->wraps, $outerSep ) |
69 | ); |
70 | } else { |
71 | $consolidated[] = $list; |
72 | } |
73 | } |
74 | |
75 | return WrappedString::compact( $consolidated ); |
76 | } |
77 | |
78 | /** |
79 | * Join a several wrapped strings with a separator between each. |
80 | * |
81 | * @param string $sep |
82 | * @param (string|WrappedString|WrappedStringList)[] $lists |
83 | * @return string |
84 | */ |
85 | public static function join( $sep, array $lists ) { |
86 | return implode( $sep, self::compact( $lists, $sep ) ); |
87 | } |
88 | |
89 | /** @return string */ |
90 | public function __toString() { |
91 | return self::join( $this->sep, [ $this ] ); |
92 | } |
93 | } |