Passed
Push — v1 ( ac69b6...2a5986 )
by Andrew
08:08 queued 05:31
created

TypogrifyVariable::wordLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Typogrify plugin for Craft CMS 3.x
4
 *
5
 * Typogrify prettifies your web typography by preventing ugly quotes and 'widows' and more
6
 *
7
 * @link      https://nystudio107.com/
8
 * @copyright Copyright (c) 2017 nystudio107
9
 */
10
11
namespace nystudio107\typogrify\variables;
12
13
use nystudio107\typogrify\Typogrify;
14
15
use craft\helpers\Template;
16
17
use Stringy\Stringy;
18
19
/**
20
 * @author    nystudio107
21
 * @package   Typogrify
22
 * @since     1.0.0
23
 */
24
class TypogrifyVariable
25
{
26
    // Public Methods
27
    // =========================================================================
28
29
    /**
30
     * Typogrify applies a veritable kitchen sink of typographic treatments to
31
     * beautify your web typography
32
     *
33
     * @param string $text    The text or HTML fragment to process
34
     * @param bool   $isTitle Optional. If the HTML fragment is a title.
35
     *                        Default false
36
     *
37
     * @return string The processed HTML
38
     */
39
    public function typogrify($text, $isTitle = false)
40
    {
41
        return Template::raw(Typogrify::$plugin->typogrify->typogrify($text, $isTitle));
42
    }
43
44
    /**
45
     * Typogrify applies a veritable kitchen sink of typographic treatments to
46
     * beautify your web typography but in a way that is appropriate for RSS
47
     * (or similar) feeds -- i.e. excluding processes that may cause issues in
48
     * contexts with limited character set intelligence.
49
     *
50
     * @param string $text    The text or HTML fragment to process
51
     * @param bool   $isTitle Optional. If the HTML fragment is a title.
52
     *                        Default false
53
     *
54
     * @return string The processed HTML
55
     */
56
    public function typogrifyFeed($text, $isTitle = false)
57
    {
58
        return Template::raw(Typogrify::$plugin->typogrify->typogrifyFeed($text, $isTitle));
59
    }
60
61
    /**
62
     * @param string $text
63
     *
64
     * @return \Twig_Markup
65
     */
66
    public function smartypants($text)
67
    {
68
        return Template::raw(Typogrify::$plugin->typogrify->smartypants($text));
69
    }
70
71
    /**
72
     * @return \PHP_Typography\Settings
73
     */
74
    public function getPhpTypographySettings()
75
    {
76
        return Typogrify::$plugin->typogrify->phpTypographySettings;
77
    }
78
79
    /**
80
     * Truncates the string to a given length. If $substring is provided, and
81
     * truncating occurs, the string is further truncated so that the substring
82
     * may be appended without exceeding the desired length.
83
     *
84
     * @param  string $string    The string to truncate
85
     * @param  int    $length    Desired length of the truncated string
86
     * @param  string $substring The substring to append if it can fit
87
     *
88
     * @return string with the resulting $str after truncating
89
     */
90
    public function truncate($string, $length, $substring = '…'): string
91
    {
92
        return Template::raw(Typogrify::$plugin->typogrify->truncate($string, $length, $substring));
93
    }
94
95
    /**
96
     * Truncates the string to a given length, while ensuring that it does not
97
     * split words. If $substring is provided, and truncating occurs, the
98
     * string is further truncated so that the substring may be appended without
99
     * exceeding the desired length.
100
     *
101
     * @param  string $string    The string to truncate
102
     * @param  int    $length    Desired length of the truncated string
103
     * @param  string $substring The substring to append if it can fit
104
     *
105
     * @return string with the resulting $str after truncating
106
     */
107
    public function truncateOnWord($string, $length, $substring = '…'): string
108
    {
109
        return Template::raw(Typogrify::$plugin->typogrify->truncateOnWord($string, $length, $substring));
110
    }
111
112
    /**
113
     * Creates a Stringy object and assigns both string and encoding properties
114
     * the supplied values. $string is cast to a string prior to assignment, and if
115
     * $encoding is not specified, it defaults to mb_internal_encoding(). It
116
     * then returns the initialized object. Throws an InvalidArgumentException
117
     * if the first argument is an array or object without a __toString method.
118
     *
119
     * @param  string $string   The string initialize the Stringy object with
120
     * @param  string $encoding The character encoding
121
     *
122
     * @return Stringy
123
     */
124
    public function stringy($string = '', $encoding = null)
125
    {
126
        return Typogrify::$plugin->typogrify->stringy($string, $encoding);
127
    }
128
129
    /**
130
     * Formats the value in bytes as a size in human readable form for example `12 KB`.
131
     *
132
     * This is the short form of [[asSize]].
133
     *
134
     * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix)
135
     * (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result.
136
     *
137
     * @param string|int|float $bytes    value in bytes to be formatted.
138
     * @param int              $decimals the number of digits after the decimal point.
139
     *
140
     * @return string the formatted result.
141
     */
142
    public function humanFileSize($bytes, $decimals = 1): string
143
    {
144
        return Template::raw(Typogrify::$plugin->typogrify->humanFileSize($bytes, $decimals));
145
    }
146
147
    /**
148
     * Represents the value as duration in human readable format.
149
     *
150
     * @param \DateInterval|string|int $value the value to be formatted. Acceptable formats:
151
     *  - [DateInterval object](http://php.net/manual/ru/class.dateinterval.php)
152
     *  - integer - number of seconds. For example: value `131` represents `2 minutes, 11 seconds`
153
     *  - ISO8601 duration format. For example, all of these values represent `1 day, 2 hours, 30 minutes` duration:
154
     *    `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z` - between two datetime values
155
     *    `2015-01-01T13:00:00Z/P1D2H30M` - time interval after datetime value
156
     *    `P1D2H30M/2015-01-02T13:30:00Z` - time interval before datetime value
157
     *    `P1D2H30M` - simply a date interval
158
     *    `P-1D2H30M` - a negative date interval (`-1 day, 2 hours, 30 minutes`)
159
     *
160
     * @return string the formatted duration.
161
     */
162
    public function humanDuration($value)
163
    {
164
        return Template::raw(Typogrify::$plugin->typogrify->humanDuration($value));
165
    }
166
167
    /**
168
     * Formats the value as the time interval between a date and now in human readable form.
169
     *
170
     * This method can be used in three different ways:
171
     *
172
     * 1. Using a timestamp that is relative to `now`.
173
     * 2. Using a timestamp that is relative to the `$referenceTime`.
174
     * 3. Using a `DateInterval` object.
175
     *
176
     * @param int|string|\DateTime|\DateInterval $value the value to be formatted. The following
177
     * types of value are supported:
178
     *
179
     * - an integer representing a UNIX timestamp
180
     * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php).
181
     *   The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
182
     * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
183
     * - a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future)
184
     *
185
     * @param int|string|\DateTime $referenceTime if specified the value is used as a reference time instead of `now`
186
     * when `$value` is not a `DateInterval` object.
187
     *
188
     * @return string the formatted result.
189
     */
190
    public function humanRelativeTime($value, $referenceTime = null)
191
    {
192
        return Template::raw(Typogrify::$plugin->typogrify->humanRelativeTime($value, $referenceTime));
193
    }
194
195
    /**
196
     * Converts number to its ordinal English form
197
     * For example, converts 13 to 13th, 2 to 2nd
198
     *
199
     * @param int $number
200
     *
201
     * @return \Twig_Markup
202
     */
203
    public function ordinalize(int $number)
204
    {
205
        return Template::raw(Typogrify::$plugin->typogrify->ordinalize($number));
206
    }
207
208
    /**
209
     * Converts a word to its plural form
210
     * For example, 'apple' will become 'apples', and 'child' will become 'children'
211
     *
212
     * @param string $word
213
     *
214
     * @return \Twig_Markup
215
     */
216
    public function pluralize(string $word)
217
    {
218
        return Template::raw(Typogrify::$plugin->typogrify->pluralize($word));
219
    }
220
221
    /**
222
     * Converts a word to its singular form
223
     * For example, 'apples' will become 'apple', and 'children' will become 'child'
224
     *
225
     * @param string $word
226
     *
227
     * @return \Twig_Markup
228
     */
229
    public function singularize(string $word)
230
    {
231
        return Template::raw(Typogrify::$plugin->typogrify->singularize($word));
232
    }
233
234
    /**
235
     * Returns transliterated version of a string
236
     * For example, 获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?
237
     * will be transliterated to huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?
238
     *
239
     * @param string $string
240
     * @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...
241
     *
242
     * @return \Twig_Markup
243
     */
244
    public function transliterate(string $string, $transliterator = null)
245
    {
246
        return Template::raw(Typogrify::$plugin->typogrify->transliterate($string, $transliterator));
247
    }
248
249
    /**
250
     * Limits a string by word count. If $substring is provided, and truncating occurs, the
251
     * string is further truncated so that the substring may be appended without
252
     * exceeding the desired length.
253
     *
254
     * @param string $string
255
     * @param int    $length
256
     * @param string $substring
257
     *
258
     * @return string
259
     */
260
    public function wordLimit(string $string, int $length, string $substring = '…')
261
    {
262
        return Template::raw(Typogrify::$plugin->typogrify->wordLimit($string, $length, $substring));
263
    }
264
}
265