1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Djunehor\Logos; |
4
|
|
|
|
5
|
|
|
class Bible |
6
|
|
|
{ |
7
|
|
|
private $lang; |
8
|
|
|
private $version; |
9
|
|
|
private $bible; |
10
|
|
|
private $book; |
11
|
|
|
private $chapter; |
12
|
|
|
private $verse; |
13
|
|
|
|
14
|
11 |
|
public function __construct($lang = 'en', $version = 'kjv') |
15
|
|
|
{ |
16
|
11 |
|
$this->lang($lang); |
17
|
9 |
|
$this->version($version); |
18
|
|
|
|
19
|
8 |
|
$this->lang = $lang; |
20
|
8 |
|
$this->version = $version; |
21
|
8 |
|
} |
22
|
|
|
|
23
|
11 |
|
public function lang($lang) |
24
|
|
|
{ |
25
|
11 |
|
if (! file_exists(__DIR__.'/../bibles/'.$lang)) { |
26
|
1 |
|
throw new \Exception("The language [$lang] doesn't exist"); |
27
|
|
|
} |
28
|
10 |
View Code Duplication |
if (! file_exists(__DIR__.'/../bibles/'.$lang.'/Books.json')) { |
|
|
|
|
29
|
1 |
|
throw new \Exception("Index of available books not found for [$lang]"); |
30
|
|
|
} |
31
|
9 |
|
$this->lang = $lang; |
32
|
9 |
|
} |
33
|
|
|
|
34
|
9 |
|
public function version($version) |
35
|
|
|
{ |
36
|
9 |
View Code Duplication |
if (! file_exists(__DIR__.'/../bibles/'.$this->lang.'/'.$version)) { |
|
|
|
|
37
|
1 |
|
throw new \Exception("The bible version [$version] for language [$this->lang] doesn't exist"); |
38
|
|
|
} |
39
|
8 |
|
$this->version = $version; |
40
|
8 |
|
} |
41
|
|
|
|
42
|
6 |
|
public function book($bookName) |
43
|
|
|
{ |
44
|
6 |
|
$this->bible = json_decode(file_get_contents(__DIR__."/../bibles/$this->lang/Books.json"), true); |
45
|
6 |
|
$this->book = ucfirst($bookName); |
46
|
|
|
|
47
|
6 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
public function chapter(int $chapterNumber) |
51
|
|
|
{ |
52
|
5 |
|
$this->chapter = $chapterNumber - 1; |
53
|
|
|
|
54
|
5 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function verse(int $verseNumber) |
58
|
|
|
{ |
59
|
4 |
|
$this->verse = $verseNumber - 1; |
60
|
|
|
|
61
|
4 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
public function getBook() |
65
|
|
|
{ |
66
|
6 |
|
$bookPath = __DIR__."/../bibles/$this->lang/$this->version/$this->book.json"; |
67
|
6 |
|
if (file_exists($bookPath)) { |
68
|
6 |
|
return json_decode(file_get_contents($bookPath), true); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
public function getChapter() |
73
|
|
|
{ |
74
|
5 |
|
if ($book = $this->getBook()) { |
75
|
5 |
|
return $book['chapters'][$this->chapter] ?? null; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
public function getVerse() |
80
|
|
|
{ |
81
|
4 |
|
if ($chapter = $this->getChapter()) { |
82
|
4 |
|
if ($verse = $chapter['verses'][$this->verse]) { |
83
|
4 |
|
return $verse['text'] ?? null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
public function get($string) |
89
|
|
|
{ |
90
|
3 |
|
$explodeSpace = explode(' ', $string); |
91
|
3 |
|
$this->book($explodeSpace[0]); |
92
|
3 |
|
$explodeColumn = explode(':', $explodeSpace[1]); |
93
|
3 |
|
$this->chapter($explodeColumn[0]); |
94
|
3 |
|
$this->verse($explodeColumn[1]); |
95
|
|
|
|
96
|
3 |
|
return $this->getVerse(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.