Completed
Push — master ( 2da7a8...7e6b77 )
by Faiz
01:55
created

Quran::version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
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.3.11';
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
    public function getSource()
53
    {
54
        return $this->source;
55
    }
56
57
    /**
58
     * Set translations to be used.
59
     *
60
     * @param array|string $translations
61
     *
62
     * @return self
63
     */
64 30
    public function translation($translations)
65
    {
66 30
        if (is_string($translations)) {
67 27
            $translations = explode(',', str_replace(' ', '', $translations));
68 27
        }
69
70 30
        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 27
        $this->translations = $translations;
75
76 27
        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 66
    public function get($args)
90
    {
91 66
        $args = explode(':', $args);
92 66
        $result = [];
93
94 66
        if (sizeof($args) <= 1) {
95 6
            throw new AyahNotProvided();
96
        }
97
98
        // Parse ayah arguments into array of ayah
99 60
        $ayah = $this->parseSurah($args[1]);
100
101 60
        if (sizeof($ayah) > $this->config->get('limit.ayah')) {
102 3
            throw new ExceedLimit('Too much ayah provided. Your limit is '.$this->config->get('limit.ayah').' only.');
103
        }
104
105
        // Get text for all translation
106 57
        foreach ($this->translations as $translation) {
107
108
            // Check if Surah and Ayah is in correct format
109 57
            if (!is_numeric($args[0]) || sizeof($ayah) === 0) {
110 9
                throw new WrongArgument();
111
            }
112
113 48
            $result[$translation] = $this->source->ayah($args[0], $ayah, $translation);
114 39
        }
115
116 39
        return $this->minimize($result);
117
    }
118
119
    /**
120
     * Get surah information.
121
     *
122
     * @return object Chapter information of a chapter
123
     */
124 6
    public function surah($surah = null)
125
    {
126 6
        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 60
    private function parseSurah($surah)
138
    {
139 60
        $result = [];
140
141 60
        foreach (explode(',', $surah) as $comma) {
142 60
            $dash = explode('-', $comma);
143
144
            // Skip any invalid ayah
145 60
            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 54
            if (sizeof($dash) === 1) {
151 45
                array_push($result, intval($dash[0]));
152 45
            } // Range ayah, so we create all ayah in between.
153
            else {
154 21
                for ($i = $dash[0]; $i <= $dash[1]; ++$i) {
155 21
                    array_push($result, intval($i));
156 21
                }
157
            }
158 60
        }
159
160 60
        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 24
    private function sortArray(array $arr)
171
    {
172 24
        ksort($arr, SORT_NUMERIC);
173
174 24
        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 39
    private function minimize(array $array)
185
    {
186 39
        foreach ($array as $key => $translation) {
187
188
            // If one ayah is requested, we remove it's key
189 39
            if (sizeof($translation) === 1) {
190 15
                $array[$key] = $translation[key($translation)];
191 15
            } // Else we maintain current format, but in sorted form
192
            else {
193 24
                $array[$key] = $this->sortArray($translation);
194
            }
195 39
        }
196
197
        // If one translation is requested, we remove it's key.
198
        // Else just return the current format
199 39
        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