Passed
Pull Request — main (#104)
by Andrey
14:17
created

Stringable::studly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Str as Helper;
6
use Stringable as StringableContract;
7
8
final class Stringable implements StringableContract
9
{
10
    protected $value;
11
12
    public function __construct(?string $value = null)
13
    {
14
        $this->value = (string) $value;
15
    }
16
17
    public function __toString(): string
18
    {
19
        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 $this
28
     */
29
    public function of(?string $value): self
30
    {
31
        return new self($value);
32
    }
33
34
    /**
35
     * Replacing multiple spaces with a single space.
36
     *
37
     * @return \Helldar\Support\Helpers\Stringable
38
     */
39
    public function removeSpaces(): self
40
    {
41
        return new self(Helper::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\Stringable
50
     */
51
    public function start(string $prefix): self
52
    {
53
        return new self(Helper::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\Stringable
62
     */
63
    public function finish(string $cap = '/'): self
64
    {
65
        return new self(Helper::finish($this->value, $cap));
66
    }
67
68
    /**
69
     * Convert the given string to lower-case.
70
     *
71
     * @return \Helldar\Support\Helpers\Stringable
72
     */
73
    public function lower(): self
74
    {
75
        return new self(Helper::lower($this->value));
76
    }
77
78
    /**
79
     * Convert the given string to upper-case.
80
     *
81
     * @return \Helldar\Support\Helpers\Stringable
82
     */
83
    public function upper(): self
84
    {
85
        return new self(Helper::upper($this->value));
86
    }
87
88
    /**
89
     * Convert a value to studly caps case.
90
     *
91
     * @return \Helldar\Support\Helpers\Stringable
92
     */
93
    public function studly(): self
94
    {
95
        return new self(Helper::studly($this->value));
96
    }
97
98
    /**
99
     * Convert a value to camel case.
100
     *
101
     * @return \Helldar\Support\Helpers\Stringable
102
     */
103
    public function camel(): self
104
    {
105
        return new self(Helper::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\Stringable
114
     */
115
    public function snake(?string $delimiter = '_'): self
116
    {
117
        return new self(Helper::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\Stringable
127
     */
128
    public function slug(string $separator = '-', ?string $language = 'en'): self
129
    {
130
        return new self(Helper::slug($this->value, $separator, $language));
131
    }
132
133
    /**
134
     * Convert the given string to title case.
135
     *
136
     * @return \Helldar\Support\Helpers\Stringable
137
     */
138
    public function title(): self
139
    {
140
        return new self(Helper::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\Stringable
150
     */
151
    public function substr(int $start, int $length = null): self
152
    {
153
        return new self(Helper::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\Stringable
163
     */
164
    public function replace(array $values, string $key_format = null): self
165
    {
166
        return new self(Helper::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\Stringable
175
     */
176
    public function before(string $search): self
177
    {
178
        return new self(Helper::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\Stringable
187
     */
188
    public function after(string $search): self
189
    {
190
        return new self(Helper::after($this->value, $search));
191
    }
192
193
    /**
194
     * Transliterate a UTF-8 value to ASCII.
195
     *
196
     * @param  string|null  $language
197
     *
198
     * @return \Helldar\Support\Helpers\Stringable
199
     */
200
    public function ascii(?string $language = 'en'): self
201
    {
202
        return new self(Helper::ascii($this->value, $language));
203
    }
204
}
205