Completed
Push — master ( 606ca4...edcce7 )
by Kacper
05:58
created

WordMatcher::subtract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, 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
    private $words   = [];
21
    private $options = [];
22
23
    /**
24
     * WordMatcher constructor.
25
     *
26
     * @param array $words
27
     * @param array $options
28
     */
29 12
    public function __construct(array $words, array $options = [])
30
    {
31 12
        $this->words   = $words;
32 12
        $this->options = $options;
33
34 12
        $options = array_merge([
35 12
            'escape'           => true,
36 12
            'atomic'           => false,
37 12
            'separated'        => true,
38 12
            'case-sensitivity' => false,
39 12
        ], $options);
40
41 12
        if ($options['escape']) {
42 11
            $words = array_map(function ($word) {
43 11
                return preg_quote($word, '/');
44 11
            }, $words);
45 11
        }
46
47 12
        $regex = implode('|', $words);
48 12
        if ($options['atomic']) {
49 4
            $regex = "(?>$regex)";
50 4
        }
51 12
        $regex = "($regex)";
52
53 12
        if ($options['separated']) {
54 7
            $regex = "\\b$regex\\b";
55 7
        }
56
57 12
        $regex = "/$regex/";
58 12
        if (!$options['case-sensitivity']) {
59 11
            $regex .= 'i';
60 11
        }
61
62 12
        parent::__construct($regex);
63 12
    }
64
65 1
    public function merge(array $words)
66
    {
67 1
        return new static(array_merge($this->words, $words), $this->options);
68
    }
69
70 1
    public function subtract(array $words)
71
    {
72 1
        return new static(array_diff($this->words, $words), $this->options);
73
    }
74
75 3
    public function getWords()
76
    {
77 3
        return $this->words;
78
    }
79
80 3
    public function getOptions()
81
    {
82 3
        return $this->options;
83
    }
84
}
85