1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the samshal/rando package. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Adeshina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Samshal\Rando\Packages\Basics; |
12
|
|
|
|
13
|
|
|
use Samshal\Rando\Packages\PackageableInterface; |
14
|
|
|
use Samshal\Rando\Packages\Packages; |
15
|
|
|
use Samshal\Rando\Rando; |
16
|
|
|
|
17
|
|
|
class Character extends Packages implements PackageableInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected $pool; |
21
|
|
|
protected $alpha; |
22
|
|
|
protected $casing; |
23
|
|
|
protected $symbols; |
24
|
|
|
|
25
|
|
|
private $lowerAlphs = 'abcdefghijklmnopqrstuvwxyz'; |
26
|
|
|
private $upperAlphs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
27
|
|
|
private $numbers = '0123456789'; |
28
|
|
|
private $specialChars = '!@#$%^&*()'; |
29
|
|
|
|
30
|
|
|
public function setDefaults($defaultsArray = []) |
31
|
|
|
{ |
32
|
|
|
$defaultsArray['pool'] = $this->lowerAlphs.$this->upperAlphs.$this->numbers.$this->specialChars; |
33
|
|
|
|
34
|
|
|
return $defaultsArray; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function setPool($pool) |
38
|
|
|
{ |
39
|
|
|
$this->pool = $pool; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function setAlpha($boolean) |
43
|
|
|
{ |
44
|
|
|
$this->alpha = $boolean; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function setCasing($casing) |
48
|
|
|
{ |
49
|
|
|
$this->casing = $casing; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setSymbols($boolean) |
53
|
|
|
{ |
54
|
|
|
$this->symbols = $boolean; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function generatePool() |
58
|
|
|
{ |
59
|
|
|
if (!empty($this->symbols)) { |
60
|
|
|
$this->pool = $this->specialChars; |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!empty($this->casing)) { |
65
|
|
|
if ($this->casing == 'upper') { |
66
|
|
|
$this->pool = $this->upperAlphs; |
67
|
|
|
} elseif ($this->casing == 'lower') { |
68
|
|
|
$this->pool = $this->lowerAlphs; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!empty($this->alpha)) { |
75
|
|
|
$this->pool = $this->upperAlphs.$this->lowerAlphs; |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function doRandomization() |
83
|
|
|
{ |
84
|
|
|
self::generatePool(); |
85
|
|
|
$characters = str_split($this->pool); |
86
|
|
|
$character = $characters[array_rand($characters)]; |
87
|
|
|
|
88
|
|
|
return $character; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function stringify() |
92
|
|
|
{ |
93
|
|
|
return self::doRandomization(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|