1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Hashid\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Jenssegers\Optimus\Energon; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class OptimusGenerateCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The console command name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'hashid:optimus'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Generate Optimus numbers'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute the console command. |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
1 |
|
public function handle() |
31
|
|
|
{ |
32
|
1 |
|
$this->table( |
33
|
1 |
|
['prime', 'inverse', 'random'], |
34
|
1 |
|
$this->generateOptimusNumbers( |
35
|
1 |
|
$this->getTimes(), |
36
|
1 |
|
(int) $this->option('prime') ?: null |
37
|
1 |
|
) |
38
|
1 |
|
); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get "times" option value. |
43
|
|
|
* |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
1 |
|
protected function getTimes() |
47
|
|
|
{ |
48
|
1 |
|
$times = (int) $this->option('times'); |
49
|
|
|
|
50
|
1 |
|
return max(1, min($times, 100)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Generate Optimus numbers. |
55
|
|
|
* |
56
|
|
|
* @param int $times |
57
|
|
|
* @param int $prime |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
protected function generateOptimusNumbers($times = 1, $prime = null) |
61
|
|
|
{ |
62
|
1 |
|
$result = []; |
63
|
|
|
|
64
|
1 |
|
for ($i = 0; $i < $times; $i++) { |
65
|
1 |
|
$result[] = Energon::generate($prime); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the console command options. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
2 |
|
protected function getOptions() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
2 |
|
['prime', 'p', InputOption::VALUE_OPTIONAL, 'Generate with the given prime'], |
80
|
2 |
|
['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1], |
81
|
2 |
|
]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|