Passed
Push — master ( 82b0d0...1ac70f )
by Bobby
09:15
created

MarkdownTansformer::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ballen\Linguist\Transformers;
4
5
use Ballen\Linguist\Transformers\TransformerInitTrait;
6
use Ballen\Linguist\Transformers\TransformerInterface;
7
use Ballen\Linguist\Transformers\Transformer;
8
9
/**
10
 * Linguist
11
 *
12
 * Linguist is a PHP library for parsing strings, it can extract and manipulate
13
 *  prefixed words in content ideal for working with @mentions, #topics and
14
 *  even custom action tags!
15
 *
16
 * @author Bobby Allen <[email protected]>
17
 * @license http://www.gnu.org/licenses/gpl-3.0.html
18
 * @link https://github.com/allebb/linguist
19
 * @link http://www.bobbyallen.me
20
 *
21
 */
22
class MarkdownTansformer extends Transformer implements TransformerInterface
23
{
24
25 1
    use TransformerInitTrait;
26
27
    /**
28
     * Transforms the message string.
29
     * @param string $string
30
     * @return void
31
     */
32 2
    private function transform($string)
0 ignored issues
show
Unused Code introduced by
The method transform() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
33
    {
34
35 2
        $string = preg_replace(Transformer::URL_REGEX, "[$0]($0) ", $string);
36
37 2
        foreach (array_keys($this->tags) as $tagtype) {
38 2
            $tagconf = $this->tags[$tagtype];
39 2
            $string = preg_replace_callback('/\s+' . $this->tags[$tagtype]['prefix'] . '(\w+)/', function($matches) use ($tagconf) {
40 2
                return ' ' . trim($this->buildMarkdownLink($matches[1], trim($matches[0]), $tagconf));
41 2
            }, $string);
42
        }
43 2
        $this->formatted = $string;
44 2
    }
45
46
    /**
47
     * Builds the Mardown link replacement for the Markdown transformation.
48
     * @param string $link The URL
49
     * @param string $text The name of the link
50
     * @param array $tag
51
     * @return string
52
     */
53 2
    private function buildMarkdownLink($link, $text, $tag)
54
    {
55 2
        $url = sprintf($tag['url'], $link);
56 2
        return "[{$url}]({$text})";
57
    }
58
}
59