TypogrifyService   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
eloc 46
c 8
b 1
f 0
dl 0
loc 344
rs 10
wmc 26

15 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 17 4
A humanFileSize() 0 8 1
A truncateOnWord() 0 10 2
A ordinalize() 0 3 1
A typogrifyFeed() 0 7 2
A singularize() 0 3 2
A humanDuration() 0 3 1
A truncate() 0 10 2
A humanRelativeTime() 0 3 1
A typogrify() 0 7 2
A wordLimit() 0 6 2
A smartypants() 0 7 2
A transliterate() 0 3 1
A stringy() 0 3 1
A pluralize() 0 3 2
1
<?php
2
/**
3
 * Typogrify plugin for Craft CMS
4
 *
5
 * Typogrify prettifies your web typography by preventing ugly quotes and
6
 * 'widows' and more
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\typogrify\services;
13
14
use Craft;
15
use craft\base\Component;
16
use DateInterval;
17
use DateTime;
18
use Michelf\SmartyPants;
19
use nystudio107\typogrify\Typogrify;
20
use PHP_Typography\PHP_Typography;
21
use PHP_Typography\Settings;
22
use Stringy\Stringy;
23
use yii\helpers\Inflector;
24
25
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
27
 * @package   Typogrify
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
28
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
29
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
30
class TypogrifyService extends Component
31
{
32
    // Public Methods
33
    // =========================================================================
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var null|PHP_Typography
37
     */
38
    public ?PHP_Typography $phpTypography = null;
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var null|Settings
42
     */
43
    public ?Settings $phpTypographySettings = null;
44
45
    // Public Methods
46
    // =========================================================================
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @inheritdoc
50
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
51
    public function init(): void
52
    {
53
        parent::init();
54
55
        // Create a new phpTypographySettings instance
56
        $this->phpTypographySettings = new Settings();
57
58
        // Create a new PhpTypography instance
59
        $this->phpTypography = new PHP_Typography();
60
61
        // Apply our default settings
62
        $settings = Typogrify::$plugin->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $settings = Typogrify::$plugin->getSettings();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        if ($settings) {
0 ignored issues
show
introduced by
$settings is of type craft\base\Model, thus it always evaluated to true.
Loading history...
64
            $settingsArray = $settings->toArray();
65
            foreach ($settingsArray as $key => $value) {
66
                if ($key !== 'default_escape') {
67
                    $this->phpTypographySettings->{$key}($value);
68
                }
69
            }
70
        }
71
    }
72
73
    /**
74
     * Typogrify applies a veritable kitchen sink of typographic treatments to
75
     * beautify your web typography
76
     *
77
     * @param string|int|float|null $text The text or HTML fragment to process
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
78
     * @param bool $isTitle Optional. If the HTML fragment is a title.
0 ignored issues
show
Coding Style introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
79
     *                        Default false
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 22 spaces but found 24
Loading history...
80
     *
81
     * @return string The processed HTML
82
     */
83
    public function typogrify(string|int|float|null $text, bool $isTitle = false): string
84
    {
85
        if (empty($text)) {
86
            return '';
87
        }
88
89
        return $this->phpTypography->process((string)$text, $this->phpTypographySettings, $isTitle);
0 ignored issues
show
Bug introduced by
The method process() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        return $this->phpTypography->/** @scrutinizer ignore-call */ process((string)$text, $this->phpTypographySettings, $isTitle);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $this->phpTypographySettings can also be of type null; however, parameter $settings of PHP_Typography\PHP_Typography::process() does only seem to accept PHP_Typography\Settings, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        return $this->phpTypography->process((string)$text, /** @scrutinizer ignore-type */ $this->phpTypographySettings, $isTitle);
Loading history...
90
    }
91
92
    /**
93
     * Typogrify applies a veritable kitchen sink of typographic treatments to
94
     * beautify your web typography but in a way that is appropriate for RSS
95
     * (or similar) feeds -- i.e. excluding processes that may cause issues in
96
     * contexts with limited character set intelligence.
97
     *
98
     * @param string|int|float|null $text The text or HTML fragment to process
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
99
     * @param bool $isTitle Optional. If the HTML fragment is a title.
0 ignored issues
show
Coding Style introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
100
     *                        Default false
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 22 spaces but found 24
Loading history...
101
     *
102
     * @return string The processed HTML
103
     */
104
    public function typogrifyFeed(string|int|float|null $text, bool $isTitle = false): string
105
    {
106
        if (empty($text)) {
107
            return '';
108
        }
109
110
        return $this->phpTypography->process_feed((string)$text, $this->phpTypographySettings, $isTitle);
0 ignored issues
show
Bug introduced by
It seems like $this->phpTypographySettings can also be of type null; however, parameter $settings of PHP_Typography\PHP_Typography::process_feed() does only seem to accept PHP_Typography\Settings, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        return $this->phpTypography->process_feed((string)$text, /** @scrutinizer ignore-type */ $this->phpTypographySettings, $isTitle);
Loading history...
111
    }
112
113
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
114
     * @param string|int|float|null $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
115
     *
116
     * @return string
117
     */
118
    public function smartypants(string|int|float|null $text): string
119
    {
120
        if (empty($text)) {
121
            return '';
122
        }
123
124
        return SmartyPants::defaultTransform((string)$text);
125
    }
126
127
    /**
128
     * Truncates the string to a given length. If $substring is provided, and
129
     * truncating occurs, the string is further truncated so that the substring
130
     * may be appended without exceeding the desired length.
131
     *
132
     * @param string|int|float|null $string The string to truncate
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
133
     * @param int $length Desired length of the truncated string
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
134
     * @param string $substring The substring to append if it can fit
0 ignored issues
show
Coding Style introduced by
Expected 16 spaces after parameter type; 1 found
Loading history...
135
     *
136
     * @return string with the resulting $str after truncating
137
     */
138
    public function truncate(string|int|float|null $string, int $length, string $substring = '…'): string
139
    {
140
        $result = (string)$string;
141
142
        if (!empty($string)) {
143
            $string = strip_tags($string);
144
            $result = (string)Stringy::create($string)->truncate($length, $substring);
145
        }
146
147
        return $result;
148
    }
149
150
    /**
151
     * Truncates the string to a given length, while ensuring that it does not
152
     * split words. If $substring is provided, and truncating occurs, the
153
     * string is further truncated so that the substring may be appended without
154
     * exceeding the desired length.
155
     *
156
     * @param string|int|float|null $string The string to truncate
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
157
     * @param int $length Desired length of the truncated string
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
158
     * @param string $substring The substring to append if it can fit
0 ignored issues
show
Coding Style introduced by
Expected 16 spaces after parameter type; 1 found
Loading history...
159
     *
160
     * @return string with the resulting $str after truncating
161
     */
162
    public function truncateOnWord(string|int|float|null $string, int $length, string $substring = '…'): string
163
    {
164
        $result = (string)$string;
165
166
        if (!empty($string)) {
167
            $string = strip_tags($string);
168
            $result = (string)Stringy::create($string)->safeTruncate($length, $substring);
169
        }
170
171
        return $result;
172
    }
173
174
    /**
175
     * Creates a Stringy object and assigns both string and encoding properties
176
     * the supplied values. $string is cast to a string prior to assignment,
177
     * and if
178
     * $encoding is not specified, it defaults to mb_internal_encoding(). It
179
     * then returns the initialized object. Throws an InvalidArgumentException
180
     * if the first argument is an array or object without a __toString method.
181
     *
182
     * @param string|int|float|null $string The string initialize the Stringy object with
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
183
     * @param null|string $encoding The character encoding
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
184
     *
185
     * @return Stringy
186
     */
187
    public function stringy(string|int|float|null $string = '', ?string $encoding = null): Stringy
188
    {
189
        return Stringy::create($string, $encoding);
190
    }
191
192
    /**
193
     * Formats the value in bytes as a size in human readable form for example
194
     * `12 KB`.
195
     *
196
     * This is the short form of [[asSize]].
197
     *
198
     * If [[sizeFormatBase]] is 1024, [binary
199
     * prefixes](http://en.wikipedia.org/wiki/Binary_prefix)
200
     * (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting
201
     * result.
202
     *
203
     * @param string|int|float $bytes value in bytes to be formatted.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
204
     * @param int $decimals the number of digits after the decimal
0 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
205
     *                                   point.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 22 spaces but found 35
Loading history...
206
     *
207
     * @return string the formatted result.
208
     */
209
    public function humanFileSize(string|int|float $bytes, int $decimals = 1): string
210
    {
211
        $oldSize = Craft::$app->formatter->sizeFormatBase;
212
        Craft::$app->formatter->sizeFormatBase = 1000;
213
        $result = Craft::$app->formatter->asShortSize($bytes, $decimals);
214
        Craft::$app->formatter->sizeFormatBase = $oldSize;
215
216
        return $result;
217
    }
218
219
    /**
220
     * Represents the value as duration in human readable format.
221
     *
222
     * @param DateInterval|string|int $value the value to be formatted.
223
     *                                        Acceptable formats:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
224
     *                                        - [DateInterval
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
225
     *                                        object](http://php.net/manual/ru/class.dateinterval.php)
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
226
     *                                        - integer - number of seconds.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
227
     *                                        For example: value `131`
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
228
     *                                        represents `2 minutes, 11
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
229
     *                                        seconds`
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
230
     *                                        - ISO8601 duration format. For
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
231
     *                                        example, all of these values
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
232
     *                                        represent `1 day, 2 hours, 30
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
233
     *                                        minutes` duration:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
234
     *                                        `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z`
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
235
     *                                        - between two datetime values
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
236
     *                                        `2015-01-01T13:00:00Z/P1D2H30M` -
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
237
     *                                        time interval after datetime
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
238
     *                                        value
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
239
     *                                        `P1D2H30M/2015-01-02T13:30:00Z` -
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
240
     *                                        time interval before datetime
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
241
     *                                        value
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
242
     *                                        `P1D2H30M` - simply a date
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
243
     *                                        interval
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
244
     *                                        `P-1D2H30M` - a negative date
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
245
     *                                        interval (`-1 day, 2 hours, 30
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
246
     *                                        minutes`)
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 39 spaces but found 40
Loading history...
247
     *
248
     * @return string the formatted duration.
249
     */
250
    public function humanDuration(DateInterval|string|int $value): string
251
    {
252
        return Craft::$app->formatter->asDuration($value);
253
    }
254
255
    /**
256
     * Formats the value as the time interval between a date and now in human
257
     * readable form.
258
     *
259
     * This method can be used in three different ways:
260
     *
261
     * 1. Using a timestamp that is relative to `now`.
262
     * 2. Using a timestamp that is relative to the `$referenceTime`.
263
     * 3. Using a `DateInterval` object.
264
     *
265
     * @param int|string|DateTime|DateInterval $value the value to be
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter name; 1 found
Loading history...
266
     *                                                          formatted. The
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
267
     *                                                          following types
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
268
     *                                                          of value are
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
269
     *                                                          supported:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
270
     *
271
     * - an integer representing a UNIX timestamp
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
272
     * - a string that can be [parsed to create a DateTime
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
273
     * object](http://php.net/manual/en/datetime.formats.php). The timestamp is
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
274
     * assumed to be in [[defaultTimeZone]] unless a time zone is explicitly
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
275
     * given.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
276
     * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
277
     * - a PHP DateInterval object (a positive time interval will refer to the
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
278
     * past, a negative one to the future)
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 1
Loading history...
279
     *
280
     * @param null|int|string|DateTime $referenceTime if specified
0 ignored issues
show
Coding Style introduced by
Parameter tags must be grouped together in a doc comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
281
     *                                                          the value is
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
282
     *                                                          used as a
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
283
     *                                                          reference time
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
284
     *                                                          instead of
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
285
     *                                                          `now` when
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
286
     *                                                          `$value` is not
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
287
     *                                                          a
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
288
     *                                                          `DateInterval`
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
289
     *                                                          object.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 48 spaces but found 58
Loading history...
290
     *
291
     * @return string the formatted result.
292
     */
293
    public function humanRelativeTime(int|string|DateTime|DateInterval $value, null|int|string|DateTime $referenceTime = null): string
294
    {
295
        return Craft::$app->formatter->asRelativeTime($value, $referenceTime);
296
    }
297
298
    /**
299
     * Converts number to its ordinal English form
300
     * For example, converts 13 to 13th, 2 to 2nd
301
     *
302
     * @param int $number
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
303
     *
304
     * @return string
305
     */
306
    public function ordinalize(int $number): string
307
    {
308
        return Inflector::ordinalize($number);
309
    }
310
311
    /**
312
     * Converts a word to its plural form
313
     * For example, 'apple' will become 'apples', and 'child' will become
314
     * 'children'
315
     *
316
     * @param string $word
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
317
     * @param int $number
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
318
     *
319
     * @return string
320
     */
321
    public function pluralize(string $word, int $number = 2): string
322
    {
323
        return abs($number) === 1 ? $word : Inflector::pluralize($word);
324
    }
325
326
    /**
327
     * Converts a word to its singular form
328
     * For example, 'apples' will become 'apple', and 'children' will become
329
     * 'child'
330
     *
331
     * @param string $word
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
332
     * @param int $number
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
333
     *
334
     * @return string
335
     */
336
    public function singularize(string $word, int $number = 1): string
337
    {
338
        return abs($number) === 1 ? Inflector::singularize($word) : $word;
339
    }
340
341
    /**
342
     * Returns transliterated version of a string
343
     * For example, 获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?
344
     * will be transliterated to huo qu dao dochira Ukrainsʹka: g,e, Srpska: d,
345
     * n, d! ¿Espanol?
346
     *
347
     * @param string $string
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
348
     * @param null $transliterator
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $transliterator is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
349
     *
350
     * @return string
351
     */
352
    public function transliterate(string $string, $transliterator = null): string
353
    {
354
        return Inflector::transliterate($string, $transliterator);
355
    }
356
357
    /**
358
     * Limits a string by word count. If $substring is provided, and truncating
359
     * occurs, the string is further truncated so that the substring may be
360
     * appended without exceeding the desired length.
361
     *
362
     * @param string $string
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
363
     * @param int $length
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
364
     * @param string $substring
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
365
     *
366
     * @return string
367
     */
368
    public function wordLimit(string $string, int $length, string $substring = '…'): string
369
    {
370
        $words = preg_split("/[\s]+/u", strip_tags($string));
371
        $result = implode(' ', array_slice($words, 0, $length));
372
373
        return count($words) > $length ? $result . $substring : $result;
374
    }
375
}
376