@@ -6,38 +6,28 @@ class TitleFormatter
6
6
{
7
7
/**
8
8
* The title that we need to format and return.
9
- *
10
- * @var string|null
11
9
*/
12
- protected $ title ;
10
+ protected ? string $ title = null ;
13
11
14
12
/**
15
13
* The separator character for in between words.
16
- *
17
- * @var string
18
14
*/
19
- protected $ separator = ' ' ;
15
+ protected string $ separator = ' ' ;
20
16
21
17
/**
22
18
* Encoding used for mb_ functions.
23
- *
24
- * @var string
25
19
*/
26
- protected $ encoding = 'UTF-8 ' ;
20
+ protected string $ encoding = 'UTF-8 ' ;
27
21
28
22
/**
29
23
* Collection of words generated from the original title.
30
- *
31
- * @var array
32
24
*/
33
- protected $ indexedWords = [];
25
+ protected array $ indexedWords = [];
34
26
35
27
/**
36
28
* Words that should be ignored from capitalization.
37
- *
38
- * @var array
39
29
*/
40
- protected $ ignoredWords = [
30
+ protected array $ ignoredWords = [
41
31
'a ' ,
42
32
'an ' ,
43
33
'and ' ,
@@ -59,22 +49,16 @@ class TitleFormatter
59
49
'via ' ,
60
50
];
61
51
62
- /**
63
- * @param string $title
64
- * @param string $separator
65
- */
66
- private function __construct ($ title , $ separator = ' ' )
52
+ private function __construct (?string $ title = null , string $ separator = ' ' )
67
53
{
68
54
$ this ->setTitle ($ title );
69
55
$ this ->separator = $ separator ;
70
56
}
71
57
72
58
/**
73
59
* Converts the initial title to a correctly formatted one.
74
- *
75
- * @return string
76
60
*/
77
- public function convertTitle ()
61
+ public function convertTitle (): string
78
62
{
79
63
if ($ this ->title === null ) {
80
64
return '' ;
@@ -86,46 +70,39 @@ public function convertTitle()
86
70
}
87
71
}
88
72
89
- return $ this ->title ;
73
+ return $ this ->title ?? '' ;
90
74
}
91
75
92
76
/**
93
77
* Returns the newly formatted title.
94
- *
95
- * @param string $title
96
- * @param string $separator
97
- * @return string
98
78
*/
99
- public static function titleCase ($ title, $ separator = ' ' )
79
+ public static function titleCase (? string $ title = null , string $ separator = ' ' ): string
100
80
{
101
81
return (new self ($ title , $ separator ))->convertTitle ();
102
82
}
103
83
104
84
/**
105
85
* Sets the title after cleaning up extra spaces.
106
- *
107
- * @param string $title
108
86
*/
109
- protected function setTitle ($ title)
87
+ protected function setTitle (? string $ title = null ): void
110
88
{
111
89
$ title = trim (preg_replace ('/\s\s+/ ' , ' ' , str_replace ("\n" , ' ' , $ title )));
112
90
113
- if ($ title != '' ) {
91
+ if ($ title !== '' ) {
114
92
$ this ->title = $ title ;
115
93
}
116
94
}
117
95
118
96
/**
119
97
* Creates an array of words from the title to be formatted.
120
98
*/
121
- protected function splitWords ()
99
+ protected function splitWords (): array
122
100
{
123
101
$ indexedWords = [];
124
102
$ offset = 0 ;
125
103
126
- $ words = explode ($ this ->separator , $ this ->title );
127
- foreach ($ words as $ word ) {
128
- if (mb_strlen ($ word , $ this ->encoding ) == 0 ) {
104
+ foreach (explode ($ this ->separator , $ this ->title ) as $ word ) {
105
+ if (mb_strlen ($ word , $ this ->encoding ) === 0 ) {
129
106
continue ;
130
107
}
131
108
@@ -145,12 +122,8 @@ protected function splitWords()
145
122
146
123
/**
147
124
* Finds the correct index of the word within the title.
148
- *
149
- * @param $word
150
- * @param $offset
151
- * @return int
152
125
*/
153
- protected function getWordIndex ($ word , $ offset )
126
+ protected function getWordIndex (string $ word , int $ offset ): int
154
127
{
155
128
$ index = mb_strpos ($ this ->title , $ word , $ offset , $ this ->encoding );
156
129
@@ -159,22 +132,16 @@ protected function getWordIndex($word, $offset)
159
132
160
133
/**
161
134
* Corrects the potential offset issue with some UTF-8 characters.
162
- *
163
- * @param $index
164
- * @return int
165
135
*/
166
- protected function correctIndexOffset ($ index )
136
+ protected function correctIndexOffset (? int $ index ): int
167
137
{
168
138
return mb_strlen (mb_substr ($ this ->title , 0 , $ index , $ this ->encoding ), $ this ->encoding );
169
139
}
170
140
171
141
/**
172
142
* Replaces a formatted word within the current title.
173
- *
174
- * @param int $index
175
- * @param string $word
176
143
*/
177
- protected function rebuildTitle ($ index , $ word )
144
+ protected function rebuildTitle (int $ index , string $ word ): void
178
145
{
179
146
$ this ->title =
180
147
mb_substr ($ this ->title , 0 , $ index , $ this ->encoding ) .
@@ -189,11 +156,8 @@ protected function rebuildTitle($index, $word)
189
156
190
157
/**
191
158
* Performs the uppercase action on the given word.
192
- *
193
- * @param $word
194
- * @return string
195
159
*/
196
- protected function uppercaseWord ($ word )
160
+ protected function uppercaseWord (string $ word ): string
197
161
{
198
162
// see if first characters are special
199
163
$ prefix = '' ;
@@ -213,31 +177,24 @@ protected function uppercaseWord($word)
213
177
214
178
/**
215
179
* Condition to see if the given word should be uppercase.
216
- *
217
- * @param $index
218
- * @param $word
219
- * @return bool
220
180
*/
221
- protected function wordShouldBeUppercase ($ index , $ word )
181
+ protected function wordShouldBeUppercase (int $ index , string $ word ): bool
222
182
{
223
183
return
224
184
(
225
185
$ this ->isFirstWordOfSentence ($ index ) ||
226
186
$ this ->isLastWord ($ word ) ||
227
- !$ this ->isIgnoredWord ($ word )
187
+ ! $ this ->isIgnoredWord ($ word )
228
188
) &&
229
189
(
230
- !$ this ->hasUppercaseLetter ($ word )
190
+ ! $ this ->hasUppercaseLetter ($ word )
231
191
);
232
192
}
233
193
234
194
/**
235
195
* Checks to see if the word is the last word in the title.
236
- *
237
- * @param $word
238
- * @return bool
239
196
*/
240
- protected function isLastWord ($ word )
197
+ protected function isLastWord (string $ word ): bool
241
198
{
242
199
if ($ word === end ($ this ->indexedWords )) {
243
200
return true ;
@@ -247,14 +204,11 @@ protected function isLastWord($word)
247
204
}
248
205
249
206
/**
250
- * Checks to see if the word the start of a new sentence.
251
- *
252
- * @param $index
253
- * @return bool
207
+ * Checks to see if the word is the start of a new sentence.
254
208
*/
255
- protected function isFirstWordOfSentence ($ index )
209
+ protected function isFirstWordOfSentence (int $ index ): bool
256
210
{
257
- if ($ index == 0 ) {
211
+ if ($ index === 0 ) {
258
212
return true ;
259
213
}
260
214
@@ -269,47 +223,35 @@ protected function isFirstWordOfSentence($index)
269
223
270
224
/**
271
225
* Checks to see if the given string is a punctuation character.
272
- *
273
- * @param $string
274
- * @return int
275
226
*/
276
- protected function isPunctuation ($ string )
227
+ protected function isPunctuation (string $ string ): int
277
228
{
278
229
return preg_match ("/[\p{P}\p{S}]/u " , $ string );
279
230
}
280
231
281
232
/**
282
233
* Checks if the given word should be ignored.
283
- *
284
- * @param $word
285
- * @return bool
286
234
*/
287
- protected function isIgnoredWord ($ word )
235
+ protected function isIgnoredWord (string $ word ): bool
288
236
{
289
- return in_array ($ word , $ this ->ignoredWords );
237
+ return in_array ($ word , $ this ->ignoredWords , true );
290
238
}
291
239
292
240
/**
293
241
* Checks to see if a word has an uppercase letter.
294
- *
295
- * @param $word
296
- * @return int
297
242
*/
298
- protected function hasUppercaseLetter ($ word )
243
+ protected function hasUppercaseLetter (string $ word ): int
299
244
{
300
245
return preg_match ('/[A-Z]/ ' , $ word );
301
246
}
302
247
303
248
/**
304
249
* Checks to see if the word has a dash.
305
- *
306
- * @param $word
307
- * @return int
308
250
*/
309
- protected function hasDash ($ word )
251
+ protected function hasDash (string $ word ): bool
310
252
{
311
253
$ wordWithoutDashes = str_replace ('- ' , '' , $ word );
312
254
313
- return preg_match ("/\-/ " , $ word ) && mb_strlen ($ wordWithoutDashes , $ this ->encoding ) > 1 ;
255
+ return ( bool ) preg_match ("/\-/ " , $ word ) && mb_strlen ($ wordWithoutDashes , $ this ->encoding ) > 1 ;
314
256
}
315
257
}
0 commit comments