|
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
|
4 |
|
public function handle() |
|
31
|
|
|
{ |
|
32
|
4 |
|
$numbers = $this->generateOptimusNumbers( |
|
33
|
4 |
|
$this->getTimes(), |
|
34
|
4 |
|
(int) $this->option('prime') |
|
35
|
2 |
|
); |
|
36
|
|
|
|
|
37
|
4 |
|
$this->table(['prime', 'inverse', 'random'], $numbers); |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get "times" option value. |
|
42
|
|
|
* |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
4 |
|
protected function getTimes() |
|
46
|
|
|
{ |
|
47
|
4 |
|
return max(1, min(100, (int) $this->option('times'))); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Generate Optimus numbers. |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $times |
|
54
|
|
|
* @param int $prime |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
4 |
|
protected function generateOptimusNumbers($times = 1, $prime = null) |
|
58
|
|
|
{ |
|
59
|
4 |
|
$prime = $prime ?: null; |
|
60
|
|
|
|
|
61
|
4 |
|
$result = []; |
|
62
|
4 |
|
for ($i = 0; $i < $times; $i++) { |
|
63
|
4 |
|
$result[] = Energon::generate($prime); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the console command options. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
8 |
|
protected function getOptions() |
|
75
|
|
|
{ |
|
76
|
4 |
|
return [ |
|
77
|
8 |
|
['prime', 'p', InputOption::VALUE_OPTIONAL, 'Generate with the given prime'], |
|
78
|
4 |
|
['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1], |
|
79
|
4 |
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|