1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CupsGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to generate a random cups. |
7
|
|
|
*/ |
8
|
|
|
class Cups |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Cups id. |
12
|
|
|
* |
13
|
|
|
* @param $string |
14
|
|
|
*/ |
15
|
|
|
private $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Generate a Random cups. |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function generate() |
23
|
|
|
{ |
24
|
|
|
$reeId = $this->generateReeId(); |
25
|
|
|
$distId = $this->generateDistId(); |
26
|
|
|
|
27
|
|
|
$control = ($reeId.$distId) % 529; |
28
|
|
|
$division = $control / 23; |
29
|
|
|
$resto = $control % 23; |
30
|
|
|
|
31
|
|
|
$this->id = $this->getCountry() |
32
|
|
|
. $reeId |
33
|
|
|
. $distId |
34
|
|
|
. $this->getControlNumbersBy($division) |
35
|
|
|
. $this->getControlNumbersBy($resto); |
36
|
|
|
|
37
|
|
|
return $this->id; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Genera los 4 numeros dados por la Red electrica de España |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
private function generateReeId() |
46
|
|
|
{ |
47
|
|
|
$random = mt_rand(0, 9999); |
48
|
|
|
|
49
|
|
|
return str_pad($random, 4, STR_PAD_LEFT); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Genera los 12 numeros Id del distribuidor. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
private function generateDistId() |
58
|
|
|
{ |
59
|
|
|
if (PHP_INT_SIZE == 4) { |
60
|
|
|
return $this->generateRandomNumberWithRange(9) |
61
|
|
|
. $this->generateRandomNumberWithRange(3); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->generateRandomNumberWithRange(12); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $range |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private function generateRandomNumberWithRange($range) |
72
|
|
|
{ |
73
|
|
|
$randomNumber = mt_rand(0, str_repeat("9", $range)); |
74
|
|
|
|
75
|
|
|
return str_pad($randomNumber, $range, '0', STR_PAD_LEFT); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retorna ISO pais. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function getCountry() |
84
|
|
|
{ |
85
|
|
|
return 'ES'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retorna array with control. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function getControlNumbers() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
0 => 'T', |
97
|
|
|
1 => 'R', |
98
|
|
|
2 => 'W', |
99
|
|
|
3 => 'A', |
100
|
|
|
4 => 'G', |
101
|
|
|
5 => 'M', |
102
|
|
|
6 => 'Y', |
103
|
|
|
7 => 'F', |
104
|
|
|
8 => 'P', |
105
|
|
|
9 => 'D', |
106
|
|
|
10 => 'X', |
107
|
|
|
11 => 'B', |
108
|
|
|
12 => 'N', |
109
|
|
|
13 => 'J', |
110
|
|
|
14 => 'Z', |
111
|
|
|
15 => 'S', |
112
|
|
|
16 => 'Q', |
113
|
|
|
17 => 'V', |
114
|
|
|
18 => 'H', |
115
|
|
|
19 => 'L', |
116
|
|
|
20 => 'C', |
117
|
|
|
21 => 'K', |
118
|
|
|
22 => 'E', |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return array with control. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private function getControlNumbersBy($id) |
128
|
|
|
{ |
129
|
|
|
$controlNumber = $this->getControlNumbers(); |
130
|
|
|
|
131
|
|
|
return $controlNumber[$id]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|