RegularExpression::createSearchPattern()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
3
namespace EasyDictionary;
4
5
/**
6
 * Class RegularExpression
7
 * @package EasyDictionary
8
 */
9
class RegularExpression
10
{
11
    /**
12
     * Create search pattern
13
     *
14
     * @param string|array $searchPhrases
15
     * @param bool $strictMode
16
     *
17
     * @return string
18
     */
19 1
    public static function createSearchPattern($searchPhrases, bool $strictMode = false): string
20
    {
21 1
        if (!is_array($searchPhrases)) {
22 1
            $searchPhrases = explode(',', $searchPhrases);
23
        }
24
25 1
        $patternGroups = [];
26 1
        foreach ($searchPhrases as $phrase) {
27 1
            $phrase = preg_quote($phrase, '/');
28 1
            $patternGroups[] = $strictMode ? "((\s+)?{$phrase}(\s+)?)" : "({$phrase})";
29
        }
30
31 1
        $pattern = '/' . implode('|', $patternGroups) . '/i';
32
33 1
        return $pattern;
34
    }
35
}
36