|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PersonGenerator |
|
4
|
|
|
* |
|
5
|
|
|
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
|
6
|
|
|
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted to use or copy this program |
|
9
|
|
|
* for any purpose, provided the above notices are retained on all copies. |
|
10
|
|
|
* Permission to modify the code and to distribute modified code is granted, |
|
11
|
|
|
* provided the above notices are retained, and a notice that the code was |
|
12
|
|
|
* modified is included with the above copyright notice. |
|
13
|
|
|
* |
|
14
|
|
|
* @category Wp |
|
15
|
|
|
* @package Punction |
|
16
|
|
|
* @author Andrzej Marcinkowski <[email protected]> |
|
17
|
|
|
* @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz |
|
18
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
|
19
|
|
|
* @version 1.0 $Id: a88997670673063e272ee5666aa86ed1ec9a1561 $ $Format:%H$ |
|
20
|
|
|
* @link http:// |
|
21
|
|
|
* @since File available since Release 1.0.0 |
|
22
|
|
|
* PHP Version 5 |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace Hospitalplugin\utils; |
|
25
|
|
|
|
|
26
|
|
|
use Hospitalplugin\Entities\Patient; |
|
27
|
|
|
use Hospitalplugin\Entities\PatientZZ; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* PersonGenerator |
|
31
|
|
|
* |
|
32
|
|
|
* @category Wp |
|
33
|
|
|
* @package Punction |
|
34
|
|
|
* @author Andrzej Marcinkowski <[email protected]> |
|
35
|
|
|
* @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz |
|
36
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
|
37
|
|
|
* @version 1.0 $Id: a88997670673063e272ee5666aa86ed1ec9a1561 $ $Format:%H$ |
|
38
|
|
|
* @link http:// |
|
39
|
|
|
* @since File available since Release 1.0.0 |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
class PersonGenerator |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
private static $names; |
|
46
|
|
|
|
|
47
|
|
|
private static $mnames; |
|
48
|
|
|
|
|
49
|
|
|
private static $fnames; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \Hospitalplugin\Entities\Patient |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getRandomPerson() |
|
55
|
|
|
{ |
|
56
|
|
|
// TODO extract paths |
|
57
|
|
|
self::$names = Utils::readFileToArray(__DIR__ . '/../../resources/pl_names.csv'); |
|
58
|
|
|
self::$mnames = Utils::readFileToArray(__DIR__ . '/../../resources/pl_mnames.csv'); |
|
59
|
|
|
self::$fnames = Utils::readFileToArray(__DIR__ . '/../../resources/pl_fnames.csv'); |
|
60
|
|
|
// var_dump(self::$mnames); |
|
|
|
|
|
|
61
|
|
|
$sex = (rand(0, 1) == 0 ? 'm' : 'f'); |
|
62
|
|
|
$firstname = ($sex == 'm' ? self::getRandom(self::$mnames) : self::getRandom(self::$fnames)); |
|
|
|
|
|
|
63
|
|
|
$lastname = self::getRandom(self::$names); |
|
|
|
|
|
|
64
|
|
|
if ($sex == 'f' && self::endsWith($lastname, 'i')) { |
|
|
|
|
|
|
65
|
|
|
$lastname = rtrim($lastname, "i") . 'a'; |
|
66
|
|
|
} |
|
67
|
|
|
$bdate = self::getRandomBirthDate(); |
|
68
|
|
|
$pesel = self::getRandomPesel($bdate, $sex); |
|
69
|
|
|
return $firstname . ' ' . $lastname . '|' . $pesel; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param unknown $haystack |
|
74
|
|
|
* @param unknown $needle |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
static function endsWith($haystack, $needle) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
return $needle === "" || substr($haystack, - strlen($needle)) === $needle; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return random date |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function getRandomBirthDate() |
|
86
|
|
|
{ |
|
87
|
|
|
$age = rand(0, 100); |
|
88
|
|
|
$dayOfYear = rand(0, 365); |
|
89
|
|
|
$interval = new \DateInterval('P' . $age . 'Y' . $dayOfYear . 'D'); |
|
90
|
|
|
$date = new \DateTime(); |
|
91
|
|
|
$birth = $date->sub($interval); |
|
92
|
|
|
return $birth->format('Y-m-d'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* |
|
97
|
|
|
* @param $date |
|
98
|
|
|
* @param $sex |
|
99
|
|
|
* 'm' / 'f' |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function getRandomPesel($date, $sex = 'm') |
|
102
|
|
|
{ |
|
103
|
|
|
$datetime = new \DateTime($date); |
|
104
|
|
|
$y = (int) $datetime->format('y'); |
|
105
|
|
|
$Y = (int) $datetime->format('Y'); |
|
106
|
|
|
$m = (int) $datetime->format('m'); |
|
107
|
|
|
$d = (int) $datetime->format('d'); |
|
108
|
|
|
if ($Y >= 2000) { |
|
109
|
|
|
$m += 20; |
|
110
|
|
|
} |
|
111
|
|
|
$sexNum = ($sex == 'f' ? rand(0, 4) * 2 : rand(0, 4) * 2 + 1); |
|
112
|
|
|
$x = sprintf('%02d%02d%02d%03d%d', $y, $m, $d, rand(0, 999), $sexNum); |
|
113
|
|
|
$arrSteps = array( |
|
114
|
|
|
1, |
|
115
|
|
|
3, |
|
116
|
|
|
7, |
|
117
|
|
|
9, |
|
118
|
|
|
1, |
|
119
|
|
|
3, |
|
120
|
|
|
7, |
|
121
|
|
|
9, |
|
122
|
|
|
1, |
|
123
|
|
|
3 |
|
124
|
|
|
); // tablica z odpowiednimi wagami |
|
125
|
|
|
$intSum = 0; |
|
126
|
|
|
for ($i = 0; $i < 10; $i ++) { |
|
127
|
|
|
$intSum += $arrSteps[$i] * $x[$i]; // mnożymy każdy ze znaków |
|
128
|
|
|
// przez wagć i sumujemy |
|
129
|
|
|
// wszystko |
|
130
|
|
|
} |
|
131
|
|
|
$int = 10 - $intSum % 10; // obliczamy sumć kontrolną |
|
132
|
|
|
$intControlNr = ($int == 10) ? 0 : $int; |
|
133
|
|
|
$x .= $intControlNr; |
|
134
|
|
|
return $x; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* random element frmo array |
|
139
|
|
|
* |
|
140
|
|
|
* @param unknown $arr |
|
141
|
|
|
* @return string random element |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function getRandom($arr) |
|
144
|
|
|
{ |
|
145
|
|
|
return implode('', $arr[array_rand($arr)]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.