RegexpBuilder::fromList()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 9.4555
c 0
b 0
f 0
cc 5
nc 2
nop 2
crap 5
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\Helpers;
9
10
use s9e\RegexpBuilder\Builder;
11
12
abstract class RegexpBuilder
13
{
14
	/**
15
	* Create a regexp pattern that matches a list of words
16
	*
17
	* @param  array  $words   Words to sort (must be UTF-8)
18
	* @param  array  $options
19
	* @return string
20
	*/
21 73
	public static function fromList(array $words, array $options = [])
22
	{
23
		$options += [
24 73
			'delimiter'       => '/',
25
			'caseInsensitive' => false,
26
			'specialChars'    => [],
27
			'unicode'         => true
28
		];
29
30
		// Normalize ASCII if the regexp is meant to be case-insensitive
31 73
		if ($options['caseInsensitive'])
32
		{
33 2
			foreach ($words as &$word)
34
			{
35 2
				$word = strtr($word, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
36
			}
37 2
			unset($word);
38
		}
39
40 73
		$builder = new Builder([
41 73
			'delimiter' => $options['delimiter'],
42 73
			'meta'      => $options['specialChars'],
43 73
			'input'     => $options['unicode'] ? 'Utf8' : 'Bytes',
44 73
			'output'    => $options['unicode'] ? 'Utf8' : 'Bytes'
45
		]);
46
47 73
		return $builder->build($words);
48
	}
49
}