Debian Codegraph @

vendor/wikimedia/wrappedstring/src/WrappedString.php

raw
1 <?php
2 /**
3 * Copyright 2015 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 use DomainException;
30
31 class WrappedString {
32 /** @var string */
33 protected $value;
34
35 /** @var string|null */
36 protected $prefix;
37
38 /** @var string|null */
39 protected $suffix;
40
41 /**
42 * @param string $value
43 * @param string|null $prefix
44 * @param string|null $suffix
45 */
46 public function __construct( $value, $prefix = null, $suffix = null ) {
47 $prefixlen = strlen( $prefix );
48 if ( $prefixlen && substr( $value, 0, $prefixlen ) !== $prefix ) {
49 throw new DomainException( 'Prefix must match value' );
50 }
51 $suffixlen = strlen( $suffix );
52 if ( $suffixlen && substr( $value, -$suffixlen ) !== $suffix ) {
53 throw new DomainException( 'Suffix must match value' );
54 }
55
56 $this->value = $value;
57 $this->prefix = $prefix;
58 $this->suffix = $suffix;
59 }
60
61 /**
62 * @param string $value Value of a WrappedString with the same prefix and suffix
63 * @return WrappedString Newly wrapped string
64 */
65 protected function extend( $value ) {
66 $wrap = clone $this;
67 $suffixlen = strlen( $this->suffix );
68 // Remove the suffix (temporarily), to open the string for merging.
69 if ( $suffixlen ) {
70 $wrap->value = substr( $this->value, 0, -$suffixlen );
71 }
72 // Append the next value without prefix, thus ending with the suffix again.
73 $prefixlen = strlen( $this->prefix );
74 $wrap->value .= substr( $value, $prefixlen );
75 return $wrap;
76 }
77
78 /**
79 * Merge consecutive WrappedString objects with the same prefix and suffix.
80 *
81 * Does not modify the array or the WrappedString objects.
82 *
83 * NOTE: This is an internal method. Use join() or WrappedStringList instead.
84 *
85 * @param (string|WrappedString|WrappedStringList)[] $wraps
86 * @return string[] Compacted list to be treated as strings
87 * (may contain WrappedString and WrappedStringList objects)
88 */
89 public static function compact( array $wraps ) {
90 $consolidated = [];
91 if ( $wraps === [] ) {
92 // Return early so that we don't have to deal with $prev being
93 // set or not set, and avoid risk of adding $prev's initial null
94 // value to the list as extra value (T196496).
95 return $consolidated;
96 }
97 $first = true;
98 $prev = null;
99 foreach ( $wraps as $wrap ) {
100 if ( $first ) {
101 $first = false;
102 $prev = $wrap;
103 continue;
104 }
105 if ( $prev instanceof WrappedString
106 && $wrap instanceof WrappedString
107 && $prev->prefix !== null
108 && $prev->prefix === $wrap->prefix
109 && $prev->suffix !== null
110 && $prev->suffix === $wrap->suffix
111 ) {
112 $prev = $prev->extend( $wrap->value );
113 } else {
114 $consolidated[] = $prev;
115 $prev = $wrap;
116 }
117 }
118 // Add last one
119 $consolidated[] = $prev;
120
121 return $consolidated;
122 }
123
124 /**
125 * Join several wrapped strings with a separator between each.
126 *
127 * This method is compatibile with native PHP implode(). The actual join
128 * operation is deferred to WrappedStringList::__toString(). This allows
129 * callers to collect multiple lists and compact them together.
130 *
131 * @param string $sep
132 * @param (string|WrappedString|WrappedStringList)[] $wraps
133 * @return WrappedStringList
134 */
135 public static function join( $sep, array $wraps ) {
136 return new WrappedStringList( $sep, $wraps );
137 }
138
139 /** @return string */
140 public function __toString() {
141 return $this->value;
142 }
143 }