1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Hashid\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
|
8
|
|
|
class AlphabetGenerateCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The console command name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'hashid:alphabet'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Generate a random alphabet for Hashid encoding'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Execute the console command. |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
1 |
|
public function handle() |
30
|
|
|
{ |
31
|
1 |
|
for ($i = 0; $i < $this->getTimes(); $i++) { |
32
|
1 |
|
$this->comment( |
33
|
1 |
|
$this->generateRandomAlphabet($this->option('characters')) |
34
|
|
|
); |
35
|
|
|
} |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get "times" option value. |
40
|
|
|
* |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
1 |
|
protected function getTimes() |
44
|
|
|
{ |
45
|
1 |
|
$times = (int) $this->option('times'); |
46
|
|
|
|
47
|
1 |
|
return max(1, min($times, 3)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generate random alphabet. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
protected function generateRandomAlphabet($characters) |
56
|
|
|
{ |
57
|
1 |
|
return str_shuffle(count_chars($characters, 3)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the console command options. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
1 |
|
protected function getOptions() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
1 |
|
['characters', 'c', InputOption::VALUE_OPTIONAL, 'Use custom characters', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'], |
69
|
|
|
['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|