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