Completed
Push — master ( 88111b...b3fce7 )
by Michael
05:58
created

Highlighter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B replace() 0 24 5
A highlight() 0 9 1
1
<?php namespace XoopsModules\Smartobject;
2
3
use XoopsModules\Smartobject;
4
5
/**
6
 * This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer.
7
 *
8
 * @package keyhighlighter
9
 */
10
11
/**
12
 * keyhighlighter class
13
 *
14
 * This class highlight the chosen keywords in the current output buffer
15
 *
16
 * @package   keyhighlighter
17
 * @author    Setec Astronomy
18
 * @abstract  Highlight specific keywords.
19
 * @copyright 2004
20
 * @example   sample.php A sample code.
21
 * @link      http://setecastronomy.stufftoread.com
22
 */
23
24
25
class Highlighter
26
{
27
    /**
28
     * @access private
29
     */
30
    public $preg_keywords = '';
31
    /**
32
     * @access private
33
     */
34
    public $keywords = '';
35
    /**
36
     * @access private
37
     */
38
    public $singlewords = false;
39
    /**
40
     * @access private
41
     */
42
    public $replace_callback = null;
43
44
    public $content;
45
46
    /**
47
     * Main constructor
48
     *
49
     * This is the main constructor of keyhighlighter class. <br>
50
     * It's the only public method of the class.
51
     * @param string   $keywords         the keywords you want to highlight
52
     * @param boolean  $singlewords      specify if it has to highlight also the single words.
53
     * @param callback $replace_callback a custom callback for keyword highlight.
54
     *                                   <code>
55
     *                                   <?php
56
     *                                   require ('keyhighlighter.class.php');
57
     *
58
     * function my_highlighter ($matches) {
59
     *  return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>';
60
     * }
61
     *
62
     * new keyhighlighter ('W3C', false, 'my_highlighter');
63
     * readfile ('http://www.w3c.org/');
64
     * ?>
65
     * </code>
66
     */
67
    // public function __construct ()
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
    public function __construct($keywords, $singlewords = false, $replace_callback = null)
69
    {
70
        $this->keywords         = $keywords;
71
        $this->singlewords      = $singlewords;
72
        $this->replace_callback = $replace_callback;
73
    }
74
75
    /**
76
     * @access private
77
     * @param $replace_matches
78
     * @return mixed
79
     */
80
    public function replace($replace_matches)
81
    {
82
        $patterns = [];
83
        if ($this->singlewords) {
84
            $keywords = explode(' ', $this->preg_keywords);
85
            foreach ($keywords as $keyword) {
86
                $patterns[] = '/(?' . '>' . $keyword . '+)/si';
87
            }
88
        } else {
89
            $patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si';
90
        }
91
92
        $result = $replace_matches[0];
93
94
        foreach ($patterns as $pattern) {
95
            if (null !== $this->replace_callback) {
96
                $result = preg_replace_callback($pattern, $this->replace_callback, $result);
97
            } else {
98
                $result = preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result);
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @access private
107
     * @param $buffer
108
     * @return mixed|string
109
     */
110
    public function highlight($buffer)
111
    {
112
        $buffer              = '>' . $buffer . '<';
113
        $this->preg_keywords = preg_replace('/[^\w ]/si', '', $this->keywords);
114
        $buffer              = preg_replace_callback("/(\>(((?" . ">[^><]+)|(?R))*)\<)/is", [&$this, 'replace'], $buffer);
115
        $buffer              = substr($buffer, 1, -1);
116
117
        return $buffer;
118
    }
119
}
120