1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FaizShukri\Quran\Commands; |
4
|
|
|
|
5
|
|
|
use FaizShukri\Quran\Quran; |
6
|
|
|
use Hoa\Console\Parser; |
7
|
|
|
use Hoa\Console\GetOption; |
8
|
|
|
|
9
|
|
|
class QuranCommand |
10
|
|
|
{ |
11
|
|
|
private $uri; |
12
|
|
|
|
13
|
|
|
private $options; |
14
|
|
|
|
15
|
|
|
private $parser; |
16
|
|
|
|
17
|
|
|
public function __construct($uri) |
18
|
|
|
{ |
19
|
|
|
$this->uri = $uri; |
20
|
|
|
$this->parser = new Parser(); |
21
|
|
|
$this->parser->parse($this->uri); |
22
|
|
|
$this->options = new GetOption( |
23
|
|
|
[ |
24
|
|
|
['help', GetOption::NO_ARGUMENT, 'h'], |
25
|
|
|
['version', GetOption::NO_ARGUMENT, 'v'] |
26
|
|
|
], |
27
|
|
|
$this->parser |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function execute() |
32
|
|
|
{ |
33
|
|
|
$this->processFlag(); |
34
|
|
|
|
35
|
|
|
if ($this->options->isPipetteEmpty()) { |
36
|
|
|
$this->processAyah(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function processFlag() |
41
|
|
|
{ |
42
|
|
|
while (false !== $c = $this->options->getOption($v)) { |
43
|
|
|
switch ($c) { |
44
|
|
|
case '__ambiguous': |
45
|
|
|
echo "Option `" . $v['option'] . "` does not exists."; |
46
|
|
|
|
47
|
|
|
if (sizeof($v['solutions']) > 0) { |
48
|
|
|
echo "Do you mean one of below?\n"; |
49
|
|
|
echo implode("\n", array_map(function ($opt) { |
50
|
|
|
return " - $opt"; |
51
|
|
|
}, $v['solutions'])); |
52
|
|
|
} |
53
|
|
|
break; |
54
|
|
|
|
55
|
|
|
case 'h': |
56
|
|
|
echo $this->usage(); |
57
|
|
|
break; |
58
|
|
|
case 'v': |
59
|
|
|
echo $this->version(); |
60
|
|
|
} |
61
|
|
|
echo "\n\n"; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function processAyah() |
66
|
|
|
{ |
67
|
|
|
$this->parser->listInputs($ayah, $translation); |
68
|
|
|
$quran = new Quran(); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
if ( $translation ) { |
|
|
|
|
72
|
|
|
$quran->translation( $translation ); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$ayah = $quran->get($ayah); |
76
|
|
|
echo $this->parseResult($ayah) . "\n"; |
77
|
|
|
|
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
return "Error: " . $e->getMessage() . "\n\n"; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function usage() |
84
|
|
|
{ |
85
|
|
|
return |
86
|
|
|
"Usage:" . "\n" . |
87
|
|
|
" quran <surah> <ayah> [<translation>='ar'] <options>" . "\n\n" . |
88
|
|
|
|
89
|
|
|
"Example: " . "\n" . |
90
|
|
|
" quran 3:6 (Surah 3, ayah 6)". "\n". |
91
|
|
|
" quran 3:2,4-6 ar,en (Surah 3, ayah 2,4,5,6, in arabic and english)". "\n\n" . |
92
|
|
|
|
93
|
|
|
"Options:" . "\n" . |
94
|
|
|
" -h, --help : This help" . "\n" . |
95
|
|
|
" -v, --version : Show version"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
private function parseResult($args) |
101
|
|
|
{ |
102
|
|
|
// Just a single ayah is return. No need to parse anything. |
103
|
|
|
if (is_string($args)) { |
104
|
|
|
return $args . "\n"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Multiple ayah/one surah or multiple surah/one ayah. Not both. |
108
|
|
|
if (is_string(current($args))) { |
109
|
|
|
return $this->buildAyah($args); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Both multiple ayah and multiple surah. |
113
|
|
|
$count = 0; |
114
|
|
|
$result = "\n"; |
115
|
|
|
|
116
|
|
|
foreach ($args as $translation => $aya) { |
117
|
|
|
|
118
|
|
|
$result .= strtoupper($translation) . "\n" . str_repeat('=', strlen($translation) + 2) . "\n\n"; |
119
|
|
|
$result .= $this->buildAyah($aya); |
120
|
|
|
|
121
|
|
|
++$count; |
122
|
|
|
if ($count < sizeof($args)) { |
123
|
|
|
$result .= "\n\n"; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function buildAyah($ayah) |
132
|
|
|
{ |
133
|
|
|
$result = ""; |
134
|
|
|
foreach ($ayah as $num => $aya) { |
135
|
|
|
$result .= "[ " . strtoupper($num) . " ]\t" . $aya . "\n"; |
136
|
|
|
} |
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function version() |
141
|
|
|
{ |
142
|
|
|
$string = file_get_contents(realpath( __DIR__ . '/../../composer.json' )); |
143
|
|
|
$json_a = json_decode($string, true); |
144
|
|
|
|
145
|
|
|
return 'Quran-Cli ' . $json_a['version'] . ' by Faiz Shukri'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: