Completed
Push — develop ( c96724...2e3f1f )
by Faiz
02:59
created

AyahCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class AyahCommand extends Command
12
{
13
    private $quran;
14
15
    public function __construct()
16
    {
17
        parent::__construct();
18
        $this->quran = new Quran();
19
    }
20
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('ayah')
25
            ->setDescription('Retrieve ayah')
26
            ->addArgument(
27
                'surah',
28
                InputArgument::REQUIRED,
29
                'Select surah'
30
            )
31
            ->addArgument(
32
                'ayah',
33
                InputArgument::REQUIRED,
34
                'Select ayah. You can mix comma (,) and range (-)'
35
            )
36
            ->addArgument(
37
                'translation',
38
                InputArgument::OPTIONAL,
39
                'Who do you want to greet?'
40
            )
41
            ->addUsage('2 3')
42
            ->addUsage('2 3,5 en')
43
            ->addUsage('2 3,5-6 ar,en')
44
        ;
45
    }
46
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $surah = $input->getArgument('surah');
50
        $ayah = $input->getArgument('ayah');
51
        $translation = $input->getArgument('translation');
52
53
        if ( $translation ) {
54
            $this->quran->translation( $translation );
55
        }
56
57
        $ayah = $this->quran->get($surah . ':' . $ayah);
58
        $output->writeln( $this->parseResult($ayah) );
59
    }
60
61
    private function parseResult($args)
62
    {
63
        // Just a single ayah is return. No need to parse anything.
64
        if (is_string($args)) {
65
            return $args . "\n";
66
        }
67
68
        // Multiple ayah/one surah or multiple surah/one ayah. Not both.
69
        if (is_string(current($args))) {
70
            return $this->buildAyah($args);
71
        }
72
73
        // Both multiple ayah and multiple surah.
74
        $count = 0;
75
        $result = "\n";
76
77
        foreach ($args as $translation => $aya) {
78
79
            $result .= strtoupper($translation) . "\n" . str_repeat('=', strlen($translation) + 2) . "\n\n";
80
            $result .= $this->buildAyah($aya);
81
82
            ++$count;
83
            if ($count < sizeof($args)) {
84
                $result .= "\n\n";
85
            }
86
        }
87
88
        return $result;
89
90
    }
91
92
    private function buildAyah($ayah)
93
    {
94
        $result = "";
95
        foreach ($ayah as $num => $aya) {
96
            $result .= "[ " . strtoupper($num) . " ]\t" . $aya . "\n";
97
        }
98
        return $result;
99
    }
100
}
101