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 (#56)
by Der Mundschenk
14:38 queued 04:42
created

Settings   F

Complexity

Total Complexity 111

Size/Duplication

Total Lines 1010
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 111
dl 0
loc 1010
ccs 332
cts 332
cp 1
rs 1.2
c 0
b 0
f 0

76 Methods

Rating   Name   Duplication   Size   Complexity  
A dash_style() 0 2 1
A set_true_no_break_narrow_space() 0 6 2
A set_smart_diacritics() 0 2 1
A set_smart_quotes_secondary() 0 2 1
A __construct() 0 3 2
A __set() 0 2 1
A set_smart_dashes_style() 0 2 1
A __isset() 0 2 1
A __get() 0 2 1
A set_smart_dashes() 0 2 1
A offsetUnset() 0 2 1
A get_quote_style() 0 2 1
A set_smart_ellipses() 0 2 1
A no_break_narrow_space() 0 2 1
A primary_quote_style() 0 2 1
A set_smart_quotes() 0 2 1
A set_diacritic_language() 0 16 4
A offsetSet() 0 3 2
A set_ignore_parser_errors() 0 2 1
A custom_units() 0 2 1
A set_ids_to_ignore() 0 2 1
A set_classes_to_ignore() 0 2 1
A jsonSerialize() 0 9 1
A offsetExists() 0 2 1
A __unset() 0 2 1
A set_parser_errors_handler() 0 2 1
A set_tags_to_ignore() 0 5 1
A set_smart_quotes_primary() 0 2 1
A set_diacritic_custom_replacements() 0 17 4
A set_defaults() 0 62 1
A offsetGet() 0 2 2
A secondary_quote_style() 0 2 1
A get_style() 0 12 4
A set_smart_fractions() 0 2 1
A set_fraction_spacing() 0 2 1
A set_space_collapse() 0 2 1
A set_min_before_hyphenation() 0 4 2
A set_dewidow_word_number() 0 4 3
A set_dewidow() 0 2 1
A set_smart_ordinal_suffix() 0 2 1
A set_single_character_word_spacing() 0 2 1
A set_smart_marks() 0 2 1
A set_max_dewidow_pull() 0 4 2
A parse_diacritics_replacement_string() 0 14 3
A set_hyphenate_headings() 0 2 1
A set_email_wrap() 0 2 1
A parse_diacritics_rules() 0 5 2
A set_min_after_url_wrap() 0 4 2
A set_smart_exponents() 0 2 1
A get_hash() 0 8 3
A set_unit_spacing() 0 2 1
A set_url_wrap() 0 2 1
A update_diacritics_replacement_arrays() 0 14 3
A set_hyphenate_all_caps() 0 2 1
A set_smart_math() 0 2 1
A set_hyphenation_exceptions() 0 2 1
A set_style_numbers() 0 2 1
A set_max_dewidow_length() 0 4 2
A array_map_assoc() 0 12 3
A set_units() 0 3 1
A set_hyphenate_title_case() 0 2 1
A update_unit_pattern() 0 8 3
A set_french_punctuation_spacing() 0 2 1
A set_style_ampersands() 0 2 1
A set_wrap_hard_hyphens() 0 2 1
A set_min_length_hyphenation() 0 4 2
A set_hyphenation() 0 2 1
A set_initial_quote_tags() 0 8 2
A set_hyphenate_compounds() 0 2 1
A set_style_initial_quotes() 0 2 1
A set_dash_spacing() 0 2 1
A set_min_after_hyphenation() 0 4 2
A set_style_caps() 0 2 1
A set_numbered_abbreviation_spacing() 0 2 1
A set_hyphenation_language() 0 6 3
A set_style_hanging_punctuation() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Settings, and based on these observations, apply Extract Interface, too.

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