Completed
Push — master ( cf1346...b28e72 )
by Faiz
01:57
created

SurahCommand::array_chunk_vertical()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 9
nop 2
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
                'Select surah'
32
            )
33
        ;
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $surah = $input->getArgument('surah');
39
40
        if($surah){
41
            $this->chapter($output, $surah);
42
        } else {
43
            $this->chapters($output);
44
        }
45
    }
46
47
    private function chapter($output, $verse)
48
    {
49
        $surah = $this->quran->getSource()->chapter($verse);
50
51
        $table = new Table($output);
52
        $table
53
            ->setHeaders([
54
                [new TableCell('Surah ' . $surah->tname, array('colspan' => 2))]
55
            ])
56
            ->setRows([
57
                [ "Index",  $surah->index],
58
                [ "Name",  $surah->tname],
59
                [ "Name (ar)",  $surah->name],
60
                [ "Meaning",  $surah->ename],
61
                [ "No. Ayah",  $surah->ayas],
62
                [ "Start",  $surah->start],
63
                [ "Type",  $surah->type],
64
                [ "Order",  $surah->order],
65
                [ "Rukus",  $surah->rukus],
66
            ])
67
            ->setStyle('borderless')
68
        ;
69
        $table->render();
70
    }
71
72
    private function chapters($output)
73
    {
74
        $surah = $this->quran->getSource()->chapters();
75
        $surah = array_map(function($sura){ return "$sura->index. $sura->tname"; }, $surah);
76
        $surah = $this->array_chunk_vertical($surah, 4);
77
78
        $table = new Table($output);
79
        $table
80
            ->setHeaders([
81
                [new TableCell('All surah', array('colspan' => 4))]
82
            ])
83
            ->setRows($surah)
84
        ;
85
        $table->render();
86
    }
87
88
    private function array_chunk_vertical($data, $columns) {
89
        $n = count($data) ;
90
        $per_column = floor($n / $columns) ;
91
        $rest = $n % $columns ;
92
93
        // The map
94
        $per_columns = array( ) ;
95
        for ( $i = 0 ; $i < $columns ; $i++ ) {
96
            $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
97
        }
98
99
        $tabular = array( ) ;
100
        foreach ( $per_columns as $rows ) {
101
            for ( $i = 0 ; $i < $rows ; $i++ ) {
102
                $tabular[$i][ ] = array_shift($data) ;
103
            }
104
        }
105
106
        return $tabular ;
107
    }
108
}
109