Completed
Push — master ( 6ab195...1e296c )
by Faiz
02:51
created

Quran::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace FaizShukri\Quran;
4
5
use FaizShukri\Quran\Repositories\Source\SourceInterface;
6
use FaizShukri\Quran\Repositories\Source\XMLRepository;
7
use FaizShukri\Quran\Exceptions\AyahNotProvided;
8
use FaizShukri\Quran\Exceptions\WrongArgument;
9
use FaizShukri\Quran\Exceptions\ExceedLimit;
10
use FaizShukri\Quran\Supports\Config;
11
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())
28
    {
29 78
        $this->config = new Config($config);
30
31
        // By default, source is XML
32 78
        $this->setSource(new XMLRepository());
33 78
    }
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)
41
    {
42 78
        $this->source = $source;
43 78
        $this->source->setConfig($this->config);
44 78
        $this->source->initialize();
45 78
    }
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)
125
    {
126 18
        return $this->source->surah($surah);
127
    }
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)
171
    {
172 30
        ksort($arr, SORT_NUMERIC);
173
174 30
        return $arr;
175
    }
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()
208
    {
209 3
        return static::VERSION;
210
    }
211
}
212