Completed
Push — master ( b59592...c78116 )
by Faiz
01:47
created

Quran::setSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
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 57
    public function __construct(array $config = array())
20
    {
21 57
        $this->config = new Config($config);
22
23
        // By default, source is XML
24 57
        $this->source = new XMLRepository();
25 57
    }
26
27 57
    public function setSource(SourceInterface $source)
28
    {
29 57
        $this->source = $source;
30 57
        $this->source->setConfig( $this->config );
31 57
        $this->source->initialize();
32 57
    }
33
34
    /**
35
     * Set translations to be used
36
     *
37
     * @param array $translations
38
     * @return $this
39
     */
40 30
    public function translation($translations)
41
    {
42 30
        if (is_array($translations)) {
43 3
            $this->translations = $translations;
44 3
        } else {
45 24
            $this->translations = explode(',', str_replace(' ', '', $translations));
46
        }
47
48 27
        return $this;
49
    }
50
51 57
    public function get($args)
52
    {
53 57
        $args = explode(':', $args);
54 57
        $result = [];
55
56 57
        if (sizeof($args) <= 1) {
57 6
            throw new AyahNotProvided("Ayah argument was not provided. Give at least one ayah from a surah.");
58
        }
59
60
        // Get text for all translation
61 51
        foreach ($this->translations as $translation) {
62
63
            // Parse ayah arguments into array of ayah
64 51
            $ayah = $this->parseSurah($args[1]);
65
66
            // Check if Surah and Ayah is in correct format
67 51
            if (!is_numeric($args[0]) || sizeof($ayah) === 0) {
68 9
                throw new WrongArgument("Surah / Ayah format was incorrect. Please try again.");
69
            }
70
71 42
            $result[$translation] = $this->source->getAyah($args[0], $ayah, $translation);
72 39
        }
73
74 39
        return $this->minimize($result);
75
    }
76
77 51
    private function parseSurah($surah)
78
    {
79 51
        $result = [];
80
81 51
        foreach (explode(',', $surah) as $comma) {
82
83 51
            $dash = explode('-', $comma);
84
85
            // Skip any invalid ayah
86 51
            if (!is_numeric($dash[0]) || (isset($dash[1]) && !is_numeric($dash[1]))) {
87 9
                continue;
88
            }
89
90
            // Single ayah, just push it into array.
91 45
            if (sizeof($dash) === 1) {
92 39
                array_push($result, intval($dash[0]));
93 39
            } // Range ayah, so we create all ayah in between.
94
            else {
95 18
                for ($i = $dash[0]; $i <= $dash[1]; $i++) {
96 18
                    array_push($result, intval($i));
97 18
                }
98
            }
99 51
        }
100
101 51
        return $result;
102
    }
103
104 24
    private function sortArray(array $arr)
105
    {
106 24
        ksort($arr, SORT_NUMERIC);
107 24
        return $arr;
108
    }
109
110 39
    private function minimize(array $array)
111
    {
112 39
        foreach ($array as $key => $translation) {
113
114
            // If one ayah is requested, we remove it's key
115 39
            if (sizeof($translation) === 1) {
116 15
                $array[$key] = $translation[key($translation)];
117 15
            } // Else we maintain current format, but in sorted form
118
            else {
119 24
                $array[$key] = $this->sortArray($translation);
120
            }
121 39
        }
122
123
        // If one translation is requested, we remove it's key.
124
        // Else just return the current format
125 39
        return (sizeof($array) > 1) ? $array : $array[key($array)];
126
    }
127
128
}
129