Paragraph::strposa()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 2
b 0
f 1
nc 3
nop 3
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
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
        // Split for each line to exclude.
27 2
        $content = explode("\n", $content);
28 2
        $pContent = '';
29 2
        foreach ($content as $line) {
30 2
            if (!$this->strposa($line, self::$paragraphExcludes)) {
31 2
                $line = sprintf('<p>%s</p>', trim($line));
32
            }
33 2
            $pContent .= $line;
34
        }
35
36 2
        return $pContent;
37
    }
38
39
    /**
40
     * Find the position of the first occurrence of a substring in a string.
41
     * Get only true, if is on the first position (0).
42
     *
43
     * @param string $haystack The string to search in.
44
     * @param array  $needle   An array with strings for search.
45
     * @param int    $offset   If specified, search will start this number of characters counted
46
     *                         from the beginning of the string.
47
     *
48
     * @return bool
49
     */
50 2
    protected function strposa(string $haystack, array $needle, int $offset = 0): bool
51 2
    {
52 2
        foreach ($needle as $query) {
53
            // If we found the string only on position 0.
54 2
            if (strpos($haystack, $query, $offset) === 0) {
55
                return true;
56
            } // stop on first false result
57
        }
58
59 2
        return false;
60
    }
61
}
62