-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfaker.php
135 lines (114 loc) · 2.3 KB
/
faker.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Class for generating fake data
*
* @package Faker
* @version 0.2
* @copyright 2007 Caius Durling
* @author Caius Durling
* @author ifunk
* @author FionaSarah
*
*/
/**
* Faker Class
*
* @package Faker
*/
class Faker
{
public static $_instances = array();
public function __construct()
{
}
public function __tostring()
{
return "";
}
public function &__get( $var )
{
if (empty(Faker::$_instances[$var])) {
$filename = dirname(__FILE__)."/lib/".strtolower($var).".php";
if(!file_exists($filename))
return NULL;
include $filename;
Faker::$_instances[$var] = new $var;
}
return Faker::$_instances[$var];
}
// todo: use __autoload()
/**
* Returns a random element from a passed array
*
* @param array $array
* @return string
* @author Caius Durling
*/
protected function random(&$array)
{
return $array[mt_rand(0, count($array)-1)];
}
/**
* Returns a random number between 0 and 9
*
* @return integer
* @author Caius Durling
*/
protected function rand_num()
{
return mt_rand(0, 9);
}
/**
* Returns a random letter from a to z
*
* @return string
* @author Caius Durling
*/
protected function rand_letter()
{
return chr(mt_rand(97, 122));
}
/**
* Replaces all occurrences of # with a random number
*
* @param string $string String you wish to have parsed
* @return string
* @author Caius Durling
*/
public function numerify( $string )
{
foreach ( str_split( $string ) as $char ) {
$result[] = str_replace( '#', $this->rand_num(), $char );
}
return join( $result );
}
/**
* Replaces all occurrences of ? with a random letter
*
* @param string $string String you wish to have parsed
* @return string
* @author Caius Durling
*/
public function lexify( $string )
{
foreach ( str_split( $string ) as $char ) {
$result[] = str_replace( '?', $this->rand_letter(), $char );
}
return join( $result );
}
/**
* Replaces all occurrences of # with a random number and
* replaces all occurrences of ? with a random letter
*
* @param string $string String you wish to have parsed
* @return string
* @author Caius Durling
*/
public function bothify( $string )
{
$result = $this->numerify( $string );
$result = $this->lexify( $result );
return $result;
}
}
?>