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.
Completed
Pull Request — master (#118)
by Der Mundschenk
06:06
created

Settings::set_smart_dashes_style()   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 If true, set default values for various properties. Defaults to true.
117
	 * @param string[] $mapping      A (partial) Unicode character remapping.
118
	 */
119 1
	public function __construct( $set_defaults = true, array $mapping = [] ) {
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     = \array_merge( U::DEFAULT_MAPPING, $mapping );
126 1
		$this->remapped_characters = \array_filter(
127 1
			$this->unicode_mapping,
128
			function( $key, $value ) {
129 1
				return $key !== $value;
130 1
			},
131 1
			\ARRAY_FILTER_USE_BOTH
132
		);
133
134
		// Keep backwards compatibility.
135 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

135
		/** @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...
136 1
	}
137
138
	/**
139
	 * Provides access to named settings (object syntax).
140
	 *
141
	 * @param string $key The settings key.
142
	 *
143
	 * @return mixed
144
	 */
145 1
	public function &__get( $key ) {
146 1
		return $this->data[ $key ];
147
	}
148
149
	/**
150
	 * Changes a named setting (object syntax).
151
	 *
152
	 * @param string $key   The settings key.
153
	 * @param mixed  $value The settings value.
154
	 */
155 1
	public function __set( $key, $value ) {
156 1
		$this->data[ $key ] = $value;
157 1
	}
158
159
	/**
160
	 * Checks if a named setting exists (object syntax).
161
	 *
162
	 * @param string $key The settings key.
163
	 */
164 1
	public function __isset( $key ) {
165 1
		return isset( $this->data[ $key ] );
166
	}
167
168
	/**
169
	 * Unsets a named setting.
170
	 *
171
	 * @param string $key The settings key.
172
	 */
173 1
	public function __unset( $key ) {
174 1
		unset( $this->data[ $key ] );
175 1
	}
176
177
	/**
178
	 * Changes a named setting (array syntax).
179
	 *
180
	 * @param string $offset The settings key.
181
	 * @param mixed  $value  The settings value.
182
	 */
183 1
	public function offsetSet( $offset, $value ) {
184 1
		if ( ! empty( $offset ) ) {
185 1
			$this->data[ $offset ] = $value;
186
		}
187 1
	}
188
189
	/**
190
	 * Checks if a named setting exists (array syntax).
191
	 *
192
	 * @param string $offset The settings key.
193
	 */
194 1
	public function offsetExists( $offset ) {
195 1
		return isset( $this->data[ $offset ] );
196
	}
197
198
	/**
199
	 * Unsets a named setting (array syntax).
200
	 *
201
	 * @param string $offset The settings key.
202
	 */
203 1
	public function offsetUnset( $offset ) {
204 1
		unset( $this->data[ $offset ] );
205 1
	}
206
207
	/**
208
	 * Provides access to named settings (array syntax).
209
	 *
210
	 * @param string $offset The settings key.
211
	 *
212
	 * @return mixed
213
	 */
214 1
	public function offsetGet( $offset ) {
215 1
		return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null;
216
	}
217
218
	/**
219
	 * Provides a JSON serialization of the settings.
220
	 *
221
	 * @return mixed
222
	 */
223 1
	public function jsonSerialize() {
224 1
		return \array_merge(
225 1
			$this->data,
226
			[
227 1
				'unicode_mapping'       => $this->unicode_mapping,
228 1
				'primary_quotes'        => "{$this->primary_quote_style->open()}|{$this->primary_quote_style->close()}",
229 1
				'secondary_quotes'      => "{$this->secondary_quote_style->open()}|{$this->secondary_quote_style->close()}",
230 1
				'dash_style'            => "{$this->dash_style->interval_dash()}|{$this->dash_style->interval_space()}|{$this->dash_style->parenthetical_dash()}|{$this->dash_style->parenthetical_space()}",
231 1
				'custom_units'          => $this->custom_units,
232
			]
233
		);
234
	}
235
236
	/**
237
	 * Retrieves the unicode character array (including any remappings).
238
	 *
239
	 * @since 6.5.0
240
	 *
241
	 * @return string[]
242
	 */
243
	public function character_mapping() {
244
		return $this->unicode_mapping;
245
	}
246
247
	/**
248
	 * Remaps a unicode character to another one.
249
	 *
250
	 * @since 6.5.0
251
	 *
252
	 * @param  string $char     The remapped character.
253
	 * @param  string $new_char The character to actually use.
254
	 *
255
	 * @return bool             Returns true on success, false otherwise.
256
	 */
257
	public function remap_character( $char, $new_char ) {
258
		if ( isset( $this->unicode_mapping[ $char ] ) ) {
259
			$this->unicode_mapping[ $char ] = $new_char;
260
261
			if ( $char !== $new_char ) {
262
				$this->remapped_characters[ $char ] = $new_char;
263
			} else {
264
				unset( $this->remapped_characters[ $char ] );
265
			}
266
267
			// Compatibility with the old way of setting the no-break narrow space.
268
			if ( U::NO_BREAK_NARROW_SPACE === $char ) {
269
				$this->no_break_narrow_space = $new_char;
270
			}
271
272
			return true;
273
		}
274
275
		return false;
276
	}
277
278
	/**
279
	 * Remaps one or more strings.
280
	 *
281
	 * @since 6.5.0
282
	 *
283
	 * @param  string|string[] $input The input string(s).
284
	 *
285
	 * @return string|string[]
286
	 */
287
	public function apply_mapping( $input ) {
288
289
		// Nothing for us to do.
290
		if ( empty( $input ) || empty( $this->remapped_characters ) ) {
291
			return $input;
292
		}
293
294
		$native_array = \is_array( $input );
295
		$data         = (array) $input;
296
297
		foreach ( $data as $key => $string ) {
298
			$data[ $key ] = \strtr( $string, $this->remapped_characters );
299
		}
300
301
		return $native_array ? $data : $data[0];
302
	}
303
304
	/**
305
	 * Remaps a single character.
306
	 *
307
	 * @since 6.5.0
308
	 *
309
	 * @param  string $single The original character.
310
	 *
311
	 * @return string         The remapped character.
312
	 */
313
	public function map( $single ) {
314
		return isset( $this->remapped_characters[ $single ] ) ? $this->remapped_characters[ $single ] : $single;
315
	}
316
317
	/**
318
	 * Retrieves the current non-breaking narrow space character (either the
319
	 * regular non-breaking space &nbsp; or the the true non-breaking narrow space &#8239;).
320
	 *
321
	 * @deprecated 6.5.0 Use ::character_mapping() instead.
322
	 *
323
	 * @return string
324
	 */
325 1
	public function no_break_narrow_space() {
326 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

326
		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...
327
	}
328
329
	/**
330
	 * Retrieves the primary (double) quote style.
331
	 *
332
	 * @return Quotes
333
	 */
334 1
	public function primary_quote_style() {
335 1
		return $this->primary_quote_style;
336
	}
337
338
	/**
339
	 * Retrieves the secondary (single) quote style.
340
	 *
341
	 * @return Quotes
342
	 */
343 1
	public function secondary_quote_style() {
344 1
		return $this->secondary_quote_style;
345
	}
346
347
	/**
348
	 * Retrieves the dash style.
349
	 *
350
	 * @return Settings\Dashes
351
	 */
352 1
	public function dash_style() {
353 1
		return $this->dash_style;
354
	}
355
356
	/**
357
	 * Retrieves the custom units pattern.
358
	 *
359
	 * @return string The pattern is suitable for inclusion into a regular expression.
360
	 */
361 1
	public function custom_units() {
362 1
		return $this->custom_units;
363
	}
364
365
	/**
366
	 * (Re)set various options to their default values.
367
	 */
368 1
	public function set_defaults() {
369
		// General attributes.
370 1
		$this->set_tags_to_ignore();
371 1
		$this->set_classes_to_ignore();
372 1
		$this->set_ids_to_ignore();
373
374
		// Smart characters.
375 1
		$this->set_smart_quotes();
376 1
		$this->set_smart_quotes_primary();
377 1
		$this->set_smart_quotes_secondary();
378 1
		$this->set_smart_quotes_exceptions();
379 1
		$this->set_smart_dashes();
380 1
		$this->set_smart_dashes_style();
381 1
		$this->set_smart_ellipses();
382 1
		$this->set_smart_diacritics();
383 1
		$this->set_diacritic_language();
384 1
		$this->set_diacritic_custom_replacements();
385 1
		$this->set_smart_marks();
386 1
		$this->set_smart_ordinal_suffix();
387 1
		$this->set_smart_ordinal_suffix_match_roman_numerals();
388 1
		$this->set_smart_math();
389 1
		$this->set_smart_fractions();
390 1
		$this->set_smart_exponents();
391 1
		$this->set_smart_area_units();
392
393
		// Smart spacing.
394 1
		$this->set_single_character_word_spacing();
395 1
		$this->set_fraction_spacing();
396 1
		$this->set_unit_spacing();
397 1
		$this->set_french_punctuation_spacing();
398 1
		$this->set_units();
399 1
		$this->set_dash_spacing();
400 1
		$this->set_dewidow();
401 1
		$this->set_max_dewidow_length();
402 1
		$this->set_max_dewidow_pull();
403 1
		$this->set_dewidow_word_number();
404 1
		$this->set_wrap_hard_hyphens();
405 1
		$this->set_url_wrap();
406 1
		$this->set_email_wrap();
407 1
		$this->set_min_after_url_wrap();
408 1
		$this->set_space_collapse();
409 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

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