Stringable::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/*
3
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Helpers\Ables;
18
19
use Helldar\Contracts\Support\Stringable as Contract;
20
use Helldar\Support\Facades\Helpers\Str;
21
22
class Stringable implements Contract
23
{
24
    protected $value;
25
26 236
    public function __construct(?string $value = null)
27
    {
28 236
        $this->value = (string) $value;
29 236
    }
30
31 236
    public function __toString()
32
    {
33 236
        return $this->value;
34
    }
35
36
    /**
37
     * Creates the current object in case of accessing the Stringable through the facade.
38
     *
39
     * @param  string|null  $value
40
     *
41
     * @return \Helldar\Support\Helpers\Ables\Stringable
42
     */
43 21
    public function of(?string $value): self
44
    {
45 21
        return new self($value);
46
    }
47
48
    /**
49
     * Replacing multiple spaces with a single space.
50
     *
51
     * @return \Helldar\Support\Helpers\Ables\Stringable
52
     */
53 4
    public function removeSpaces(): self
54
    {
55 4
        return new self(Str::removeSpaces($this->value));
56
    }
57
58
    /**
59
     * Begin a string with a single instance of a given value.
60
     *
61
     * @param  string  $prefix
62
     *
63
     * @return \Helldar\Support\Helpers\Ables\Stringable
64
     */
65 188
    public function start(string $prefix): self
66
    {
67 188
        return new self(Str::start($this->value, $prefix));
68
    }
69
70
    /**
71
     * End a string with a single instance of a given value.
72
     *
73
     * @param  string  $suffix
74
     *
75
     * @return $this
76
     */
77 2
    public function end(string $suffix): self
78
    {
79 2
        return new self(Str::end($this->value, $suffix));
80
    }
81
82
    /**
83
     * Cap a string with a single instance of a given value.
84
     *
85
     * @param  string  $cap
86
     *
87
     * @return \Helldar\Support\Helpers\Ables\Stringable
88
     */
89 20
    public function finish(string $cap = '/'): self
90
    {
91 20
        return new self(Str::finish($this->value, $cap));
92
    }
93
94
    /**
95
     * Convert the given string to lower-case.
96
     *
97
     * @return \Helldar\Support\Helpers\Ables\Stringable
98
     */
99 2
    public function lower(): self
100
    {
101 2
        return new self(Str::lower($this->value));
102
    }
103
104
    /**
105
     * Convert the given string to upper-case.
106
     *
107
     * @return \Helldar\Support\Helpers\Ables\Stringable
108
     */
109 4
    public function upper(): self
110
    {
111 4
        return new self(Str::upper($this->value));
112
    }
113
114
    /**
115
     * Convert a value to studly caps case.
116
     *
117
     * @return \Helldar\Support\Helpers\Ables\Stringable
118
     */
119 2
    public function studly(): self
120
    {
121 2
        return new self(Str::studly($this->value));
122
    }
123
124
    /**
125
     * Convert a value to camel case.
126
     *
127
     * @return \Helldar\Support\Helpers\Ables\Stringable
128
     */
129 180
    public function camel(): self
130
    {
131 180
        return new self(Str::camel($this->value));
132
    }
133
134
    /**
135
     * Convert a string to snake case.
136
     *
137
     * @param  string|null  $delimiter
138
     *
139
     * @return \Helldar\Support\Helpers\Ables\Stringable
140
     */
141 2
    public function snake(?string $delimiter = '_'): self
142
    {
143 2
        return new self(Str::snake($this->value, $delimiter));
144
    }
145
146
    /**
147
     * Generate a URL friendly "slug" from a given string.
148
     *
149
     * @param  string  $separator
150
     * @param  string|null  $language
151
     *
152
     * @return \Helldar\Support\Helpers\Ables\Stringable
153
     */
154 6
    public function slug(string $separator = '-', ?string $language = 'en'): self
155
    {
156 6
        return new self(Str::slug($this->value, $separator, $language));
157
    }
158
159
    /**
160
     * Convert the given string to title case.
161
     *
162
     * @return \Helldar\Support\Helpers\Ables\Stringable
163
     */
164 2
    public function title(): self
165
    {
166 2
        return new self(Str::title($this->value));
167
    }
168
169
    /**
170
     * Returns the portion of string specified by the start and length parameters.
171
     *
172
     * @param  int  $start
173
     * @param  int|null  $length
174
     *
175
     * @return \Helldar\Support\Helpers\Ables\Stringable
176
     */
177 2
    public function substr(int $start, int $length = null): self
178
    {
179 2
        return new self(Str::substr($this->value, $start, $length));
180
    }
181
182
    /**
183
     * Replace all occurrences of the search string with the replacement string.
184
     *
185
     * @param  array  $values
186
     * @param  string|null  $key_format
187
     *
188
     * @return \Helldar\Support\Helpers\Ables\Stringable
189
     */
190 4
    public function replace(array $values, string $key_format = null): self
191
    {
192 4
        return new self(Str::replace($this->value, $values, $key_format));
193
    }
194
195
    /**
196
     * Get the portion of a string before the first occurrence of a given value.
197
     *
198
     * @param  string  $search
199
     *
200
     * @return \Helldar\Support\Helpers\Ables\Stringable
201
     */
202 4
    public function before(string $search): self
203
    {
204 4
        return new self(Str::before($this->value, $search));
205
    }
206
207
    /**
208
     * Return the remainder of a string after the first occurrence of a given value.
209
     *
210
     * @param  string  $search
211
     *
212
     * @return \Helldar\Support\Helpers\Ables\Stringable
213
     */
214 8
    public function after(string $search): self
215
    {
216 8
        return new self(Str::after($this->value, $search));
217
    }
218
219
    /**
220
     * Strip whitespace (or other characters) from the beginning and end of a string.
221
     *
222
     * @see  https://php.net/manual/en/function.trim.php
223
     *
224
     * @param  string  $characters
225
     *
226
     * @return $this
227
     */
228 99
    public function trim(string $characters = " \t\n\r\0\x0B"): self
229
    {
230 99
        return new self(trim($this->value, $characters));
231
    }
232
233
    /**
234
     * Get the string matching the given pattern.
235
     *
236
     * @param  string  $pattern
237
     *
238
     * @return \Helldar\Support\Helpers\Ables\Stringable
239
     */
240 2
    public function match(string $pattern): self
241
    {
242 2
        return new self(Str::match($this->value, $pattern));
243
    }
244
245
    /**
246
     * Replace a given value in the string.
247
     *
248
     * @param  string  $pattern
249
     * @param  string  $replacement
250
     *
251
     * @return \Helldar\Support\Helpers\Ables\Stringable
252
     */
253 2
    public function pregReplace(string $pattern, string $replacement): self
254
    {
255 2
        return new self(Str::pregReplace($this->value, $pattern, $replacement));
256
    }
257
258
    /**
259
     * Transliterate a UTF-8 value to ASCII.
260
     *
261
     * @param  string|null  $language
262
     *
263
     * @return \Helldar\Support\Helpers\Ables\Stringable
264
     */
265 4
    public function ascii(?string $language = 'en'): self
266
    {
267 4
        return new self(Str::ascii($this->value, $language));
268
    }
269
270
    /**
271
     * Outputs the contents of a variable without terminating the application.
272
     *
273
     * @return $this
274
     */
275
    public function dump(): self
276
    {
277
        dump($this->value);
278
279
        return $this;
280
    }
281
282
    /**
283
     * Outputs the contents of a variable, terminating the application.
284
     */
285
    public function dd(): void
286
    {
287
        dd($this->value);
288
    }
289
}
290