BiblePassageParser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 57 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TechWilk\BibleVerseParser;
6
7
use TechWilk\BibleVerseParser\Exception\UnableToParseException;
8
9
class BiblePassageParser
10
{
11
    public function parse(string $versesString): array
12
    {
13
        $sections = explode('&', $versesString);
14
15
        $verses = [];
16
        $book = '';
17
        $chapter = '';
18
        foreach ($sections as $section) {
19
            $innerSections = explode(',', $section);
20
            foreach ($innerSections as $innerSection) {
21
                $result = preg_match(
22
                    '/^\\s*((?:[0-9]+\\s+)?[^0-9]+)?([0-9]+)?(?:\\s*[\\. \\:v]\\s*([0-9\\-]+(?:end)?))?\\s*$/',
23
                    $innerSection,
24
                    $matches
25
                );
26
                if (!$result) {
27
                    throw new UnableToParseException('Unable to parse verse');
28
                }
29
30
                if (
31
                    !array_key_exists(1, $matches)
32
                    && !array_key_exists(2, $matches)
33
                    && !array_key_exists(3, $matches)
34
                ) {
35
                    throw new UnableToParseException('Unable to parse verse');
36
                }
37
38
                $matches[1] = trim($matches[1] ?? '');
39
                if ('' !== $matches[1]) {
40
                    $book = $matches[1];
41
                    $chapter = '';
42
                }
43
44
                $matches[2] = trim($matches[2] ?? '');
45
                if ('' !== $matches[2]) {
46
                    $chapter = $matches[2];
47
                }
48
49
                $verse = '';
50
                $matches[3] = trim($matches[3] ?? '');
51
                if ('' !== $matches[3]) {
52
                    if ('' === $chapter) {
53
                        $chapter = $matches[3];
54
                    } else {
55
                        $verse = $matches[3];
56
                    }
57
                }
58
59
                $verses[] = new BiblePassage(
60
                    $book,
61
                    $chapter,
62
                    $verse
63
                );
64
            }
65
        }
66
67
        return $verses;
68
    }
69
}
70