-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegenerator.php
More file actions
99 lines (74 loc) · 2.43 KB
/
Copy pathcodegenerator.php
File metadata and controls
99 lines (74 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Gerador de código aleatório, com opções de configuração.
*
* @author Francisco Yure <franciscoyurep@gmail.com>
* @since 2014-09-09 0.1
* @version 0.1
*
*/
class CodeGenerator {
const NUMBERS = '0123456789';
const ALPHABET_LOWER_CASE = 'abcdefghijklmnopqrstuvwxyz';
const ALPHABET_UPPER_CASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const SPECIAL_CHARACTERS = '!#$%&\()*+,-./:;<=>?^_{|}~[]';
private $isNumber;
private $isAlphabetLowerCase;
private $isAlphabetUpperCase;
private $isSpecialCharacter;
private $amount;
private $characters;
private $removeCharacters;
private $code;
public function __construct() {
$this->code = '';
$this->isNumber = true;
$this->isAlphabetLowerCase = true;
$this->isAlphabetUpperCase = true;
$this->isSpecialCharacter = true;
$this->characters = '';
$this->amount = 10;
$this->removeCharacters = '';
}
private function setCharacter() {
$this->characters .= $this->isNumber ? self::NUMBERS : '';
$this->characters .= $this->isAlphabetUpperCase ? self::ALPHABET_UPPER_CASE : '';
$this->characters .= $this->isSpecialCharacter ? self::SPECIAL_CHARACTERS : '';
$this->characters .= $this->isAlphabetLowerCase ? self::ALPHABET_LOWER_CASE : '';
if (strlen($this->removeCharacters) > 0) {
$strArrayRemove = str_split($this->removeCharacters);
$newCharacters = str_replace($strArrayRemove, '', $this->characters);
$this->characters = $newCharacters;
}
if (empty($this->characters)) {
throw new CodeGeneratorException("Error the string is clean, at least one must be set to TRUE.", 1);
}
}
public function setAmount($amount) {
$this->amount = $amount;
}
public function setNumber($isNumber) {
$this->isNumber = $isNumber;
}
public function setAlphabetUpperCase($isAlphabetUpperCase) {
$this->isAlphabetUpperCase = $isAlphabetUpperCase;
}
public function setSpecialCharacter($isSpecialCharacter) {
$this->isSpecialCharacter = $isSpecialCharacter;
}
public function setAlphabetLowerCase($isAlphabetLowerCase) {
$this->isAlphabetLowerCase = $isAlphabetLowerCase;
}
public function setRemovecharacters($removeCharacters) {
$this->removeCharacters = $removeCharacters;
}
public function generate() {
$this->setCharacter();
for ($i = 0; $i < $this->amount; $i++) {
$max = strlen($this->characters) - 1;
$this->code .= $this->characters[mt_rand(0, $max)];
}
return $this->code;
}
}
class CodeGeneratorException extends Exception {}