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