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

BibleBook   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A name() 0 3 1
A translateAbbreviation() 0 16 2
A abbreviation() 0 3 1
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