Image   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rule() 0 4 1
A render() 0 10 2
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace Bueltge\Marksimple\Rule;
5
6
class Image extends AbstractRegexRule
7
{
8
9
    /**
10
     * Get the regex rule to identify the content for the callback.
11
     * Parse image with optional alt string, like ![String](path/to/image.png "Alt string")
12
     *
13
     * @return string
14
     */
15 8
    public function rule(): string
16 8
    {
17
        // (?!`) for don't match with ` on end of line for code lines.
18 8
        return '#!\[[^\]]*\]\((.*?)(?=\"|\))(\".*\")?\)(?!`)#';
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 5
    public function render(array $content): string
25 5
    {
26 5
        $url = filter_var($content[1], FILTER_SANITIZE_URL);
27 5
        $alt = '';
28 5
        if (isset($content[2])) {
29 1
            $content[2] = str_replace('"', '', $content[2]);
30 1
            $alt = ' alt="' . filter_var($content[2], FILTER_SANITIZE_STRING) . '"';
31
        }
32
33 5
        return sprintf('<img src="%s"%s />', $url, $alt);
34
    }
35
}
36