Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Faker.php
1 <?php
2 
3 /**
4  * Class for generating fake data
5  *
6  * @package Faker
7  * @version 0.2
8  * @copyright 2007 Caius Durling
9  * @author Caius Durling
10  * @author ifunk
11  * @author FionaSarah
12  *
13  */
14 
15 /**
16 * Faker Class
17 *
18 * @package Faker
19 */
20 class Faker
21 {
22 
23  public static $_instances = array();
24 
25  public function __construct()
26  {
27  }
28 
29  public function __tostring()
30  {
31  return "";
32  }
33 
34  public function __get( $var )
35  {
36  if (empty(Faker::$_instances[$var])) {
37 
38  $filename = dirname(__FILE__)."/lib/".strtolower($var).".php";
39 
40  if(!file_exists($filename))
41  return NULL;
42 
43  include $filename;
44 
45  Faker::$_instances[$var] = new $var;
46 
47  }
48  return Faker::$_instances[$var];
49  }
50 
51  // todo: use __autoload()
52 
53  /**
54  * Returns a random element from a passed array
55  *
56  * @param array $array
57  * @return string
58  * @author Caius Durling
59  */
60  protected function random(&$array)
61  {
62  return $array[mt_rand(0, count($array)-1)];
63  }
64 
65  /**
66  * Returns a random number between 0 and 9
67  *
68  * @return integer
69  * @author Caius Durling
70  */
71  protected function rand_num()
72  {
73  return mt_rand(0, 9);
74  }
75 
76  /**
77  * Returns a random letter from a to z
78  *
79  * @return string
80  * @author Caius Durling
81  */
82  protected function rand_letter()
83  {
84  return chr(mt_rand(97, 122));
85  }
86 
87 
88  /**
89  * Replaces all occurrences of # with a random number
90  *
91  * @param string $string String you wish to have parsed
92  * @return string
93  * @author Caius Durling
94  */
95  public function numerify( $string )
96  {
97  foreach ( str_split( $string ) as $char ) {
98  $result[] = str_replace( '#', $this->rand_num(), $char );
99  }
100  return join( $result );
101  }
102 
103  /**
104  * Replaces all occurrences of ? with a random letter
105  *
106  * @param string $string String you wish to have parsed
107  * @return string
108  * @author Caius Durling
109  */
110  public function lexify( $string )
111  {
112  foreach ( str_split( $string ) as $char ) {
113  $result[] = str_replace( '?', $this->rand_letter(), $char );
114  }
115  return join( $result );
116  }
117 
118  /**
119  * Replaces all occurrences of # with a random number and
120  * replaces all occurrences of ? with a random letter
121  *
122  * @param string $string String you wish to have parsed
123  * @return string
124  * @author Caius Durling
125  */
126  public function bothify( $string )
127  {
128  $result = $this->numerify( $string );
129  $result = $this->lexify( $result );
130  return $result;
131  }
132 
133 }
134 
135 ?>