Passed
Pull Request — master (#3)
by Christopher
01:29
created

BibleBook::translateAbbreviation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TechWilk\BibleVerseParser;
6
7
use TechWilk\BibleVerseParser\Exception\InvalidBookException;
8
9
class BibleBook
10
{
11
    protected $name;
12
13
    public function __construct(string $name)
14
    {
15
        $this->name = $this->translateAbbreviation($name);
16
    }
17
18
    public function name(): string
19
    {
20
        return $this->name;
21
    }
22
23
    public function abbreviation(): string
24
    {
25
        return $this->name;
26
    }
27
28
    private function translateAbbreviation(string $abbreviation): string
29
    {
30
        $bookAbbreviations = require __DIR__ . '/../data/books.php';
31
        $bookNames = require __DIR__ . '/../data/bookNames.php';
32
33
        $abbreviation = strtolower($abbreviation);
34
        $abbreviation = preg_replace('/[^a-z0-9 ]/', '', $abbreviation);
35
        $abbreviation = trim($abbreviation);
36
37
        if (!array_key_exists($abbreviation, $bookAbbreviations)) {
38
            throw InvalidBookException::invalidBook($abbreviation);
39
        }
40
41
        $bookNumber = $bookAbbreviations[$abbreviation];
42
43
        return $bookNames[$bookNumber];
44
    }
45
}
46