Completed
Push — master ( ef3e6f...11a830 )
by Frank
02:32
created

Paragraph   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 56
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 14 3
A strposa() 0 11 3
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace Bueltge\Marksimple\Rule;
5
6
class Paragraph implements ElementRuleInterface
7
{
8
9
    /**
10
     * Store the strings, there we exclude on set paragraph.
11
     *
12
     * @var array
13
     */
14
    protected static $paragraphExcludes = [
15
        '<', // HTML.
16
        '    ', // Code.
17
        "\t", // Tab.
18
        '---', // hr
19
    ];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function parse(string $content): string
25 2
    {
26
27
        // Split for each line to exclude.
28 2
        $content = explode("\n", $content);
29 2
        $pContent = '';
30 2
        foreach ($content as $line) {
31 2
            if (!$this->strposa($line, self::$paragraphExcludes)) {
32 2
                $line = sprintf('<p>%s</p>', trim($line));
33
            }
34 2
            $pContent .= $line;
35
        }
36
37 2
        return $pContent;
38
    }
39
40
    /**
41
     * Find the position of the first occurrence of a substring in a string.
42
     * Get only true, if is on the first position (0).
43
     *
44
     * @param string $haystack The string to search in.
45
     * @param array  $needle   An array with strings for search.
46
     * @param int    $offset   If specified, search will start this number of characters counted
47
     *                         from the beginning of the string.
48
     *
49
     * @return bool
50
     */
51 2
    protected function strposa(string $haystack, array $needle, int $offset = 0): bool
52 2
    {
53
54 2
        foreach ($needle as $query) {
55
            // If we found the string only on position 0.
56 2
            if (strpos($haystack, $query, $offset) === 0) {
57
                return true;
58
            } // stop on first false result
59
        }
60
61 2
        return false;
62
    }
63
}
64