Passed
Push — master ( aeeab5...454cf7 )
by Kacper
02:57
created

WordMatcher::__construct()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.0113

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 32
ccs 24
cts 26
cp 0.9231
rs 8.439
cc 5
eloc 21
nc 16
nop 2
crap 5.0113
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Matcher;
17
18
class WordMatcher extends RegexMatcher
19
{
20
21
    /**
22
     * WordMatcher constructor.
23
     *
24
     * @param array $words
25
     * @param array $options
26
     */
27 5
    public function __construct(array $words, array $options = [])
28
    {
29 5
        $options = array_merge([
30 5
            'escape'           => true,
31 5
            'atomic'           => false,
32 5
            'separated'        => true,
33 5
            'case-sensitivity' => false,
34 5
        ], $options);
35
36 5
        if ($options['escape']) {
37 4
            $words = array_map(function ($word) {
38 4
                return preg_quote($word, '/');
39 4
            }, $words);
40 4
        }
41
42 5
        $regex = implode('|', $words);
43 5
        if ($options['atomic']) {
44
            $regex = "(?>$regex)";
45
        }
46 5
        $regex = "($regex)";
47
48 5
        if ($options['separated']) {
49 4
            $regex = "\\b$regex\\b";
50 4
        }
51
52 5
        $regex = "/$regex/";
53 5
        if (!$options['case-sensitivity']) {
54 4
            $regex .= 'i';
55 4
        }
56
57 5
        parent::__construct($regex);
58 5
    }
59
}
60