Completed
Push — master ( 5cd8a4...3af021 )
by Garrett
03:01
created

AString::wordwrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace StringObject;
4
5
class AString extends AnyString
6
{
7 View Code Duplication
    public function toArray($delim = '', $limit = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
    {
9
        if (empty($delim)) {
10
            return \str_split($this->raw);
11
        }
12
        if (is_int($delim)) {
13
            return \str_split($this->raw, $delim);
14
        }
15
        if ($limit === null) {
16
            return \explode($delim, $this->raw);
17
        }
18
        return \explode($delim, $this->raw, $limit);
19
    }
20
21
    // INFORMATIONAL METHODS
22
23
    public function charAt($offset)
24
    {
25
        return new static($this->raw{$offset});
26
    }
27
28
    /**
29
     * @param integer $offset
30
     */
31
    public function charCodeAt($offset)
32
    {
33
        return \ord($this->raw{$offset});
34
    }
35
36
    public function compareTo($str, $flags = self::NORMAL, $length = 1)
37
    {
38
        // strip out bits we don't understand
39
        $flags &= (self::CASE_INSENSITIVE | self::CURRENT_LOCALE | self::NATURAL_ORDER | self::FIRST_N);
40
41
        $flagsmap = [
42
            self::NORMAL => 'strcmp',
43
            self::CASE_INSENSITIVE => 'strcasecmp',
44
            self::CURRENT_LOCALE => 'strcoll',
45
            self::NATURAL_ORDER => 'strnatcmp',
46
            (self::NATURAL_ORDER | self::CASE_INSENSITIVE) => 'strnatcasecmp',
47
            self::FIRST_N => 'strncmp',
48
            (self::FIRST_N | self::CASE_INSENSITIVE) => 'strncasecmp',
49
        ];
50
51
        if ($flags & self::FIRST_N) {
52
            return \call_user_func($flagsmap[$flags], $this->raw, $str, $length);
53
        }
54
        return \call_user_func($flagsmap[$flags], $this->raw, $str);
55
    }
56
57
    public function indexOf($needle, $offset = 0, $flags = self::NORMAL)
58
    {
59
        // strip out bits we don't understand
60
        $flags &= (self::REVERSE | self::CASE_INSENSITIVE);
61
62
        $flagsmap = [
63
            self::NORMAL => 'strpos',
64
            self::CASE_INSENSITIVE => 'stripos',
65
            self::REVERSE => 'strrpos',
66
            (self::REVERSE | self::CASE_INSENSITIVE) => 'strripos',
67
        ];
68
        return \call_user_func($flagsmap[$flags], $this->raw, $needle, $offset);
69
    }
70
71
    public function length()
72
    {
73
        return \strlen($this->raw);
74
    }
75
76
    // MODIFYING METHODS
77
78
    public function append($str)
79
    {
80
        return $this->replaceWhole($this->raw . $str);
81
    }
82
83
    public function chunk($length = 76, $ending = "\r\n")
84
    {
85
        return $this->replaceWhole(\chunk_split($this->raw, $length, $ending));
86
    }
87
88
    public function escape($flags = self::NORMAL, $charlist = '')
89
    {
90
        // strip out bits we don't understand
91
        $flags &= (self::C_STYLE | self::META);
92
93
        $flagsmap = [
94
            self::NORMAL => 'addslashes',
95
            self::C_STYLE => 'addcslashes',
96
            self::META => 'quotemeta',
97
        ];
98
        if ($flags === self::C_STYLE) {
99
            return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw, $charlist));
100
        }
101
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw));
102
    }
103
104
    public function insertAt($str, $offset)
105
    {
106
        return $this->replaceSubstr($str, $offset, 0);
107
    }
108
109
    public function pad($newlength, $padding = ' ', $flags = self::END)
110
    {
111
        return $this->replaceWhole(\str_pad($this->raw, $newlength, $padding, $flags));
112
    }
113
114
    public function prepend($str)
115
    {
116
        return $this->replaceWhole($str . $this->raw);
117
    }
118
119
    public function remove($str, $flags = self::NORMAL)
120
    {
121
        return $this->replace($str, '', $flags);
122
    }
123
124
    public function removeSubstr($start, $length = null)
125
    {
126
        return $this->replaceSubstr('', $start, $length);
127
    }
128
129
    public function repeat($times)
130
    {
131
        return $this->replaceWhole(\str_repeat($this->raw, $times));
132
    }
133
134
    /**
135
     * @param string $replace
136
     */
137
    public function replace($search, $replace, $flags = self::NORMAL)
138
    {
139
        if ($flags & self::CASE_INSENSITIVE) {
140
            return $this->replaceWhole(\str_ireplace($search, $replace, $this->raw));
141
        }
142
        return $this->replaceWhole(\str_replace($search, $replace, $this->raw));
143
    }
144
145
    public function replaceSubstr($replacement, $start, $length = null)
146
    {
147
        if ($length === null) {
148
            $length = $this->length();
149
        }
150
        return $this->replaceWhole(\substr_replace($this->raw, $replacement, $start, $length));
151
    }
152
153
    public function reverse()
154
    {
155
        return $this->replaceWhole(\strrev($this->raw));
156
    }
157
158
    public function shuffle()
159
    {
160
        return $this->replaceWhole(\str_shuffle($this->raw));
161
    }
162
163
    public function substr($start, $length = 'omitted')
164
    {
165
        if ($length === 'omitted') {
166
            return new static(\substr($this->raw, $start));
167
        }
168
        return new static(\substr($this->raw, $start, $length));
169
    }
170
171
    public function translate($search, $replace = '')
172
    {
173
        if (is_array($search)) {
174
            return $this->replaceWhole(\strtr($this->raw, $search));
175
        }
176
        return $this->replaceWhole(\strtr($this->raw, $search, $replace));
177
    }
178
179 View Code Duplication
    public function trim($mask = " \t\n\r\0\x0B", $flags = self::BOTH_ENDS)
180
    {
181
        // strip out bits we don't understand
182
        $flags &= (self::END | self::BOTH_ENDS);
183
184
        $flagsmap = [
185
            self::START => 'ltrim',
186
            self::END => 'rtrim',
187
            self::BOTH_ENDS => 'trim',
188
        ];
189
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw, $mask));
190
    }
191
192 View Code Duplication
    public function unescape($flags = self::NORMAL)
193
    {
194
        // strip out bits we don't understand
195
        $flags &= (self::C_STYLE | self::META);
196
197
        $flagsmap = [
198
            self::NORMAL => 'stripslashes',
199
            self::C_STYLE => 'stripcslashes',
200
            self::META => 'stripslashes',
201
        ];
202
        return $this->replaceWhole(\call_user_func($flagsmap[$flags], $this->raw));
203
    }
204
205
    // TESTING METHODS
206
207
    public function contains($needle, $offset = 0, $flags = self::NORMAL)
208
    {
209
        if ($flags & self::EXACT_POSITION) {
210
            return ($this->indexOf($needle, $offset, $flags) === $offset);
211
        }
212
        return ($this->indexOf($needle, $offset, $flags) !== false);
213
    }
214
215
    public function countSubstr($needle, $offset = 0, $length = null)
216
    {
217
        if ($length === null) {
218
            return \substr_count($this->raw, $needle, $offset);
219
        }
220
        return \substr_count($this->raw, $needle, $offset, $length);
221
    }
222
223
    public function endsWith($str, $flags = self::NORMAL)
224
    {
225
        $flags &= self::CASE_INSENSITIVE;
226
        $offset = $this->length() - \strlen($str);
227
        return $this->contains($str, $offset, $flags | self::EXACT_POSITION | self::REVERSE);
228
    }
229
230
    public function equals($str)
231
    {
232
        self::testStringableObject($str);
233
234
        $str = (string) $str;
235
        return ($str == $this->raw);
236
    }
237
238
    public function isAscii()
239
    {
240
        $len = $this->length();
241
242
        for ($i = 0; $i < $len; $i++) {
243
            if ($this->charCodeAt($i) >= 128) {
244
                return false;
245
            }
246
        }
247
        return true;
248
    }
249
250
    public function isEmpty()
251
    {
252
        return empty($this->raw);
253
    }
254
255
    public function startsWith($str, $flags = self::NORMAL)
256
    {
257
        $flags &= self::CASE_INSENSITIVE;
258
        return $this->contains($str, 0, $flags | self::EXACT_POSITION);
259
    }
260
261
    // INTERFACE IMPLEMENTATION METHODS
262
263
    public function count()
264
    {
265
        return \strlen($this->raw);
266
    }
267
268
    public function current()
269
    {
270
        return $this->raw[$this->caret];
271
    }
272
273
    public function offsetGet($offset)
274
    {
275
        return $this->raw{$offset};
276
    }
277
278
    public function offsetSet($offset, $value)
279
    {
280
        throw new \LogicException('Cannot assign ' . $value . ' to immutable AString instance at index ' . $offset);
281
    }
282
283
    public function offsetUnset($offset)
284
    {
285
        throw new \LogicException('Cannot unset index ' . $offset . ' on immutable AString instance');
286
    }
287
}
288