1. Go to this page and download the library: Download kfilin/randdata library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class BlankTuple extends \RandData\Tuple
{
public function getDataSets()
{
return [
"name" => "string_list:values=John,Paul,George,Ringo",
"dt" => "date:min=1962-10-05;max=1970-05-08"
];
}
}
$generator = new RandData\BlankGenerator(new BlankTuple);
$tpl = "Hello, I'm {name} and today is {dt}";
$generator->init($tpl);
echo $tpl . " => ". $generator->run() . PHP_EOL;
// Hello, I'm {name} and today is {dt}
// =>
// Hello, I'm George and today is 1965-12-12
class BlankTuple extends \RandData\Tuple
{
public function getDataSets()
{
return [
"name" => "string_list:values=John,Paul,George,Ringo",
"dt" => "date:min=1962-10-05;max=1970-05-08",
"age" => "integer:min=19;max=30"
];
}
// Age field is dependant from dt field. See [Data dependency]
protected function getValue(\RandData\Set $set, $fldName) {
$birthDtList = [
"John" => "1940-10-09",
"Paul" => "1942-06-18",
"George" => "1943-02-25",
"Ringo" => "1940-07-07"
];
if ($fldName == "age") {
$name = $this->result["name"];
$dt = new \DateTime($this->result["dt"]);
$birth = !empty($birthDtList[$name])
? new \Datetime($birthDtList[$name])
: null;
if ($dt && $birth) {
$interval = $birth->diff($dt);
return $interval->format("%y");
}
return 0;
}
return $set->get();
}
}
$generator = new RandData\BlankGenerator(new BlankTuple);
$tpl = "Hello, I'm {name}, my age {age} and today is {dt}. {name} at {dt}";
$generator->init($tpl);
echo $generator->run() . PHP_EOL;
// Hello, I'm John, my age 27 and today is 1968-07-08. John at 1968-07-08
// Hello, I'm Paul, my age 21 and today is 1963-11-14. Paul at 1963-11-14
// Hello, I'm George, my age 24 and today is 1967-08-14. George at 1967-08-14
// Hello, I'm Ringo, my age 28 and today is 1969-07-05. Ringo at 1969-07-05
// ...
class EmployeeTuple extends \RandData\Tuple {
const LEVEL_1 = 40*365*24*3600;
const LEVEL_2 = 30*365*24*3600;
const HIRED_AGE_MIN = 20*365*24*3600;
const HIRED_AGE_MAX = 50*365*24*3600;
public function getDataSets() {
return [
"sex" => "string_list:values=" . RandData\Set\en_GB\Person::SEX_MALE . "," . RandData\Set\en_GB\Person::SEX_FEMALE,
"name" => "en_person",
"birth" => "date:min=now -50 year;max=now -20 year",
"hired" => "date:min=now -3 year;max=now",
"fired" => "date:min=now -3 year;max=now",
"score" => "int:min=1;max=3"
];
}
protected function getNullProbability() {
return [
"fired" => 30
];
}
protected function getSetValueOrNull(RandData\Set $set, $fldName)
{
$value = parent::getSetValueOrNull($set, $fldName);
// Override dependent datasets
if ($fldName == "sex") {
$this->getValueSex($value);
} elseif ($fldName == "birth") {
$this->getValueBirth($value);
} elseif ($fldName == "hired") {
$this->getValueHired($value);
}
return $value;
}
private function getValueSex(&$value) {
$this->datasets["name"] = "en_person:sex=" . $value;
$value = $value == RandData\Set\en_GB\Person::SEX_MALE ? "Male" : "Female";
}
private function getValueHired($value) {
// Fired date must be later than hired date,
// but earlier than today
$hiredTs = date("U", strtotime($value));
$firedDtMin = date("Y-m-d", $hiredTs + 1*24*3600);
$firedDtMax = date("Y-m-d", date("U") - 3*24*3600);
$this->datasets["fired"] = "date:min=" . $firedDtMin . ";max=" .$firedDtMax ;
}
private function getValueBirth($value) {
$birthTs = date("U", strtotime($value));
$nowTs = date("U");
// Hired date must be later than birth date
// Let's we can hire somebody in the ages from 20 to 50
$hiredTsMin = date("Y-m-d", date("U", min([ $birthTs + self::HIRED_AGE_MIN, date("U") ])));
$hiredTsMax = date("Y-m-d", date("U", min([ $birthTs + self::HIRED_AGE_MAX, date("U") ])));
$this->datasets["hired"] = "date:min=" . $hiredTsMin . ";max=" . $hiredTsMax;
// Let's some dummy score will be dependant on age
if (self::LEVEL_1 < $nowTs - $birthTs) {
$this->datasets["score"] = "int:min=20;max=25";
} elseif (self::LEVEL_2 < $nowTs - $birthTs) {
$this->datasets["score"] = "int:min=10;max=13";
}
}
}
$employeeTuple = new EmployeeTuple();
$generator = new \RandData\Generator($employeeTuple, 50);
$formatter = new \RandData\Formatter\Csv($generator);
echo $formatter->build() . PHP_EOL;
$bookTuple->setAuthorIds($authorsIds)
// ... and then choose from this list
class PersonTuple extends \RandData\Tuple
{
public function getDataSets() {
return [
// ...
"author_id" => "integer:min=1;max=100",
// ...
];
}
}
class CarTuple extends \RandData\Tuple
{
public function getDataSets() {
return [
"mark" => "string_list:values=ford,bmw,audi,vw,skoda,toyota,volvo,mercedez,bently,saab",
"color" => "string_list:values=white,black,grey,blue,yellow,green,orange,red",
"year" => "integer:min=1950;max=2017"
];
}
}
class PersonTuple extends \RandData\Tuple {
public function getDataSets() {
return [
"name" => "en_person",
"birth" => "date:min=1920-01-01;max=1999-12-31",
"login" => "string:char_list=abcdefghjklmnopqrstuvwxyz0123456789;length_min=2;length_max=8",
"car" => new CarTuple()
];
}
protected function getNullProbability() {
return [
"car" => 30
];
}
}
class OrderTuple extends \RandData\Tuple {
protected $personList;
function __construct($personList) {
parent::__construct();
$this->personList = $personList;
}
public function getDataSets() {
return [
"num" => "complex:template=id{string:char_list=abcdef;length_min=2;length_max=2}/" . date("Ymd") . "/{integer:min=1000;max=9999}",
"delivery_address" => "en_address",
"price" => "integer:min=50;max=1000",
"person" => "value:value=person"
];
}
protected function getSetValueOrNull(RandData\Set $set, $fldName) {
if ($fldName == "person") {
return $this->personList[array_rand($this->personList)];
}
return $set->get();
}
}
$generator = new \RandData\Generator(new PersonTuple(), 3);
$personList = $generator->run();
$ot = new OrderTuple($personList);
$generator2 = new \RandData\Generator($ot, 5);
$orderList = $generator2->run();
var_dump($generator2->run());
echo PHP_EOL;
$tpl = "Hello, I'm {string_list:values=Peter,James,John}, my age is {integer:min=5;max=30}, I live @ {en_address} and today is {date}";
$generator = new \RandData\Set\Complex($tpl);
echo $generator->run() . PHP_EOL;