1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FaizShukri\Quran; |
4
|
|
|
|
5
|
|
|
use FaizShukri\Quran\Repositories\Source\SourceInterface; |
6
|
|
|
use FaizShukri\Quran\Exceptions\AyahNotProvided; |
7
|
|
|
use FaizShukri\Quran\Exceptions\WrongArgument; |
8
|
|
|
use FaizShukri\Quran\Repositories\Source\XMLRepository; |
9
|
|
|
use FaizShukri\Quran\Supports\Config; |
10
|
|
|
|
11
|
|
|
class Quran |
12
|
|
|
{ |
13
|
|
|
private $config; |
14
|
|
|
|
15
|
|
|
private $translations = ['ar']; |
16
|
|
|
|
17
|
|
|
private $source; |
18
|
|
|
|
19
|
60 |
|
public function __construct(array $config = array()) |
20
|
|
|
{ |
21
|
60 |
|
$this->config = new Config($config); |
22
|
|
|
|
23
|
|
|
// By default, source is XML |
24
|
60 |
|
$this->setSource( new XMLRepository() ); |
25
|
60 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set quran source either XML, Sql |
29
|
|
|
* |
30
|
|
|
* @param \FaizShukri\Quran\Repositories\Source\SourceInterface $source |
31
|
|
|
*/ |
32
|
60 |
|
public function setSource(SourceInterface $source) |
33
|
|
|
{ |
34
|
60 |
|
$this->source = $source; |
35
|
60 |
|
$this->source->setConfig( $this->config ); |
36
|
60 |
|
$this->source->initialize(); |
37
|
60 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set translations to be used |
41
|
|
|
* |
42
|
|
|
* @param array $translations |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
27 |
|
public function translation($translations) |
47
|
|
|
{ |
48
|
27 |
|
if (is_array($translations)) { |
49
|
3 |
|
$this->translations = $translations; |
50
|
3 |
|
} else { |
51
|
24 |
|
$this->translations = explode(',', str_replace(' ', '', $translations)); |
52
|
3 |
|
} |
53
|
|
|
|
54
|
27 |
|
return $this; |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get ayah. |
59
|
|
|
* |
60
|
|
|
* @param string $args String of supported format of ayah |
61
|
|
|
* |
62
|
|
|
* @return string|array Ayah |
63
|
|
|
* |
64
|
|
|
* @throws AyahNotProvided |
65
|
|
|
* @throws WrongArgument |
66
|
|
|
*/ |
67
|
57 |
|
public function get($args) |
68
|
|
|
{ |
69
|
57 |
|
$args = explode(':', $args); |
70
|
57 |
|
$result = []; |
71
|
|
|
|
72
|
57 |
|
if (sizeof($args) <= 1) { |
73
|
6 |
|
throw new AyahNotProvided("Ayah argument was not provided. Give at least one ayah from a surah."); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Get text for all translation |
77
|
51 |
|
foreach ($this->translations as $translation) { |
78
|
|
|
|
79
|
|
|
// Parse ayah arguments into array of ayah |
80
|
51 |
|
$ayah = $this->parseSurah($args[1]); |
81
|
|
|
|
82
|
|
|
// Check if Surah and Ayah is in correct format |
83
|
51 |
|
if (!is_numeric($args[0]) || sizeof($ayah) === 0) { |
84
|
9 |
|
throw new WrongArgument("Surah / Ayah format was incorrect. Please try again."); |
85
|
|
|
} |
86
|
|
|
|
87
|
42 |
|
$result[$translation] = $this->source->ayah($args[0], $ayah, $translation); |
88
|
39 |
|
} |
89
|
|
|
|
90
|
39 |
|
return $this->minimize($result); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get array of chapters information |
95
|
|
|
* |
96
|
|
|
* @return array Array of object for each chapter |
97
|
|
|
*/ |
98
|
3 |
|
public function chapters() |
99
|
|
|
{ |
100
|
3 |
|
return $this->source->chapters(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Parse the ayah requested of a certain surah. The format of ayah will |
105
|
|
|
* be translated into an array or ayah. |
106
|
|
|
* |
107
|
|
|
* @param string $surah |
108
|
|
|
* |
109
|
|
|
* @return array Array of ayah |
110
|
|
|
*/ |
111
|
51 |
|
private function parseSurah($surah) |
112
|
|
|
{ |
113
|
51 |
|
$result = []; |
114
|
|
|
|
115
|
51 |
|
foreach (explode(',', $surah) as $comma) { |
116
|
|
|
|
117
|
51 |
|
$dash = explode('-', $comma); |
118
|
|
|
|
119
|
|
|
// Skip any invalid ayah |
120
|
51 |
|
if (!is_numeric($dash[0]) || (isset($dash[1]) && !is_numeric($dash[1]))) { |
121
|
9 |
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Single ayah, just push it into array. |
125
|
45 |
|
if (sizeof($dash) === 1) { |
126
|
39 |
|
array_push($result, intval($dash[0])); |
127
|
39 |
|
} // Range ayah, so we create all ayah in between. |
128
|
|
|
else { |
129
|
18 |
|
for ($i = $dash[0]; $i <= $dash[1]; $i++) { |
130
|
18 |
|
array_push($result, intval($i)); |
131
|
18 |
|
} |
132
|
|
|
} |
133
|
51 |
|
} |
134
|
|
|
|
135
|
51 |
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sort array in ascending by it's key. |
140
|
|
|
* |
141
|
|
|
* @param array $arr |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
24 |
|
private function sortArray(array $arr) |
146
|
|
|
{ |
147
|
24 |
|
ksort($arr, SORT_NUMERIC); |
148
|
24 |
|
return $arr; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Reduce the array level by remove unnecessary parent. |
153
|
|
|
* |
154
|
|
|
* @param array $array |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
39 |
|
private function minimize(array $array) |
159
|
|
|
{ |
160
|
39 |
|
foreach ($array as $key => $translation) { |
161
|
|
|
|
162
|
|
|
// If one ayah is requested, we remove it's key |
163
|
39 |
|
if (sizeof($translation) === 1) { |
164
|
15 |
|
$array[$key] = $translation[key($translation)]; |
165
|
15 |
|
} // Else we maintain current format, but in sorted form |
166
|
|
|
else { |
167
|
24 |
|
$array[$key] = $this->sortArray($translation); |
168
|
|
|
} |
169
|
39 |
|
} |
170
|
|
|
|
171
|
|
|
// If one translation is requested, we remove it's key. |
172
|
|
|
// Else just return the current format |
173
|
39 |
|
return (sizeof($array) > 1) ? $array : $array[key($array)]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|