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 (#44)
by Der Mundschenk
02:50
created

Settings::get_dash_style()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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