Completed
Push — master ( 26b154...7bc5ce )
by Faiz
01:44
created

Quran::chapter()   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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 63
    public function __construct(array $config = array())
20
    {
21 63
        $this->config = new Config($config);
22
23
        // By default, source is XML
24 63
        $this->setSource( new XMLRepository() );
25 63
    }
26
27
    /**
28
     * Set quran source either XML, Sql
29
     *
30
     * @param \FaizShukri\Quran\Repositories\Source\SourceInterface $source
31
     */
32 63
    public function setSource(SourceInterface $source)
33
    {
34 63
        $this->source = $source;
35 63
        $this->source->setConfig( $this->config );
36 63
        $this->source->initialize();
37 63
    }
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
        }
53
54 27
        return $this;
55
    }
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
     * Get single chapter information
105
     *
106
     * @return object Chapter information of a chapter
107
     */
108 3
    public function chapter($chapter)
109
    {
110 3
        return $this->source->chapter($chapter);
111
    }
112
113
    /**
114
     * Parse the ayah requested of a certain surah. The format of ayah will
115
     * be translated into an array or ayah.
116
     *
117
     * @param string $surah
118
     *
119
     * @return array Array of ayah
120
     */
121 51
    private function parseSurah($surah)
122
    {
123 51
        $result = [];
124
125 51
        foreach (explode(',', $surah) as $comma) {
126
127 51
            $dash = explode('-', $comma);
128
129
            // Skip any invalid ayah
130 51
            if (!is_numeric($dash[0]) || (isset($dash[1]) && !is_numeric($dash[1]))) {
131 9
                continue;
132
            }
133
134
            // Single ayah, just push it into array.
135 45
            if (sizeof($dash) === 1) {
136 39
                array_push($result, intval($dash[0]));
137 39
            } // Range ayah, so we create all ayah in between.
138
            else {
139 18
                for ($i = $dash[0]; $i <= $dash[1]; $i++) {
140 18
                    array_push($result, intval($i));
141 18
                }
142
            }
143 51
        }
144
145 51
        return $result;
146
    }
147
148
    /**
149
     * Sort array in ascending by it's key.
150
     *
151
     * @param array $arr
152
     *
153
     * @return array
154
     */
155 24
    private function sortArray(array $arr)
156
    {
157 24
        ksort($arr, SORT_NUMERIC);
158 24
        return $arr;
159
    }
160
161
    /**
162
     * Reduce the array level by remove unnecessary parent.
163
     *
164
     * @param array $array
165
     *
166
     * @return array
167
     */
168 39
    private function minimize(array $array)
169
    {
170 39
        foreach ($array as $key => $translation) {
171
172
            // If one ayah is requested, we remove it's key
173 39
            if (sizeof($translation) === 1) {
174 15
                $array[$key] = $translation[key($translation)];
175 15
            } // Else we maintain current format, but in sorted form
176
            else {
177 24
                $array[$key] = $this->sortArray($translation);
178
            }
179 39
        }
180
181
        // If one translation is requested, we remove it's key.
182
        // Else just return the current format
183 39
        return (sizeof($array) > 1) ? $array : $array[key($array)];
184
    }
185
186
}
187