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
05:22
created

Settings::remap_character()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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