Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#118)
by Der Mundschenk
03:37
created

Settings::set_smart_math()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 *  This file is part of PHP-Typography.
4
 *
5
 *  Copyright 2014-2019 Peter Putzer.
6
 *  Copyright 2009-2011 KINGdesk, LLC.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  (at your option) any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License along
19
 *  with this program; if not, write to the Free Software Foundation, Inc.,
20
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 *
22
 *  ***
23
 *
24
 *  @package mundschenk-at/php-typography
25
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
26
 */
27
28
namespace PHP_Typography;
29
30
use PHP_Typography\Settings\Dash_Style;
31
use PHP_Typography\Settings\Quote_Style;
32
use PHP_Typography\Settings\Quotes;
33
34
/**
35
 * Store settings for the PHP_Typography class.
36
 *
37
 * @author Peter Putzer <[email protected]>
38
 *
39
 * @since 4.0.0
40
 * @since 6.5.0 The protected property $no_break_narrow_space has been deprecated.
41
 */
42
class Settings implements \ArrayAccess, \JsonSerializable {
43
44
	/**
45
	 * The current no-break narrow space character.
46
	 *
47
	 * @deprecated 6.5.0
48
	 *
49
	 * @var string
50
	 */
51
	protected $no_break_narrow_space;
52
53
	/**
54
	 * Primary quote style.
55
	 *
56
	 * @var Quotes
57
	 */
58
	protected $primary_quote_style;
59
60
	/**
61
	 * Secondary quote style.
62
	 *
63
	 * @var Quotes
64
	 */
65
	protected $secondary_quote_style;
66
67
	/**
68
	 * A regex pattern for custom units (or the empty string).
69
	 *
70
	 * @var string
71
	 */
72
	protected $custom_units = '';
73
74
	/**
75
	 * A hashmap of settings for the various typographic options.
76
	 *
77
	 * @var array
78
	 */
79
	protected $data = [];
80
81
	/**
82
	 * The current dash style.
83
	 *
84
	 * @var Settings\Dashes
85
	 */
86
	protected $dash_style;
87
88
	/**
89
	 * The Unicode character mapping (some characters still have compatibility issues).
90
	 *
91
	 * @since 6.5.0
92
	 *
93
	 * @var string[]
94
	 */
95
	protected $unicode_mapping;
96
97
	/**
98
	 * An array containing just remapped characters (for optimization).
99
	 *
100
	 * @since 6.5.0
101
	 *
102
	 * @var string[]
103
	 */
104
	protected $remapped_characters;
105
106
	/**
107
	 * Sets up a new Settings object.
108
	 *
109
	 * @since 6.0.0 If $set_defaults is `false`, the settings object is not fully
110
	 *              initialized unless `set_smart_quotes_primary`,
111
	 *              `set_smart_quotes_secondary`, `set_smart_dashes_style` and
112
	 *              `set_true_no_break_narrow_space` are called explicitly.
113
	 * @since 6.5.0 A (partial) character mapping can be given to remap certain
114
	 *              characters.
115
	 *
116
	 * @param bool     $set_defaults Optional. If true, set default values for various properties. Default true.
117
	 * @param string[] $mapping      Optional. Unicode characters to remap. The default maps the narrow no-break space to the normal NO-BREAK SPACE and the apostrophe to the RIGHT SINGLE QUOTATION MARK.
118
	 */
119 1
	public function __construct( $set_defaults = true, array $mapping = [ U::NO_BREAK_NARROW_SPACE => U::NO_BREAK_SPACE, U::APOSTROPHE => U::SINGLE_QUOTE_CLOSE ] ) { // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing
120 1
		if ( $set_defaults ) {
121 1
			$this->set_defaults();
122
		}
123
124
		// Merge default character mapping with given mapping.
125 1
		$this->unicode_mapping = $mapping;
126
127
		// Keep backwards compatibility.
128 1
		$this->no_break_narrow_space = $this->unicode_mapping[ U::NO_BREAK_NARROW_SPACE ];
0 ignored issues
show
Deprecated Code introduced by
The property PHP_Typography\Settings::$no_break_narrow_space has been deprecated: 6.5.0 ( Ignorable by Annotation )

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

128
		/** @scrutinizer ignore-deprecated */ $this->no_break_narrow_space = $this->unicode_mapping[ U::NO_BREAK_NARROW_SPACE ];

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
129 1
	}
130
131
	/**
132
	 * Provides access to named settings (object syntax).
133
	 *
134
	 * @param string $key The settings key.
135
	 *
136
	 * @return mixed
137
	 */
138 1
	public function &__get( $key ) {
139 1
		return $this->data[ $key ];
140
	}
141
142
	/**
143
	 * Changes a named setting (object syntax).
144
	 *
145
	 * @param string $key   The settings key.
146
	 * @param mixed  $value The settings value.
147
	 */
148 1
	public function __set( $key, $value ) {
149 1
		$this->data[ $key ] = $value;
150 1
	}
151
152
	/**
153
	 * Checks if a named setting exists (object syntax).
154
	 *
155
	 * @param string $key The settings key.
156
	 */
157 1
	public function __isset( $key ) {
158 1
		return isset( $this->data[ $key ] );
159
	}
160
161
	/**
162
	 * Unsets a named setting.
163
	 *
164
	 * @param string $key The settings key.
165
	 */
166 1
	public function __unset( $key ) {
167 1
		unset( $this->data[ $key ] );
168 1
	}
169
170
	/**
171
	 * Changes a named setting (array syntax).
172
	 *
173
	 * @param string $offset The settings key.
174
	 * @param mixed  $value  The settings value.
175
	 */
176 1
	public function offsetSet( $offset, $value ) {
177 1
		if ( ! empty( $offset ) ) {
178 1
			$this->data[ $offset ] = $value;
179
		}
180 1
	}
181
182
	/**
183
	 * Checks if a named setting exists (array syntax).
184
	 *
185
	 * @param string $offset The settings key.
186
	 */
187 1
	public function offsetExists( $offset ) {
188 1
		return isset( $this->data[ $offset ] );
189
	}
190
191
	/**
192
	 * Unsets a named setting (array syntax).
193
	 *
194
	 * @param string $offset The settings key.
195
	 */
196 1
	public function offsetUnset( $offset ) {
197 1
		unset( $this->data[ $offset ] );
198 1
	}
199
200
	/**
201
	 * Provides access to named settings (array syntax).
202
	 *
203
	 * @param string $offset The settings key.
204
	 *
205
	 * @return mixed
206
	 */
207 1
	public function offsetGet( $offset ) {
208 1
		return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
209
	}
210
211
	/**
212
	 * Provides a JSON serialization of the settings.
213
	 *
214
	 * @return mixed
215
	 */
216 1
	public function jsonSerialize() {
217 1
		return \array_merge(
218 1
			$this->data,
219
			[
220 1
				'unicode_mapping'       => $this->unicode_mapping,
221 1
				'primary_quotes'        => "{$this->primary_quote_style->open()}|{$this->primary_quote_style->close()}",
222 1
				'secondary_quotes'      => "{$this->secondary_quote_style->open()}|{$this->secondary_quote_style->close()}",
223 1
				'dash_style'            => "{$this->dash_style->interval_dash()}|{$this->dash_style->interval_space()}|{$this->dash_style->parenthetical_dash()}|{$this->dash_style->parenthetical_space()}",
224 1
				'custom_units'          => $this->custom_units,
225
			]
226
		);
227
	}
228
229
	/**
230
	 * Remaps a unicode character to another one.
231
	 *
232
	 * @since 6.5.0
233
	 *
234
	 * @param  string $char     The remapped character.
235
	 * @param  string $new_char The character to actually use.
236
	 */
237
	public function remap_character( $char, $new_char ) {
238
		if ( $char !== $new_char ) {
239
			$this->unicode_mapping[ $char ] = $new_char;
240
		} else {
241
			unset( $this->unicode_mapping[ $char ] );
242
		}
243
244
		// Compatibility with the old way of setting the no-break narrow space.
245
		if ( U::NO_BREAK_NARROW_SPACE === $char ) {
246
			$this->no_break_narrow_space = $new_char;
0 ignored issues
show
Deprecated Code introduced by
The property PHP_Typography\Settings::$no_break_narrow_space has been deprecated: 6.5.0 ( Ignorable by Annotation )

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

246
			/** @scrutinizer ignore-deprecated */ $this->no_break_narrow_space = $new_char;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
247
		}
248
	}
249
250
	/**
251
	 * Remaps one or more strings.
252
	 *
253
	 * @since 6.5.0
254
	 *
255
	 * @param  string|string[] $input The input string(s).
256
	 *
257
	 * @return string|string[]
258
	 */
259
	public function apply_character_mapping( $input ) {
260
261
		// Nothing for us to do.
262
		if ( empty( $input ) || empty( $this->unicode_mapping ) ) {
263
			return $input;
264
		}
265
266
		$native_array = \is_array( $input );
267
		$data         = (array) $input;
268
269
		foreach ( $data as $key => $string ) {
270
			$data[ $key ] = \strtr( $string, $this->unicode_mapping );
271
		}
272
273
		return $native_array ? $data : $data[0];
274
	}
275
276
	/**
277
	 * Retrieves the current non-breaking narrow space character (either the
278
	 * regular non-breaking space &nbsp; or the the true non-breaking narrow space &#8239;).
279
	 *
280
	 * @deprecated 6.5.0 Use U::NO_BREAK_NARROW_SPACE instead and let Settings::apply_character_mapping() do the rest.
281
	 *
282
	 * @return string
283
	 */
284 1
	public function no_break_narrow_space() {
285 1
		return $this->no_break_narrow_space;
0 ignored issues
show
Deprecated Code introduced by
The property PHP_Typography\Settings::$no_break_narrow_space has been deprecated: 6.5.0 ( Ignorable by Annotation )

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

285
		return /** @scrutinizer ignore-deprecated */ $this->no_break_narrow_space;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
286
	}
287
288
	/**
289
	 * Retrieves the primary (double) quote style.
290
	 *
291
	 * @return Quotes
292
	 */
293 1
	public function primary_quote_style() {
294 1
		return $this->primary_quote_style;
295
	}
296
297
	/**
298
	 * Retrieves the secondary (single) quote style.
299
	 *
300
	 * @return Quotes
301
	 */
302 1
	public function secondary_quote_style() {
303 1
		return $this->secondary_quote_style;
304
	}
305
306
	/**
307
	 * Retrieves the dash style.
308
	 *
309
	 * @return Settings\Dashes
310
	 */
311 1
	public function dash_style() {
312 1
		return $this->dash_style;
313
	}
314
315
	/**
316
	 * Retrieves the custom units pattern.
317
	 *
318
	 * @return string The pattern is suitable for inclusion into a regular expression.
319
	 */
320 1
	public function custom_units() {
321 1
		return $this->custom_units;
322
	}
323
324
	/**
325
	 * (Re)set various options to their default values.
326
	 */
327 1
	public function set_defaults() {
328
		// General attributes.
329 1
		$this->set_tags_to_ignore();
330 1
		$this->set_classes_to_ignore();
331 1
		$this->set_ids_to_ignore();
332
333
		// Smart characters.
334 1
		$this->set_smart_quotes();
335 1
		$this->set_smart_quotes_primary();
336 1
		$this->set_smart_quotes_secondary();
337 1
		$this->set_smart_quotes_exceptions();
338 1
		$this->set_smart_dashes();
339 1
		$this->set_smart_dashes_style();
340 1
		$this->set_smart_ellipses();
341 1
		$this->set_smart_diacritics();
342 1
		$this->set_diacritic_language();
343 1
		$this->set_diacritic_custom_replacements();
344 1
		$this->set_smart_marks();
345 1
		$this->set_smart_ordinal_suffix();
346 1
		$this->set_smart_ordinal_suffix_match_roman_numerals();
347 1
		$this->set_smart_math();
348 1
		$this->set_smart_fractions();
349 1
		$this->set_smart_exponents();
350 1
		$this->set_smart_area_units();
351
352
		// Smart spacing.
353 1
		$this->set_single_character_word_spacing();
354 1
		$this->set_fraction_spacing();
355 1
		$this->set_unit_spacing();
356 1
		$this->set_french_punctuation_spacing();
357 1
		$this->set_units();
358 1
		$this->set_dash_spacing();
359 1
		$this->set_dewidow();
360 1
		$this->set_max_dewidow_length();
361 1
		$this->set_max_dewidow_pull();
362 1
		$this->set_dewidow_word_number();
363 1
		$this->set_wrap_hard_hyphens();
364 1
		$this->set_url_wrap();
365 1
		$this->set_email_wrap();
366 1
		$this->set_min_after_url_wrap();
367 1
		$this->set_space_collapse();
368 1
		$this->set_true_no_break_narrow_space();
0 ignored issues
show
Deprecated Code introduced by
The function PHP_Typography\Settings:...no_break_narrow_space() has been deprecated: 6.5.0 Use ::remap_character() instead. ( Ignorable by Annotation )

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

368
		/** @scrutinizer ignore-deprecated */ $this->set_true_no_break_narrow_space();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
369
370
		// Character styling.
371 1
		$this->set_style_ampersands();
372 1
		$this->set_style_caps();
373 1
		$this->set_style_initial_quotes();
374 1
		$this->set_style_numbers();
375 1
		$this->set_style_hanging_punctuation();
376 1
		$this->set_initial_quote_tags();
377
378
		// Hyphenation.
379 1
		$this->set_hyphenation();
380 1
		$this->set_hyphenation_language();
381 1
		$this->set_min_length_hyphenation();
382 1
		$this->set_min_before_hyphenation();
383 1
		$this->set_min_after_hyphenation();
384 1
		$this->set_hyphenate_headings();
385 1
		$this->set_hyphenate_all_caps();
386 1
		$this->set_hyphenate_title_case();
387 1
		$this->set_hyphenate_compounds();
388 1
		$this->set_hyphenation_exceptions();
389
390
		// Parser error handling.
391 1
		$this->set_ignore_parser_errors();
392 1
	}
393
394
	/**
395
	 * Enable lenient parser error handling (HTML is "best guess" if enabled).
396
	 *
397
	 * @param bool $on Optional. Default false.
398
	 */
399 1
	public function set_ignore_parser_errors( $on = false ) {
400 1
		$this->data['parserErrorsIgnore'] = $on;
401 1
	}
402
403
	/**
404
	 * Sets an optional handler for parser errors. Invalid callbacks will be silently ignored.
405
	 *
406
	 * @since 6.0.0. callable type is enforced via typehinting.
407
	 *
408
	 * @param callable|null $handler Optional. A callable that takes an array of error strings as its parameter. Default null.
409
	 */
410 2
	public function set_parser_errors_handler( callable $handler = null ) {
411 2
		$this->data['parserErrorsHandler'] = $handler;
412 2
	}
413
414
	/**
415
	 * Enable usage of true "no-break narrow space" (&#8239;) instead of the normal no-break space (&nbsp;).
416
	 *
417
	 * @deprecated 6.5.0 Use ::remap_character() instead.
418
	 *
419
	 * @param bool $on Optional. Default false.
420
	 */
421 1
	public function set_true_no_break_narrow_space( $on = false ) {
422
423 1
		if ( $on ) {
424 1
			$this->remap_character( U::NO_BREAK_NARROW_SPACE, U::NO_BREAK_NARROW_SPACE );
425
		} else {
426 1
			$this->remap_character( U::NO_BREAK_NARROW_SPACE, U::NO_BREAK_SPACE );
427
		}
428 1
	}
429
430
	/**
431
	 * Sets tags for which the typography of their children will be left untouched.
432
	 *
433
	 * @param string|array $tags A comma separated list or an array of tag names.
434
	 */
435 1
	public function set_tags_to_ignore( $tags = [ 'code', 'head', 'kbd', 'object', 'option', 'pre', 'samp', 'script', 'noscript', 'noembed', 'select', 'style', 'textarea', 'title', 'var', 'math' ] ) {
436
		// Ensure that we pass only lower-case tag names to XPath.
437 1
		$tags = array_filter( array_map( 'strtolower', Strings::maybe_split_parameters( $tags ) ), 'ctype_alnum' );
438
439 1
		$this->data['ignoreTags'] = array_unique( array_merge( $tags, array_flip( DOM::inappropriate_tags() ) ) );
440 1
	}
441
442
	/**
443
	 * Sets classes for which the typography of their children will be left untouched.
444
	 *
445
	 * @param string|array $classes A comma separated list or an array of class names.
446
	 */
447 1
	public function set_classes_to_ignore( $classes = [ 'vcard', 'noTypo' ] ) {
448 1
		$this->data['ignoreClasses'] = Strings::maybe_split_parameters( $classes );
449 1
	}
450
451
	/**
452
	 * Sets IDs for which the typography of their children will be left untouched.
453
	 *
454
	 * @param string|array $ids A comma separated list or an array of tag names.
455
	 */
456 1
	public function set_ids_to_ignore( $ids = [] ) {
457 1
		$this->data['ignoreIDs'] = Strings::maybe_split_parameters( $ids );
458 1
	}
459
460
	/**
461
	 * Enables/disables typographic quotes.
462
	 *
463
	 * @param bool $on Optional. Default true.
464
	 */
465 1
	public function set_smart_quotes( $on = true ) {
466 1
		$this->data['smartQuotes'] = $on;
467 1
	}
468
469
	/**
470
	 * Sets the style for primary ('double') quotemarks.
471
	 *
472
	 * Allowed values for $style:
473
	 * "doubleCurled" => "&ldquo;foo&rdquo;",
474
	 * "doubleCurledReversed" => "&rdquo;foo&rdquo;",
475
	 * "doubleLow9" => "&bdquo;foo&rdquo;",
476
	 * "doubleLow9Reversed" => "&bdquo;foo&ldquo;",
477
	 * "singleCurled" => "&lsquo;foo&rsquo;",
478
	 * "singleCurledReversed" => "&rsquo;foo&rsquo;",
479
	 * "singleLow9" => "&sbquo;foo&rsquo;",
480
	 * "singleLow9Reversed" => "&sbquo;foo&lsquo;",
481
	 * "doubleGuillemetsFrench" => "&laquo;&nbsp;foo&nbsp;&raquo;",
482
	 * "doubleGuillemets" => "&laquo;foo&raquo;",
483
	 * "doubleGuillemetsReversed" => "&raquo;foo&laquo;",
484
	 * "singleGuillemets" => "&lsaquo;foo&rsaquo;",
485
	 * "singleGuillemetsReversed" => "&rsaquo;foo&lsaquo;",
486
	 * "cornerBrackets" => "&#x300c;foo&#x300d;",
487
	 * "whiteCornerBracket" => "&#x300e;foo&#x300f;"
488
	 *
489
	 * @param  Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'doubleCurled'.
490
	 *
491
	 * @throws \DomainException Thrown if $style constant is invalid.
492
	 */
493 3
	public function set_smart_quotes_primary( $style = Quote_Style::DOUBLE_CURLED ) {
494 3
		$this->primary_quote_style = $this->get_quote_style( $style );
495 2
	}
496
497
	/**
498
	 * Sets the style for secondary ('single') quotemarks.
499
	 *
500
	 * Allowed values for $style:
501
	 * "doubleCurled" => "&ldquo;foo&rdquo;",
502
	 * "doubleCurledReversed" => "&rdquo;foo&rdquo;",
503
	 * "doubleLow9" => "&bdquo;foo&rdquo;",
504
	 * "doubleLow9Reversed" => "&bdquo;foo&ldquo;",
505
	 * "singleCurled" => "&lsquo;foo&rsquo;",
506
	 * "singleCurledReversed" => "&rsquo;foo&rsquo;",
507
	 * "singleLow9" => "&sbquo;foo&rsquo;",
508
	 * "singleLow9Reversed" => "&sbquo;foo&lsquo;",
509
	 * "doubleGuillemetsFrench" => "&laquo;&nbsp;foo&nbsp;&raquo;",
510
	 * "doubleGuillemets" => "&laquo;foo&raquo;",
511
	 * "doubleGuillemetsReversed" => "&raquo;foo&laquo;",
512
	 * "singleGuillemets" => "&lsaquo;foo&rsaquo;",
513
	 * "singleGuillemetsReversed" => "&rsaquo;foo&lsaquo;",
514
	 * "cornerBrackets" => "&#x300c;foo&#x300d;",
515
	 * "whiteCornerBracket" => "&#x300e;foo&#x300f;"
516
	 *
517
	 * @param  Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'singleCurled'.
518
	 *
519
	 * @throws \DomainException Thrown if $style constant is invalid.
520
	 */
521 3
	public function set_smart_quotes_secondary( $style = Quote_Style::SINGLE_CURLED ) {
522 3
		$this->secondary_quote_style = $this->get_quote_style( $style );
523 2
	}
524
525
	/**
526
	 * Retrieves a Quotes instance from a given style.
527
	 *
528
	 * @param  Quotes|string $style A Quotes instance or a quote style constant.
529
	 *
530
	 * @throws \DomainException Thrown if $style constant is invalid.
531
	 *
532
	 * @return Quotes
533
	 */
534 6
	protected function get_quote_style( $style ) {
535 6
		return $this->get_style( $style, Quotes::class, [ Quote_Style::class, 'get_styled_quotes' ], 'quote' );
536
	}
537
538
	/**
539
	 * Sets the list of exceptional words for smart quotes replacement. Mainly,
540
	 * this is used for contractions beginning with an apostrophe.
541
	 *
542
	 * @param string[] $exceptions Optional. An array of replacements indexed by the ”non-smart" form.
543
	 *                             Default a list of English words beginning with an apostrophy.
544
	 */
545 1
	public function set_smart_quotes_exceptions( $exceptions = [
546
		"'tain't"   => U::APOSTROPHE . 'tain' . U::APOSTROPHE . 't',
547
		"'twere"    => U::APOSTROPHE . 'twere',
548
		"'twas"     => U::APOSTROPHE . 'twas',
549
		"'tis"      => U::APOSTROPHE . 'tis',
550
		"'til"      => U::APOSTROPHE . 'til',
551
		"'bout"     => U::APOSTROPHE . 'bout',
552
		"'nuff"     => U::APOSTROPHE . 'nuff',
553
		"'round"    => U::APOSTROPHE . 'round',
554
		"'cause"    => U::APOSTROPHE . 'cause',
555
		"'splainin" => U::APOSTROPHE . 'splainin',
556
		"'em'"      => U::APOSTROPHE . 'em',
557
	] ) {
558 1
		$this->data['smartQuotesExceptions'] = [
559 1
			'patterns'     => \array_keys( $exceptions ),
560 1
			'replacements' => \array_values( $exceptions ),
561
		];
562 1
	}
563
564
	/**
565
	 * Retrieves an object from a given style.
566
	 *
567
	 * @param  object|string $style          A style object instance or a style constant.
568
	 * @param  string        $expected_class A class name.
569
	 * @param  callable      $get_style      A function that returns a style object from a given style constant.
570
	 * @param  string        $description    Style description for the exception message.
571
	 *
572
	 * @throws \DomainException Thrown if $style constant is invalid.
573
	 *
574
	 * @return object An instance of $expected_class.
575
	 */
576 9
	protected function get_style( $style, $expected_class, callable $get_style, $description ) {
577 9
		if ( $style instanceof $expected_class ) {
578 3
			$object = $style;
579
		} else {
580 6
			$object = $get_style( $style, $this );
581
		}
582
583 9
		if ( ! \is_object( $object ) || ! $object instanceof $expected_class ) {
584 3
			throw new \DomainException( "Invalid $description style $style." );
585
		}
586
587 6
		return $object;
588
	}
589
590
	/**
591
	 * Enables/disables replacement of "a--a" with En Dash " -- " and "---" with Em Dash.
592
	 *
593
	 * @param bool $on Optional. Default true.
594
	 */
595 1
	public function set_smart_dashes( $on = true ) {
596 1
		$this->data['smartDashes'] = $on;
597 1
	}
598
599
	/**
600
	 * Sets the typographical conventions used by smart_dashes.
601
	 *
602
	 * Allowed values for $style:
603
	 * - "traditionalUS"
604
	 * - "international"
605
	 *
606
	 * @param string|Settings\Dashes $style Optional. Default Dash_Style::TRADITIONAL_US.
607
	 *
608
	 * @throws \DomainException Thrown if $style constant is invalid.
609
	 */
610 3
	public function set_smart_dashes_style( $style = Dash_Style::TRADITIONAL_US ) {
611 3
		$this->dash_style = $this->get_style( $style, Settings\Dashes::class, [ Dash_Style::class, 'get_styled_dashes' ], 'dash' );
612 2
	}
613
614
	/**
615
	 * Enables/disables replacement of "..." with "…".
616
	 *
617
	 * @param bool $on Optional. Default true.
618
	 */
619 1
	public function set_smart_ellipses( $on = true ) {
620 1
		$this->data['smartEllipses'] = $on;
621 1
	}
622
623
	/**
624
	 * Enables/disables replacement "creme brulee" with "crème brûlée".
625
	 *
626
	 * @param bool $on Optional. Default true.
627
	 */
628 1
	public function set_smart_diacritics( $on = true ) {
629 1
		$this->data['smartDiacritics'] = $on;
630 1
	}
631
632
	/**
633
	 * Sets the language used for diacritics replacements.
634
	 *
635
	 * @param string $lang Has to correspond to a filename in 'diacritics'. Optional. Default 'en-US'.
636
	 */
637 1
	public function set_diacritic_language( $lang = 'en-US' ) {
638 1
		if ( isset( $this->data['diacriticLanguage'] ) && $this->data['diacriticLanguage'] === $lang ) {
639 1
			return;
640
		}
641
642 1
		$this->data['diacriticLanguage'] = $lang;
643 1
		$language_file_name              = \dirname( __FILE__ ) . '/diacritics/' . $lang . '.json';
644
645 1
		if ( \file_exists( $language_file_name ) ) {
646 1
			$diacritics_file              = \json_decode( \file_get_contents( $language_file_name ), true );
647 1
			$this->data['diacriticWords'] = $diacritics_file['diacritic_words'];
648
		} else {
649 1
			unset( $this->data['diacriticWords'] );
650
		}
651
652 1
		$this->update_diacritics_replacement_arrays();
653 1
	}
654
655
	/**
656
	 * Sets up custom diacritics replacements.
657
	 *
658
	 * @param string|array $custom_replacements An array formatted [needle=>replacement, needle=>replacement...],
659
	 *                                          or a string formatted `"needle"=>"replacement","needle"=>"replacement",...
660
	 */
661 5
	public function set_diacritic_custom_replacements( $custom_replacements = [] ) {
662 5
		if ( ! \is_array( $custom_replacements ) ) {
663 2
			$custom_replacements = $this->parse_diacritics_replacement_string( $custom_replacements );
664
		}
665
666 5
		$this->data['diacriticCustomReplacements'] = self::array_map_assoc(
667
			function( $key, $replacement ) {
668 5
				$key         = \strip_tags( \trim( $key ) );
669 5
				$replacement = \strip_tags( \trim( $replacement ) );
670
671 5
				if ( ! empty( $key ) && ! empty( $replacement ) ) {
672 3
					return [ $key => $replacement ];
673
				} else {
674 2
					return [];
675
				}
676 5
			},
677
			$custom_replacements
678
		);
679
680 5
		$this->update_diacritics_replacement_arrays();
681 5
	}
682
683
	/**
684
	 * Parses a custom diacritics replacement string into an array.
685
	 *
686
	 * @param string $custom_replacements A string formatted `"needle"=>"replacement","needle"=>"replacement",...
687
	 *
688
	 * @return array
689
	 */
690 2
	private function parse_diacritics_replacement_string( $custom_replacements ) {
691 2
		return self::array_map_assoc(
692
			function( $key, $replacement ) {
693
				// Account for single and double quotes in keys in and values, discard everything else.
694 2
				if ( \preg_match( '/(?<kquo>"|\')(?<key>(?:(?!\k<kquo>).)+)\k<kquo>\s*=>\s*(?<rquo>"|\')(?<replacement>(?:(?!\k<rquo>).)+)\k<rquo>/', $replacement, $match ) ) {
695 2
					$key         = $match['key'];
696 2
					$replacement = $match['replacement'];
697
698 2
					return [ $key => $replacement ];
699
				}
700
701
				return [];
702 2
			},
703
			/** RE correct. @scrutinizer ignore-type */
704 2
			\preg_split( '/,/', $custom_replacements, -1, PREG_SPLIT_NO_EMPTY )
705
		);
706
	}
707
708
	/**
709
	 * Provides an array_map implementation with control over resulting array's keys.
710
	 *
711
	 * Based on https://gist.github.com/jasand-pereza/84ecec7907f003564584.
712
	 *
713
	 * @since 6.0.0
714
	 *
715
	 * @param  callable $callback A callback function that needs to return [ $key => $value ] pairs.
716
	 * @param  array    $array    The array.
717
	 *
718
	 * @return array
719
	 */
720 2
	protected static function array_map_assoc( callable $callback, array $array ) {
721 2
		$new = [];
722
723 2
		foreach ( $array as $k => $v ) {
724 2
			$u = $callback( $k, $v );
725
726 2
			if ( ! empty( $u ) ) {
727 1
				$new[ \key( $u ) ] = \current( $u );
728
			}
729
		}
730
731 2
		return $new;
732
	}
733
734
	/**
735
	 * Update the pattern and replacement arrays in $settings['diacriticReplacement'].
736
	 *
737
	 * Should be called whenever a new diacritics replacement language is selected or
738
	 * when the custom replacements are updated.
739
	 */
740 6
	private function update_diacritics_replacement_arrays() {
741 6
		$patterns     = [];
742 6
		$replacements = [];
743
744 6
		if ( ! empty( $this->data['diacriticCustomReplacements'] ) ) {
745 3
			$this->parse_diacritics_rules( $this->data['diacriticCustomReplacements'], $patterns, $replacements );
746
		}
747 6
		if ( ! empty( $this->data['diacriticWords'] ) ) {
748 1
			$this->parse_diacritics_rules( $this->data['diacriticWords'], $patterns, $replacements );
749
		}
750
751 6
		$this->data['diacriticReplacement'] = [
752 6
			'patterns'     => $patterns,
753 6
			'replacements' => $replacements,
754
		];
755 6
	}
756
757
	/**
758
	 * Parse an array of diacritics rules.
759
	 *
760
	 * @param array $diacritics_rules The rules ( $word => $replacement ).
761
	 * @param array $patterns         Resulting patterns. Passed by reference.
762
	 * @param array $replacements     Resulting replacements. Passed by reference.
763
	 */
764 4
	private function parse_diacritics_rules( array $diacritics_rules, array &$patterns, array &$replacements ) {
765
766 4
		foreach ( $diacritics_rules as $needle => $replacement ) {
767 4
			$patterns[]              = '/\b(?<!\w[' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . '])' . $needle . '\b(?![' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . ']\w)/u';
768 4
			$replacements[ $needle ] = $replacement;
769
		}
770 4
	}
771
772
	/**
773
	 * Enables/disables replacement of (r) (c) (tm) (sm) (p) (R) (C) (TM) (SM) (P) with ® © ™ ℠ ℗.
774
	 *
775
	 * @param bool $on Optional. Default true.
776
	 */
777 1
	public function set_smart_marks( $on = true ) {
778 1
		$this->data['smartMarks'] = $on;
779 1
	}
780
781
	/**
782
	 * Enables/disables proper mathematical symbols.
783
	 *
784
	 * @param bool $on Optional. Default true.
785
	 */
786 1
	public function set_smart_math( $on = true ) {
787 1
		$this->data['smartMath'] = $on;
788 1
	}
789
790
	/**
791
	 * Enables/disables replacement of 2^2 with 2<sup>2</sup>
792
	 *
793
	 * @param bool $on Optional. Default true.
794
	 */
795 1
	public function set_smart_exponents( $on = true ) {
796 1
		$this->data['smartExponents'] = $on;
797 1
	}
798
799
	/**
800
	 * Enables/disables replacement of 1/4 with <sup>1</sup>&#8260;<sub>4</sub>.
801
	 *
802
	 * @param bool $on Optional. Default true.
803
	 */
804 1
	public function set_smart_fractions( $on = true ) {
805 1
		$this->data['smartFractions'] = $on;
806 1
	}
807
808
	/**
809
	 * Enables/disables replacement of 1st with 1<sup>st</sup>.
810
	 *
811
	 * @param bool $on Optional. Default true.
812
	 */
813 1
	public function set_smart_ordinal_suffix( $on = true ) {
814 1
		$this->data['smartOrdinalSuffix'] = $on;
815 1
	}
816
817
	/**
818
	 * Enables/disables replacement of XXe with XX<sup>e</sup>.
819
	 *
820
	 * @since 6.5.0
821
	 *
822
	 * @param bool $on Optional. Default false.
823
	 */
824 1
	public function set_smart_ordinal_suffix_match_roman_numerals( $on = false ) {
825 1
		$this->data['smartOrdinalSuffixRomanNumerals'] = $on;
826 1
	}
827
828
	/**
829
	 * Enables/disables replacement of m2 with m³ and m3 with m³.
830
	 *
831
	 * @param bool $on Optional. Default true.
832
	 */
833 1
	public function set_smart_area_units( $on = true ) {
834 1
		$this->data['smartAreaVolumeUnits'] = $on;
835 1
	}
836
837
	/**
838
	 * Enables/disables forcing single character words to next line with the insertion of &nbsp;.
839
	 *
840
	 * @param bool $on Optional. Default true.
841
	 */
842 1
	public function set_single_character_word_spacing( $on = true ) {
843 1
		$this->data['singleCharacterWordSpacing'] = $on;
844 1
	}
845
846
	/**
847
	 * Enables/disables fraction spacing.
848
	 *
849
	 * @param bool $on Optional. Default true.
850
	 */
851 1
	public function set_fraction_spacing( $on = true ) {
852 1
		$this->data['fractionSpacing'] = $on;
853 1
	}
854
855
	/**
856
	 * Enables/disables keeping units and values together with the insertion of &nbsp;.
857
	 *
858
	 * @param bool $on Optional. Default true.
859
	 */
860 1
	public function set_unit_spacing( $on = true ) {
861 1
		$this->data['unitSpacing'] = $on;
862 1
	}
863
864
	/**
865
	 * Enables/disables numbered abbreviations like "ISO 9000" together with the insertion of &nbsp;.
866
	 *
867
	 * @param bool $on Optional. Default true.
868
	 */
869 1
	public function set_numbered_abbreviation_spacing( $on = true ) {
870 1
		$this->data['numberedAbbreviationSpacing'] = $on;
871 1
	}
872
873
	/**
874
	 * Enables/disables extra whitespace before certain punction marks, as is the French custom.
875
	 *
876
	 * @since 6.0.0 The default value is now `false`.`
877
	 *
878
	 * @param bool $on Optional. Default false.
879
	 */
880 1
	public function set_french_punctuation_spacing( $on = false ) {
881 1
		$this->data['frenchPunctuationSpacing'] = $on;
882 1
	}
883
884
	/**
885
	 * Sets the list of units to keep together with their values.
886
	 *
887
	 * @param string|array $units A comma separated list or an array of units.
888
	 */
889 1
	public function set_units( $units = [] ) {
890 1
		$this->data['units'] = Strings::maybe_split_parameters( $units );
891 1
		$this->custom_units  = $this->update_unit_pattern( $this->data['units'] );
892 1
	}
893
894
	/**
895
	 * Update pattern for matching custom units.
896
	 *
897
	 * @since 6.4.0 Visibility changed to protected, return value added.
898
	 *
899
	 * @param array $units An array of unit names.
900
	 *
901
	 * @return string
902
	 */
903 2
	protected function update_unit_pattern( array $units ) {
904
		// Update unit regex pattern.
905 2
		foreach ( $units as $index => $unit ) {
906 2
			$units[ $index ] = \preg_quote( $unit, '/' );
907
		}
908
909 2
		$custom_units  = \implode( '|', $units );
910 2
		$custom_units .= ! empty( $custom_units ) ? '|' : '';
911
912 2
		return $custom_units;
913
	}
914
915
	/**
916
	 * Enables/disables wrapping of Em and En dashes are in thin spaces.
917
	 *
918
	 * @param bool $on Optional. Default true.
919
	 */
920 1
	public function set_dash_spacing( $on = true ) {
921 1
		$this->data['dashSpacing'] = $on;
922 1
	}
923
924
	/**
925
	 * Enables/disables removal of extra whitespace characters.
926
	 *
927
	 * @param bool $on Optional. Default true.
928
	 */
929 1
	public function set_space_collapse( $on = true ) {
930 1
		$this->data['spaceCollapse'] = $on;
931 1
	}
932
933
	/**
934
	 * Enables/disables widow handling.
935
	 *
936
	 * @param bool $on Optional. Default true.
937
	 */
938 1
	public function set_dewidow( $on = true ) {
939 1
		$this->data['dewidow'] = $on;
940 1
	}
941
942
	/**
943
	 * Sets the maximum length of widows that will be protected.
944
	 *
945
	 * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default.
946
	 */
947 1
	public function set_max_dewidow_length( $length = 5 ) {
948 1
		$length = ( $length > 1 ) ? $length : 5;
949
950 1
		$this->data['dewidowMaxLength'] = $length;
951 1
	}
952
953
	/**
954
	 * Sets the maximum number of words considered for dewidowing.
955
	 *
956
	 * @param int $number Defaults to 1. Only 1, 2 and 3 are valid.
957
	 */
958 1
	public function set_dewidow_word_number( $number = 1 ) {
959 1
		$number = ( $number > 3 || $number < 1 ) ? 1 : $number;
960
961 1
		$this->data['dewidowWordNumber'] = $number;
962 1
	}
963
964
	/**
965
	 * Sets the maximum length of pulled text to keep widows company.
966
	 *
967
	 * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default.
968
	 */
969 1
	public function set_max_dewidow_pull( $length = 5 ) {
970 1
		$length = ( $length > 1 ) ? $length : 5;
971
972 1
		$this->data['dewidowMaxPull'] = $length;
973 1
	}
974
975
	/**
976
	 * Enables/disables wrapping at internal hard hyphens with the insertion of a zero-width-space.
977
	 *
978
	 * @param bool $on Optional. Default true.
979
	 */
980 1
	public function set_wrap_hard_hyphens( $on = true ) {
981 1
		$this->data['hyphenHardWrap'] = $on;
982 1
	}
983
984
	/**
985
	 * Enables/disables wrapping of urls.
986
	 *
987
	 * @param bool $on Optional. Default true.
988
	 */
989 1
	public function set_url_wrap( $on = true ) {
990 1
		$this->data['urlWrap'] = $on;
991 1
	}
992
993
	/**
994
	 * Enables/disables wrapping of email addresses.
995
	 *
996
	 * @param bool $on Optional. Default true.
997
	 */
998 1
	public function set_email_wrap( $on = true ) {
999 1
		$this->data['emailWrap'] = $on;
1000 1
	}
1001
1002
	/**
1003
	 * Sets the minimum character requirement after an URL wrapping point.
1004
	 *
1005
	 * @param int $length Defaults to 5. Trying to set the value to less than 1 resets the length to the default.
1006
	 */
1007 1
	public function set_min_after_url_wrap( $length = 5 ) {
1008 1
		$length = ( $length > 0 ) ? $length : 5;
1009
1010 1
		$this->data['urlMinAfterWrap'] = $length;
1011 1
	}
1012
1013
	/**
1014
	 * Enables/disables wrapping of ampersands in <span class="amp">.
1015
	 *
1016
	 * @param bool $on Optional. Default true.
1017
	 */
1018 1
	public function set_style_ampersands( $on = true ) {
1019 1
		$this->data['styleAmpersands'] = $on;
1020 1
	}
1021
1022
	/**
1023
	 * Enables/disables wrapping caps in <span class="caps">.
1024
	 *
1025
	 * @param bool $on Optional. Default true.
1026
	 */
1027 1
	public function set_style_caps( $on = true ) {
1028 1
		$this->data['styleCaps'] = $on;
1029 1
	}
1030
1031
	/**
1032
	 * Enables/disables wrapping of initial quotes in <span class="quo"> or <span class="dquo">.
1033
	 *
1034
	 * @param bool $on Optional. Default true.
1035
	 */
1036 1
	public function set_style_initial_quotes( $on = true ) {
1037 1
		$this->data['styleInitialQuotes'] = $on;
1038 1
	}
1039
1040
	/**
1041
	 * Enables/disables wrapping of numbers in <span class="numbers">.
1042
	 *
1043
	 * @param bool $on Optional. Default true.
1044
	 */
1045 1
	public function set_style_numbers( $on = true ) {
1046 1
		$this->data['styleNumbers'] = $on;
1047 1
	}
1048
1049
	/**
1050
	 * Enables/disables wrapping of punctiation and wide characters in <span class="pull-*">.
1051
	 *
1052
	 * @param bool $on Optional. Default true.
1053
	 */
1054 1
	public function set_style_hanging_punctuation( $on = true ) {
1055 1
		$this->data['styleHangingPunctuation'] = $on;
1056 1
	}
1057
1058
	/**
1059
	 * Sets the list of tags where initial quotes and guillemets should be styled.
1060
	 *
1061
	 * @param string|array $tags A comma separated list or an array of tag names.
1062
	 */
1063 1
	public function set_initial_quote_tags( $tags = [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li', 'dd', 'dt' ] ) {
1064
		// Make array if handed a list of tags as a string.
1065 1
		if ( ! \is_array( $tags ) ) {
1066 1
			$tags = \preg_split( '/[^a-z0-9]+/', $tags, -1, PREG_SPLIT_NO_EMPTY );
1067
		}
1068
1069
		// Store the tag array inverted (with the tagName as its index for faster lookup).
1070 1
		$this->data['initialQuoteTags'] = \array_change_key_case( \array_flip( /** Array. @scrutinizer ignore-type */ $tags ), CASE_LOWER );
1071 1
	}
1072
1073
	/**
1074
	 * Enables/disables hyphenation.
1075
	 *
1076
	 * @param bool $on Optional. Default true.
1077
	 */
1078 1
	public function set_hyphenation( $on = true ) {
1079 1
		$this->data['hyphenation'] = $on;
1080 1
	}
1081
1082
	/**
1083
	 * Sets the hyphenation pattern language.
1084
	 *
1085
	 * @param string $lang Has to correspond to a filename in 'lang'. Optional. Default 'en-US'.
1086
	 */
1087 8
	public function set_hyphenation_language( $lang = 'en-US' ) {
1088 8
		if ( isset( $this->data['hyphenLanguage'] ) && $this->data['hyphenLanguage'] === $lang ) {
1089 3
			return; // Bail out, no need to do anything.
1090
		}
1091
1092 8
		$this->data['hyphenLanguage'] = $lang;
1093 8
	}
1094
1095
	/**
1096
	 * Sets the minimum length of a word that may be hyphenated.
1097
	 *
1098
	 * @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default.
1099
	 */
1100 1
	public function set_min_length_hyphenation( $length = 5 ) {
1101 1
		$length = ( $length > 1 ) ? $length : 5;
1102
1103 1
		$this->data['hyphenMinLength'] = $length;
1104 1
	}
1105
1106
	/**
1107
	 * Sets the minimum character requirement before a hyphenation point.
1108
	 *
1109
	 * @param int $length Defaults to 3. Trying to set the value to less than 1 resets the length to the default.
1110
	 */
1111 1
	public function set_min_before_hyphenation( $length = 3 ) {
1112 1
		$length = ( $length > 0 ) ? $length : 3;
1113
1114 1
		$this->data['hyphenMinBefore'] = $length;
1115 1
	}
1116
1117
	/**
1118
	 * Sets the minimum character requirement after a hyphenation point.
1119
	 *
1120
	 * @param int $length Defaults to 2. Trying to set the value to less than 1 resets the length to the default.
1121
	 */
1122 1
	public function set_min_after_hyphenation( $length = 2 ) {
1123 1
		$length = ( $length > 0 ) ? $length : 2;
1124
1125 1
		$this->data['hyphenMinAfter'] = $length;
1126 1
	}
1127
1128
	/**
1129
	 * Enables/disables hyphenation of titles and headings.
1130
	 *
1131
	 * @param bool $on Optional. Default true.
1132
	 */
1133 1
	public function set_hyphenate_headings( $on = true ) {
1134 1
		$this->data['hyphenateTitle'] = $on;
1135 1
	}
1136
1137
	/**
1138
	 * Enables/disables hyphenation of words set completely in capital letters.
1139
	 *
1140
	 * @param bool $on Optional. Default true.
1141
	 */
1142 1
	public function set_hyphenate_all_caps( $on = true ) {
1143 1
		$this->data['hyphenateAllCaps'] = $on;
1144 1
	}
1145
1146
	/**
1147
	 * Enables/disables hyphenation of words starting with a capital letter.
1148
	 *
1149
	 * @param bool $on Optional. Default true.
1150
	 */
1151 1
	public function set_hyphenate_title_case( $on = true ) {
1152 1
		$this->data['hyphenateTitleCase'] = $on;
1153 1
	}
1154
1155
	/**
1156
	 * Enables/disables hyphenation of compound words (e.g. "editor-in-chief").
1157
	 *
1158
	 * @param bool $on Optional. Default true.
1159
	 */
1160 1
	public function set_hyphenate_compounds( $on = true ) {
1161 1
		$this->data['hyphenateCompounds'] = $on;
1162 1
	}
1163
1164
	/**
1165
	 * Sets custom word hyphenations.
1166
	 *
1167
	 * @param string|array $exceptions An array of words with all hyphenation points marked with a hard hyphen (or a string list of such words).
1168
	 *        In the latter case, only alphanumeric characters and hyphens are recognized. The default is empty.
1169
	 */
1170 2
	public function set_hyphenation_exceptions( $exceptions = [] ) {
1171 2
		$this->data['hyphenationCustomExceptions'] = Strings::maybe_split_parameters( $exceptions );
1172 2
	}
1173
1174
	/**
1175
	 * Retrieves a unique hash value for the current settings.
1176
	 *
1177
	 * @since 5.2.0 The new parameter $raw_output has been added.
1178
	 *
1179
	 * @param int  $max_length Optional. The maximum number of bytes returned (0 for unlimited). Default 16.
1180
	 * @param bool $raw_output Optional. Wether to return raw binary data for the hash. Default true.
1181
	 *
1182
	 * @return string A binary hash value for the current settings limited to $max_length.
1183
	 */
1184 1
	public function get_hash( $max_length = 16, $raw_output = true ) {
1185 1
		$hash = \md5( \json_encode( $this ), $raw_output );
1186
1187 1
		if ( $max_length < \strlen( $hash ) && $max_length > 0 ) {
1188 1
			$hash = \substr( $hash, 0, $max_length );
1189
		}
1190
1191 1
		return $hash;
1192
	}
1193
}
1194