Passed
Push — main ( 1c7dfd...e0e27b )
by Andrey
16:04 queued 14:32
created

Stringable::replace()   A

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
namespace Helldar\Support\Helpers\Ables;
4
5
use Helldar\Support\Facades\Helpers\Str;
6
use Stringable as Contract;
7
8
class Stringable implements Contract
9
{
10
    protected $value;
11
12 45
    public function __construct(?string $value = null)
13
    {
14 45
        $this->value = (string) $value;
15 45
    }
16
17 45
    public function __toString(): string
18
    {
19 45
        return $this->value;
20
    }
21
22
    /**
23
     * Creates the current object in case of accessing the Stringable through the facade.
24
     *
25
     * @param  string|null  $value
26
     *
27
     * @return \Helldar\Support\Helpers\Ables\Stringable
28
     */
29 20
    public function of(?string $value): self
30
    {
31 20
        return new self($value);
32
    }
33
34
    /**
35
     * Replacing multiple spaces with a single space.
36
     *
37
     * @return \Helldar\Support\Helpers\Ables\Stringable
38
     */
39 4
    public function removeSpaces(): self
40
    {
41 4
        return new self(Str::removeSpaces($this->value));
42
    }
43
44
    /**
45
     * Begin a string with a single instance of a given value.
46
     *
47
     * @param  string  $prefix
48
     *
49
     * @return \Helldar\Support\Helpers\Ables\Stringable
50
     */
51 4
    public function start(string $prefix): self
52
    {
53 4
        return new self(Str::start($this->value, $prefix));
54
    }
55
56
    /**
57
     * Cap a string with a single instance of a given value.
58
     *
59
     * @param  string  $cap
60
     *
61
     * @return \Helldar\Support\Helpers\Ables\Stringable
62
     */
63 2
    public function finish(string $cap = '/'): self
64
    {
65 2
        return new self(Str::finish($this->value, $cap));
66
    }
67
68
    /**
69
     * Convert the given string to lower-case.
70
     *
71
     * @return \Helldar\Support\Helpers\Ables\Stringable
72
     */
73 2
    public function lower(): self
74
    {
75 2
        return new self(Str::lower($this->value));
76
    }
77
78
    /**
79
     * Convert the given string to upper-case.
80
     *
81
     * @return \Helldar\Support\Helpers\Ables\Stringable
82
     */
83 4
    public function upper(): self
84
    {
85 4
        return new self(Str::upper($this->value));
86
    }
87
88
    /**
89
     * Convert a value to studly caps case.
90
     *
91
     * @return \Helldar\Support\Helpers\Ables\Stringable
92
     */
93 2
    public function studly(): self
94
    {
95 2
        return new self(Str::studly($this->value));
96
    }
97
98
    /**
99
     * Convert a value to camel case.
100
     *
101
     * @return \Helldar\Support\Helpers\Ables\Stringable
102
     */
103 4
    public function camel(): self
104
    {
105 4
        return new self(Str::camel($this->value));
106
    }
107
108
    /**
109
     * Convert a string to snake case.
110
     *
111
     * @param  string|null  $delimiter
112
     *
113
     * @return \Helldar\Support\Helpers\Ables\Stringable
114
     */
115 2
    public function snake(?string $delimiter = '_'): self
116
    {
117 2
        return new self(Str::snake($this->value, $delimiter));
118
    }
119
120
    /**
121
     * Generate a URL friendly "slug" from a given string.
122
     *
123
     * @param  string  $separator
124
     * @param  string|null  $language
125
     *
126
     * @return \Helldar\Support\Helpers\Ables\Stringable
127
     */
128 6
    public function slug(string $separator = '-', ?string $language = 'en'): self
129
    {
130 6
        return new self(Str::slug($this->value, $separator, $language));
131
    }
132
133
    /**
134
     * Convert the given string to title case.
135
     *
136
     * @return \Helldar\Support\Helpers\Ables\Stringable
137
     */
138 2
    public function title(): self
139
    {
140 2
        return new self(Str::title($this->value));
141
    }
142
143
    /**
144
     * Returns the portion of string specified by the start and length parameters.
145
     *
146
     * @param  int  $start
147
     * @param  int|null  $length
148
     *
149
     * @return \Helldar\Support\Helpers\Ables\Stringable
150
     */
151 2
    public function substr(int $start, int $length = null): self
152
    {
153 2
        return new self(Str::substr($this->value, $start, $length));
154
    }
155
156
    /**
157
     * Replace all occurrences of the search string with the replacement string.
158
     *
159
     * @param  array  $values
160
     * @param  string|null  $key_format
161
     *
162
     * @return \Helldar\Support\Helpers\Ables\Stringable
163
     */
164 4
    public function replace(array $values, string $key_format = null): self
165
    {
166 4
        return new self(Str::replace($this->value, $values, $key_format));
167
    }
168
169
    /**
170
     * Get the portion of a string before the first occurrence of a given value.
171
     *
172
     * @param  string  $search
173
     *
174
     * @return \Helldar\Support\Helpers\Ables\Stringable
175
     */
176 4
    public function before(string $search): self
177
    {
178 4
        return new self(Str::before($this->value, $search));
179
    }
180
181
    /**
182
     * Return the remainder of a string after the first occurrence of a given value.
183
     *
184
     * @param  string  $search
185
     *
186
     * @return \Helldar\Support\Helpers\Ables\Stringable
187
     */
188 8
    public function after(string $search): self
189
    {
190 8
        return new self(Str::after($this->value, $search));
191
    }
192
193
    /**
194
     * Strip whitespace (or other characters) from the beginning and end of a string.
195
     *
196
     * @see  https://php.net/manual/en/function.trim.php
197
     *
198
     * @param  string  $characters
199
     *
200
     * @return $this
201
     */
202 6
    public function trim(string $characters = " \t\n\r\0\x0B"): self
203
    {
204 6
        return new self(trim($this->value, $characters));
205
    }
206
207
    /**
208
     * Get the string matching the given pattern.
209
     *
210
     * @param  string  $pattern
211
     *
212
     * @return \Helldar\Support\Helpers\Ables\Stringable
213
     */
214 2
    public function match(string $pattern): self
215
    {
216 2
        return new self(Str::match($this->value, $pattern));
217
    }
218
219
    /**
220
     * Replace a given value in the string.
221
     *
222
     * @param  string  $pattern
223
     * @param  string  $replacement
224
     *
225
     * @return \Helldar\Support\Helpers\Ables\Stringable
226
     */
227 2
    public function pregReplace(string $pattern, string $replacement): self
228
    {
229 2
        return new self(Str::pregReplace($this->value, $pattern, $replacement));
230
    }
231
232
    /**
233
     * Transliterate a UTF-8 value to ASCII.
234
     *
235
     * @param  string|null  $language
236
     *
237
     * @return \Helldar\Support\Helpers\Ables\Stringable
238
     */
239 4
    public function ascii(?string $language = 'en'): self
240
    {
241 4
        return new self(Str::ascii($this->value, $language));
242
    }
243
}
244