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