MyTextFilter::nl2br()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Aiur18\Filter;
4
5
use Michelf\MarkdownExtra;
6
7
// // Include essentials
8
// require __DIR__ . "/config.php";
9
// require __DIR__ . "/autoload.php";
10
11
// Include essentials
12
// require __DIR__ . "/../config.php";
13
// require __DIR__ . "/../../vendor/autoload.php";
14
15
/**
16
 * Filter and format text content.
17
 *
18
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
19
 * @SuppressWarnings(PHPMD.UnusedPrivateField)
20
 */
21
class MyTextFilter
22
{
23
    /**
24
     * @var array $filters Supported filters with method names of
25
     *                     their respective handler.
26
     */
27
    private $filters = [
0 ignored issues
show
introduced by
The private property $filters is not used, and could be removed.
Loading history...
28
        "bbcode"    => "bbcode2html",
29
        "link"      => "makeClickable",
30
        "markdown"  => "markdown",
31
        "nl2br"     => "nl2br",
32
    ];
33
34
35
36
    /**
37
     * Call each filter on the text and return the processed text.
38
     *
39
     * @param string $text   The text to filter.
40
     * @param array  $filter Array of filters to use.
41
     *
42
     * @return string with the formatted text.
43
     */
44 1
    public function parse($text, $filter)
45
    {
46 1
        foreach ($filter as $value) {
47 1
            $temp = "";
48
49 1
            if ($value == "markdown") {
50
                $temp = $this->markdown($text);
51
                $text = $temp;
52 1
            } elseif ($value == "bbcode") {
53 1
                $temp = $this->bbcode2html($text);
54 1
                $text = $temp;
55 1
            } elseif ($value == "link") {
56 1
                $temp = $this->makeClickable($text);
57 1
                $text = $temp;
58
            } elseif ($value == "nl2br") {
59
                $temp = $this->nl2br($text);
60 1
                $text = $temp;
61
            }
62
        }
63 1
        return $text;
64
    }
65
66
67
68
    /**
69
     * Helper, BBCode formatting converting to HTML.
70
     *
71
     * @param string $text The text to be converted.
72
     *
73
     * @return string the formatted text.
74
     */
75 2
    public function bbcode2html($text)
76
    {
77
        $search = [
78 2
            '/\[b\](.*?)\[\/b\]/is',
79
            '/\[i\](.*?)\[\/i\]/is',
80
            '/\[u\](.*?)\[\/u\]/is',
81
            '/\[img\](https?.*?)\[\/img\]/is',
82
            '/\[url\](https?.*?)\[\/url\]/is',
83
            '/\[url=(https?.*?)\](.*?)\[\/url\]/is'
84
        ];
85
86
        $replace = [
87 2
            '<strong>$1</strong>',
88
            '<em>$1</em>',
89
            '<u>$1</u>',
90
            '<img src="$1" />',
91
            '<a href="$1">$1</a>',
92
            '<a href="$1">$2</a>'
93
        ];
94
95 2
        return preg_replace($search, $replace, $text);
96
    }
97
98
99
100
    /**
101
     * Make clickable links from URLs in text.
102
     *
103
     * @param string $text The text that should be formatted.
104
     *
105
     * @return string with formatted anchors.
106
     */
107 2
    public function makeClickable($text)
108
    {
109 2
        return preg_replace_callback(
110 2
            '#\b(?<![href|src]=[\'"])https?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#',
111
            function ($matches) {
112 2
                return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";
113 2
            },
114 2
            $text
115
        );
116
    }
117
118
119
120
    /**
121
     * Format text according to Markdown syntax.
122
     *
123
     * @param string $text The text that should be formatted.
124
     *
125
     * @return string as the formatted html text.
126
     */
127 1
    public function markdown($text)
128
    {
129 1
        return MarkdownExtra::defaultTransform($text);
130
    }
131
132
133
134
    /**
135
     * For convenience access to nl2br formatting of text.
136
     *
137
     * @param string $text The text that should be formatted.
138
     *
139
     * @return string the formatted text.
140
     */
141
    public function nl2br($text)
142
    {
143
        $temp = nl2br($text);
144
        return $temp;
145
    }
146
}
147