RegexpBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromList() 0 27 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
}