Completed
Push — master ( 244f91...2e9768 )
by Faiz
01:41
created

QuranCommand::buildAyah()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
#!/usr/bin/php
2
<?php
3
4
require_once(realpath('vendor/autoload.php'));
5
6
use FaizShukri\Quran\Quran;
7
8
class QuranCommand
9
{
10
    public function execute(array $args)
11
    {
12
        $quran = new Quran;
13
14
        try {
15
            if(isset($args[2])) $quran->translation($args[2]);
16
            $ayah = $quran->get($args[1]);
17
18
            return $this->parseResult($ayah) . "\n";
19
20
        } catch(Exception $e) {
21
            return "Error: " . $e->getMessage() . "\n";
22
        }
23
    }
24
25
    private function parseResult($args)
26
    {
27
        // Just a single ayah is return. No need to parse anything.
28
        if(is_string($args)) return $args . "\n";
29
30
        // Multiple ayah/one surah or multiple surah/one ayah. Not both.
31
        if(is_string(current($args))) return $this->buildAyah($args);
32
33
        // Both multiple ayah and multiple surah.
34
        $count = 0;
35
        $result = "\n";
36
37
        foreach($args as $translation => $aya) {
38
39
            $result .= strtoupper($translation) . "\n" . str_repeat('=', strlen($translation) + 2) . "\n\n";
40
            $result .= $this->buildAyah($aya);
41
42
            ++$count;
43
            if( $count < sizeof($args) ) $result .= "\n\n";
44
        }
45
46
        return $result;
47
48
    }
49
50
    private function buildAyah($ayah)
51
    {
52
        $result = "";
53
        foreach($ayah as $num => $aya) {
54
            $result .= "[ " . strtoupper($num) . " ]\t" . $aya . "\n";
55
        }
56
        return $result;
57
    }
58
59
}
60
61
$command = new QuranCommand;
62
echo $command->execute($argv);
63