| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Dummy_Data; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use Exception; |
| 7 |
|
| 8 |
class Generator { |
| 9 |
/** |
| 10 |
* List of sample words for text generation |
| 11 |
*/ |
| 12 |
private $words = array( |
| 13 |
'lorem', |
| 14 |
'ipsum', |
| 15 |
'dolor', |
| 16 |
'sit', |
| 17 |
'amet', |
| 18 |
'consectetur', |
| 19 |
'adipiscing', |
| 20 |
'elit', |
| 21 |
'sed', |
| 22 |
'do', |
| 23 |
'eiusmod', |
| 24 |
'tempor', |
| 25 |
'incididunt', |
| 26 |
'ut', |
| 27 |
'labore', |
| 28 |
'et', |
| 29 |
'dolore', |
| 30 |
'magna', |
| 31 |
'aliqua', |
| 32 |
'enim', |
| 33 |
'ad', |
| 34 |
'minim', |
| 35 |
'veniam', |
| 36 |
'quis', |
| 37 |
'nostrud', |
| 38 |
'exercitation', |
| 39 |
'ullamco', |
| 40 |
'laboris', |
| 41 |
'nisi', |
| 42 |
); |
| 43 |
|
| 44 |
/** |
| 45 |
* List of sample first names |
| 46 |
*/ |
| 47 |
private $firstNames = array( |
| 48 |
'John', |
| 49 |
'Jane', |
| 50 |
'Michael', |
| 51 |
'Emily', |
| 52 |
'David', |
| 53 |
'Sarah', |
| 54 |
'Robert', |
| 55 |
'Jennifer', |
| 56 |
'William', |
| 57 |
'Elizabeth', |
| 58 |
'James', |
| 59 |
'Patricia', |
| 60 |
'Charles', |
| 61 |
'Linda', |
| 62 |
'Joseph', |
| 63 |
'Barbara', |
| 64 |
); |
| 65 |
|
| 66 |
/** |
| 67 |
* List of sample last names |
| 68 |
*/ |
| 69 |
private $lastNames = array( |
| 70 |
'Smith', |
| 71 |
'Johnson', |
| 72 |
'Williams', |
| 73 |
'Brown', |
| 74 |
'Jones', |
| 75 |
'Miller', |
| 76 |
'Davis', |
| 77 |
'Garcia', |
| 78 |
'Rodriguez', |
| 79 |
'Wilson', |
| 80 |
'Martinez', |
| 81 |
'Anderson', |
| 82 |
'Taylor', |
| 83 |
'Thomas', |
| 84 |
'Hernandez', |
| 85 |
); |
| 86 |
|
| 87 |
/** |
| 88 |
* Get a random element from an array |
| 89 |
* |
| 90 |
* @param array $array |
| 91 |
* @return mixed |
| 92 |
*/ |
| 93 |
public function random_element( array $array ) { |
| 94 |
if ( empty( $array ) ) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
return $array[ array_rand( $array ) ]; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Generate a random sentence |
| 102 |
* |
| 103 |
* @param int $wordCount Number of words in the sentence |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function sentence( $wordCount = 7 ): string { |
| 107 |
$words = array(); |
| 108 |
for ( $i = 0; $i < $wordCount; $i++ ) { |
| 109 |
$words[] = $this->random_element( $this->words ); |
| 110 |
} |
| 111 |
return ucfirst( implode( ' ', $words ) ) . '.'; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Generate a random number between min and max |
| 116 |
* |
| 117 |
* @param int $min |
| 118 |
* @param int $max |
| 119 |
* |
| 120 |
* @return int |
| 121 |
*/ |
| 122 |
public function number_between( int $min = 0, int $max = 100 ): int { |
| 123 |
return wp_rand( $min, $max ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get a random name |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function name(): string { |
| 132 |
return $this->random_element( $this->firstNames ) . ' ' . $this->random_element( $this->lastNames ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get a random email |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function email(): string { |
| 141 |
$domains = array( 'gmail.com', 'yahoo.com', 'outlook.com', 'example.com' ); |
| 142 |
$name = strtolower( str_replace( ' ', '.', $this->name() ) ); |
| 143 |
return $name . '@' . $this->random_element( $domains ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get a random phone number |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
public function phoneNumber(): string { |
| 152 |
return sprintf( |
| 153 |
'(%03d) %03d-%04d', |
| 154 |
wp_rand( 100, 999 ), |
| 155 |
wp_rand( 100, 999 ), |
| 156 |
wp_rand( 1000, 9999 ) |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Generate a random date between two dates |
| 162 |
* |
| 163 |
* @param string $start Start date (strtotime compatible) |
| 164 |
* @param string $end End date (strtotime compatible) |
| 165 |
* @param string $timezone Timezone |
| 166 |
* @return DateTime |
| 167 |
*/ |
| 168 |
public function dateTimeBetween( string $start = '-30 days', string $end = 'now', $timezone = null ): DateTime { |
| 169 |
$start = new DateTime( $start ); |
| 170 |
$end = new DateTime( $end ); |
| 171 |
$randomTimestamp = wp_rand( $start->getTimestamp(), $end->getTimestamp() ); |
| 172 |
$date = new DateTime(); |
| 173 |
$date->setTimestamp( $randomTimestamp ); |
| 174 |
return $date; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Optional value generator |
| 179 |
* |
| 180 |
* @param float $weight Probability of returning a value (0.0 to 1.0) |
| 181 |
* @param mixed $default Default value to return when not generating a value |
| 182 |
* @param mixed $value Value to return when generating a value |
| 183 |
* @return mixed |
| 184 |
*/ |
| 185 |
public function optional( $weight = 0.5, $default = null, $value = null ) { |
| 186 |
return ( wp_rand( 0, 100 ) / 100 ) <= $weight |
| 187 |
? ( $value ?? $this->sentence() ) |
| 188 |
: $default; |
| 189 |
} |
| 190 |
} |
| 191 |
|