Skip to content
18 changes: 14 additions & 4 deletions lib/equal/data/DataGenerator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static function generateByFieldConf(string $field, array $field_conf, str
case 'boolean':
return self::boolean();
case 'integer':
return self::integer(9);
return self::integerByLength(9);
case 'float':
return self::realNumber(9, 2);
return self::realNumberByLength(9, 2);
}

return null;
Expand Down Expand Up @@ -115,14 +115,18 @@ public static function boolean($probability = 0.5): bool {
return mt_rand() / mt_getrandmax() < $probability;
}

public static function integer(int $length): int {
public static function integerByLength(int $length): int {
$min = (pow(10, $length) - 1) * -1;
$max = pow(10, $length) - 1;

return mt_rand($min, $max);
}

public static function realNumber(int $precision, int $scale): float {
public static function integer(int $min, int $max): int {
return mt_rand($min, $max);
}

public static function realNumberByLength(int $precision, int $scale): float {
$max_int_part = pow(10, $precision) - 1;
$min_int_part = -$max_int_part;

Expand All @@ -135,6 +139,12 @@ public static function realNumber(int $precision, int $scale): float {
return round($random_float, $scale);
}

public static function realNumber(float $min, float $max, int $decimals): float {
$scale = pow(10, $decimals);

return mt_rand($min * $scale, $max * $scale) / $scale;
}

public static function hexadecimal(int $length): string {
$num_bytes = ceil($length / 2);
$random_bytes = random_bytes($num_bytes);
Expand Down
2 changes: 1 addition & 1 deletion lib/equal/orm/usages/Usage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function getScale(): int {
public function __construct(string $usage_str) {

// check usage string consistency
if(!preg_match('/([a-z]+)(\[([0-9]+)\])?\/?([-a-z0-9]*)(\.([-a-z0-9.]*))?(:(([-0-9a-z]*)\.?([0-9]*)))?({([0-9]+)(,([0-9]+))?})?/', $usage_str, $matches)) {
if(!preg_match('/([a-z]+)(\[([0-9]+)\])?\/?([-a-z0-9]*)(\.([-a-z0-9.]*))?(:(([-0-9a-z]*)\.?([0-9]*)))?({(-?[0-9]+)(,(-?[0-9]+))?})?/', $usage_str, $matches)) {
trigger_error("ORM::invalid usage format $usage_str", QN_REPORT_WARNING);
}
else {
Expand Down
6 changes: 5 additions & 1 deletion lib/equal/orm/usages/UsageAmount.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public function generateRandomValue(): float {
case 'money':
case 'percent':
case 'rate':
return DataGenerator::realNumber($this->getPrecision(), $this->getScale());
if($this->getMin() === 0 && $this->getMax() === 0) {
return DataGenerator::realNumberByLength($this->getLength(), $this->getScale());
}

return DataGenerator::realNumber($this->getMin(), $this->getMax(), $this->getScale());
}
return 0;
}
Expand Down
12 changes: 10 additions & 2 deletions lib/equal/orm/usages/UsageNumber.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ public function generateRandomValue() {
case 'boolean':
return DataGenerator::boolean();
case 'integer':
return DataGenerator::integer($this->getLength());
if($this->getMin() === 0 && $this->getMax() === 0) {
return DataGenerator::integerByLength($this->getLength());
}

return DataGenerator::integer($this->getMin(), $this->getMax());
case 'real':
return DataGenerator::realNumber($this->getPrecision(), $this->getScale());
if($this->getMin() === 0 && $this->getMax() === 0) {
return DataGenerator::realNumberByLength($this->getLength(), $this->getScale());
}

return DataGenerator::realNumber($this->getMin(), $this->getMax(), $this->getScale());
case 'hexadecimal':
return DataGenerator::hexadecimal($this->getLength());
}
Expand Down
9 changes: 3 additions & 6 deletions packages/core/actions/init/seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,20 @@
continue;
}
foreach($classes as $class) {
if(!isset($class['name'], $class['qty'])) {
if(!isset($class['name'])) {
continue;
}

$generate_params = [
'entity' => $class['name'],
];
foreach(['lang', 'fields', 'relations', 'add_to_domain_data'] as $param_key) {
foreach(['qty', 'random_qty', 'fields', 'relations', 'set_object_data', 'lang'] as $param_key) {
if(isset($class[$param_key])) {
$generate_params[$param_key] = $class[$param_key];
}
}

$qty = is_array($class['qty']) ? mt_rand($class['qty'][0], $class['qty'][1]) : $class['qty'];
for($i = 0; $i < $qty; $i++) {
eQual::run('do', 'core_model_generate', $generate_params);
}
eQual::run('do', 'core_model_generate', $generate_params);
}
}
}
Expand Down
Loading