Passed
Pull Request — main (#121)
by Andrey
12:11
created

Stringable::end()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
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
     * End a string with a single instance of a given value.
58
     *
59
     * @param  string  $suffix
60
     *
61
     * @return $this
62
     */
63 2
    public function end(string $suffix): self
64
    {
65 2
        return new self(Str::end($this->value, $suffix));
66
    }
67
68
    /**
69
     * Cap a string with a single instance of a given value.
70
     *
71
     * @param  string  $cap
72
     *
73 2
     * @return \Helldar\Support\Helpers\Ables\Stringable
74
     */
75 2
    public function finish(string $cap = '/'): self
76
    {
77
        return new self(Str::finish($this->value, $cap));
78
    }
79
80
    /**
81
     * Convert the given string to lower-case.
82
     *
83 4
     * @return \Helldar\Support\Helpers\Ables\Stringable
84
     */
85 4
    public function lower(): self
86
    {
87
        return new self(Str::lower($this->value));
88
    }
89
90
    /**
91
     * Convert the given string to upper-case.
92
     *
93 2
     * @return \Helldar\Support\Helpers\Ables\Stringable
94
     */
95 2
    public function upper(): self
96
    {
97
        return new self(Str::upper($this->value));
98
    }
99
100
    /**
101
     * Convert a value to studly caps case.
102
     *
103 4
     * @return \Helldar\Support\Helpers\Ables\Stringable
104
     */
105 4
    public function studly(): self
106
    {
107
        return new self(Str::studly($this->value));
108
    }
109
110
    /**
111
     * Convert a value to camel case.
112
     *
113
     * @return \Helldar\Support\Helpers\Ables\Stringable
114
     */
115 2
    public function camel(): self
116
    {
117 2
        return new self(Str::camel($this->value));
118
    }
119
120
    /**
121
     * Convert a string to snake case.
122
     *
123
     * @param  string|null  $delimiter
124
     *
125
     * @return \Helldar\Support\Helpers\Ables\Stringable
126
     */
127
    public function snake(?string $delimiter = '_'): self
128 6
    {
129
        return new self(Str::snake($this->value, $delimiter));
130 6
    }
131
132
    /**
133
     * Generate a URL friendly "slug" from a given string.
134
     *
135
     * @param  string  $separator
136
     * @param  string|null  $language
137
     *
138 2
     * @return \Helldar\Support\Helpers\Ables\Stringable
139
     */
140 2
    public function slug(string $separator = '-', ?string $language = 'en'): self
141
    {
142
        return new self(Str::slug($this->value, $separator, $language));
143
    }
144
145
    /**
146
     * Convert the given string to title case.
147
     *
148
     * @return \Helldar\Support\Helpers\Ables\Stringable
149
     */
150
    public function title(): self
151 2
    {
152
        return new self(Str::title($this->value));
153 2
    }
154
155
    /**
156
     * Returns the portion of string specified by the start and length parameters.
157
     *
158
     * @param  int  $start
159
     * @param  int|null  $length
160
     *
161
     * @return \Helldar\Support\Helpers\Ables\Stringable
162
     */
163
    public function substr(int $start, int $length = null): self
164 4
    {
165
        return new self(Str::substr($this->value, $start, $length));
166 4
    }
167
168
    /**
169
     * Replace all occurrences of the search string with the replacement string.
170
     *
171
     * @param  array  $values
172
     * @param  string|null  $key_format
173
     *
174
     * @return \Helldar\Support\Helpers\Ables\Stringable
175
     */
176 4
    public function replace(array $values, string $key_format = null): self
177
    {
178 4
        return new self(Str::replace($this->value, $values, $key_format));
179
    }
180
181
    /**
182
     * Get the portion of a string before 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 before(string $search): self
189
    {
190 8
        return new self(Str::before($this->value, $search));
191
    }
192
193
    /**
194
     * Return the remainder of a string after the first occurrence of a given value.
195
     *
196
     * @param  string  $search
197
     *
198
     * @return \Helldar\Support\Helpers\Ables\Stringable
199
     */
200
    public function after(string $search): self
201
    {
202 6
        return new self(Str::after($this->value, $search));
203
    }
204 6
205
    /**
206
     * Strip whitespace (or other characters) from the beginning and end of a string.
207
     *
208
     * @see  https://php.net/manual/en/function.trim.php
209
     *
210
     * @param  string  $characters
211
     *
212
     * @return $this
213
     */
214 2
    public function trim(string $characters = " \t\n\r\0\x0B"): self
215
    {
216 2
        return new self(trim($this->value, $characters));
217
    }
218
219
    /**
220
     * Get the string matching the given pattern.
221
     *
222
     * @param  string  $pattern
223
     *
224
     * @return \Helldar\Support\Helpers\Ables\Stringable
225
     */
226
    public function match(string $pattern): self
227 2
    {
228
        return new self(Str::match($this->value, $pattern));
229 2
    }
230
231
    /**
232
     * Replace a given value in the string.
233
     *
234
     * @param  string  $pattern
235
     * @param  string  $replacement
236
     *
237
     * @return \Helldar\Support\Helpers\Ables\Stringable
238
     */
239 4
    public function pregReplace(string $pattern, string $replacement): self
240
    {
241 4
        return new self(Str::pregReplace($this->value, $pattern, $replacement));
242
    }
243
244
    /**
245
     * Transliterate a UTF-8 value to ASCII.
246
     *
247
     * @param  string|null  $language
248
     *
249
     * @return \Helldar\Support\Helpers\Ables\Stringable
250
     */
251
    public function ascii(?string $language = 'en'): self
252
    {
253
        return new self(Str::ascii($this->value, $language));
254
    }
255
256
    /**
257
     * Outputs the contents of a variable without terminating the application.
258
     *
259
     * @return $this
260
     */
261
    public function dump(): self
262
    {
263
        dump($this->value);
264
265
        return $this;
266
    }
267
268
    /**
269
     * Outputs the contents of a variable, terminating the application.
270
     */
271
    public function dd(): void
272
    {
273
        dd($this->value);
274
    }
275
}
276