Passed
Push — master ( 1564fa...d2ad69 )
by Kacper
03:00 queued 15s
created

WordMatcher::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
ccs 21
cts 21
cp 1
rs 8.5806
cc 4
eloc 17
nc 8
nop 2
crap 4
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
19
class WordMatcher extends RegexMatcher
20
{
21
22
    /**
23
     * WordMatcher constructor.
24
     *
25
     * @param array $words
26
     * @param array $options
27
     */
28 5
    public function __construct(array $words, array $options = [])
29
    {
30 5
        $options = array_merge([
31 5
            'escape'           => true,
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['separated']) {
44 4
            $regex = '\b' . $regex . '\b';
45 4
        }
46
47 5
        $regex = "/$regex/";
48 5
        if (!$options['case-sensitivity']) {
49 4
            $regex .= 'i';
50 4
        }
51
52 5
        parent::__construct($regex);
53
    }
54
}