|
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/ |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
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
|
|
|
/** |
|
|
|
|
|
|
26
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
27
|
|
|
* @package Typogrify |
|
|
|
|
|
|
28
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
|
|
30
|
|
|
class TypogrifyService extends Component |
|
31
|
|
|
{ |
|
32
|
|
|
// Public Methods |
|
33
|
|
|
// ========================================================================= |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @var null|PHP_Typography |
|
37
|
|
|
*/ |
|
38
|
|
|
public ?PHP_Typography $phpTypography = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
|
|
|
|
|
41
|
|
|
* @var null|Settings |
|
42
|
|
|
*/ |
|
43
|
|
|
public ?Settings $phpTypographySettings = null; |
|
44
|
|
|
|
|
45
|
|
|
// Public Methods |
|
46
|
|
|
// ========================================================================= |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
|
|
|
|
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
63
|
|
|
if ($settings) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
78
|
|
|
* @param bool $isTitle Optional. If the HTML fragment is a title. |
|
|
|
|
|
|
79
|
|
|
* Default false |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
99
|
|
|
* @param bool $isTitle Optional. If the HTML fragment is a title. |
|
|
|
|
|
|
100
|
|
|
* Default false |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
|
|
|
|
|
114
|
|
|
* @param string|int|float|null $text |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
133
|
|
|
* @param int $length Desired length of the truncated string |
|
|
|
|
|
|
134
|
|
|
* @param string $substring The substring to append if it can fit |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
157
|
|
|
* @param int $length Desired length of the truncated string |
|
|
|
|
|
|
158
|
|
|
* @param string $substring The substring to append if it can fit |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
183
|
|
|
* @param null|string $encoding The character encoding |
|
|
|
|
|
|
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. |
|
|
|
|
|
|
204
|
|
|
* @param int $decimals the number of digits after the decimal |
|
|
|
|
|
|
205
|
|
|
* point. |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
224
|
|
|
* - [DateInterval |
|
|
|
|
|
|
225
|
|
|
* object](http://php.net/manual/ru/class.dateinterval.php) |
|
|
|
|
|
|
226
|
|
|
* - integer - number of seconds. |
|
|
|
|
|
|
227
|
|
|
* For example: value `131` |
|
|
|
|
|
|
228
|
|
|
* represents `2 minutes, 11 |
|
|
|
|
|
|
229
|
|
|
* seconds` |
|
|
|
|
|
|
230
|
|
|
* - ISO8601 duration format. For |
|
|
|
|
|
|
231
|
|
|
* example, all of these values |
|
|
|
|
|
|
232
|
|
|
* represent `1 day, 2 hours, 30 |
|
|
|
|
|
|
233
|
|
|
* minutes` duration: |
|
|
|
|
|
|
234
|
|
|
* `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z` |
|
|
|
|
|
|
235
|
|
|
* - between two datetime values |
|
|
|
|
|
|
236
|
|
|
* `2015-01-01T13:00:00Z/P1D2H30M` - |
|
|
|
|
|
|
237
|
|
|
* time interval after datetime |
|
|
|
|
|
|
238
|
|
|
* value |
|
|
|
|
|
|
239
|
|
|
* `P1D2H30M/2015-01-02T13:30:00Z` - |
|
|
|
|
|
|
240
|
|
|
* time interval before datetime |
|
|
|
|
|
|
241
|
|
|
* value |
|
|
|
|
|
|
242
|
|
|
* `P1D2H30M` - simply a date |
|
|
|
|
|
|
243
|
|
|
* interval |
|
|
|
|
|
|
244
|
|
|
* `P-1D2H30M` - a negative date |
|
|
|
|
|
|
245
|
|
|
* interval (`-1 day, 2 hours, 30 |
|
|
|
|
|
|
246
|
|
|
* minutes`) |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
266
|
|
|
* formatted. The |
|
|
|
|
|
|
267
|
|
|
* following types |
|
|
|
|
|
|
268
|
|
|
* of value are |
|
|
|
|
|
|
269
|
|
|
* supported: |
|
|
|
|
|
|
270
|
|
|
* |
|
271
|
|
|
* - an integer representing a UNIX timestamp |
|
|
|
|
|
|
272
|
|
|
* - a string that can be [parsed to create a DateTime |
|
|
|
|
|
|
273
|
|
|
* object](http://php.net/manual/en/datetime.formats.php). The timestamp is |
|
|
|
|
|
|
274
|
|
|
* assumed to be in [[defaultTimeZone]] unless a time zone is explicitly |
|
|
|
|
|
|
275
|
|
|
* given. |
|
|
|
|
|
|
276
|
|
|
* - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
|
|
|
|
|
|
277
|
|
|
* - a PHP DateInterval object (a positive time interval will refer to the |
|
|
|
|
|
|
278
|
|
|
* past, a negative one to the future) |
|
|
|
|
|
|
279
|
|
|
* |
|
280
|
|
|
* @param null|int|string|DateTime $referenceTime if specified |
|
|
|
|
|
|
281
|
|
|
* the value is |
|
|
|
|
|
|
282
|
|
|
* used as a |
|
|
|
|
|
|
283
|
|
|
* reference time |
|
|
|
|
|
|
284
|
|
|
* instead of |
|
|
|
|
|
|
285
|
|
|
* `now` when |
|
|
|
|
|
|
286
|
|
|
* `$value` is not |
|
|
|
|
|
|
287
|
|
|
* a |
|
|
|
|
|
|
288
|
|
|
* `DateInterval` |
|
|
|
|
|
|
289
|
|
|
* object. |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
317
|
|
|
* @param int $number |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
332
|
|
|
* @param int $number |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
348
|
|
|
* @param null $transliterator |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
363
|
|
|
* @param int $length |
|
|
|
|
|
|
364
|
|
|
* @param string $substring |
|
|
|
|
|
|
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
|
|
|
|