Completed
Push — master ( b28e72...589b7f )
by Faiz
01:52
created

SurahCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace FaizShukri\Quran\Commands;
4
5
use FaizShukri\Quran\Quran;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Helper\TableCell;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Helper\Table;
12
13
class SurahCommand extends Command
14
{
15
    private $quran;
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->quran = new Quran();
21
    }
22
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('surah')
27
            ->setDescription('Retrieve surah')
28
            ->addArgument(
29
                'surah',
30
                InputArgument::OPTIONAL,
31
                'Specify surah'
32
            )
33
            ->addArgument(
34
                'ayah',
35
                InputArgument::OPTIONAL,
36
                'Specify ayah'
37
            )
38
            ->addArgument(
39
                'translation',
40
                InputArgument::OPTIONAL,
41
                'Specify ayah'
42
            )
43
            ->addUsage('2')
44
            ->addUsage('2 3')
45
            ->addUsage('2 3,5 en')
46
            ->addUsage('2 3,5-6 ar,en')
47
        ;
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $surah = $input->getArgument('surah');
53
        $ayah = $input->getArgument('ayah');
54
        $translation = $input->getArgument('translation');
55
56
        if($ayah) {
57
            if($translation){
58
                $this->quran->translation( $translation );
59
            }
60
61
            $ayah = $this->quran->get($surah . ':' . $ayah);
62
            $output->writeln( $this->parseResult($ayah) );
63
64
        } else if($surah) {
65
            $this->chapter($output, $surah);
66
        } else {
67
            $this->chapters($output);
68
        }
69
    }
70
71
    private function chapter($output, $verse)
72
    {
73
        $surah = $this->quran->getSource()->chapter($verse);
74
75
        $table = new Table($output);
76
        $table
77
            ->setHeaders([
78
                [new TableCell('Surah ' . $surah->tname, array('colspan' => 2))]
79
            ])
80
            ->setRows([
81
                [ "Index",  $surah->index],
82
                [ "Name",  $surah->tname],
83
                [ "Name (ar)",  $surah->name],
84
                [ "Meaning",  $surah->ename],
85
                [ "No. Ayah",  $surah->ayas],
86
                [ "Start",  $surah->start],
87
                [ "Type",  $surah->type],
88
                [ "Order",  $surah->order],
89
                [ "Rukus",  $surah->rukus],
90
            ])
91
            ->setStyle('borderless')
92
        ;
93
        $table->render();
94
    }
95
96
    private function chapters($output)
97
    {
98
        $surah = $this->quran->getSource()->chapters();
99
        $surah = array_map(function($sura){ return "$sura->index. $sura->tname"; }, $surah);
100
        $surah = $this->array_chunk_vertical($surah, 4);
101
102
        $table = new Table($output);
103
        $table
104
            ->setHeaders([
105
                [new TableCell('All surah', array('colspan' => 4))]
106
            ])
107
            ->setRows($surah)
108
        ;
109
        $table->render();
110
    }
111
112
    private function array_chunk_vertical($data, $columns) {
113
        $n = count($data) ;
114
        $per_column = floor($n / $columns) ;
115
        $rest = $n % $columns ;
116
117
        // The map
118
        $per_columns = array( ) ;
119
        for ( $i = 0 ; $i < $columns ; $i++ ) {
120
            $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
121
        }
122
123
        $tabular = array( ) ;
124
        foreach ( $per_columns as $rows ) {
125
            for ( $i = 0 ; $i < $rows ; $i++ ) {
126
                $tabular[$i][ ] = array_shift($data) ;
127
            }
128
        }
129
130
        return $tabular ;
131
    }
132
133
    private function parseResult($args)
134
    {
135
        // Just a single ayah is return. No need to parse anything.
136
        if (is_string($args)) {
137
            return $args . "\n";
138
        }
139
140
        // Multiple ayah/one surah or multiple surah/one ayah. Not both.
141
        if (is_string(current($args))) {
142
            return $this->buildAyah($args);
143
        }
144
145
        // Both multiple ayah and multiple surah.
146
        $count = 0;
147
        $result = "\n";
148
149
        foreach ($args as $translation => $aya) {
150
151
            $result .= strtoupper($translation) . "\n" . str_repeat('=', strlen($translation) + 2) . "\n\n";
152
            $result .= $this->buildAyah($aya);
153
154
            ++$count;
155
            if ($count < sizeof($args)) {
156
                $result .= "\n\n";
157
            }
158
        }
159
160
        return $result;
161
162
    }
163
164
    private function buildAyah($ayah)
165
    {
166
        $result = "";
167
        foreach ($ayah as $num => $aya) {
168
            $result .= "[ " . strtoupper($num) . " ]\t" . $aya . "\n";
169
        }
170
        return $result;
171
    }
172
}
173