Skip to content

Commit a65760a

Browse files
Merge pull request #179 from lucaslaurent04/enhance-model-generate-handle-qty
Enhance model generate handle qty
2 parents 35135e1 + 2d43fd6 commit a65760a

File tree

6 files changed

+269
-224
lines changed

6 files changed

+269
-224
lines changed

lib/equal/data/DataGenerator.class.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public static function generateByFieldConf(string $field, array $field_conf, str
6767
case 'boolean':
6868
return self::boolean();
6969
case 'integer':
70-
return self::integer(9);
70+
return self::integerByLength(9);
7171
case 'float':
72-
return self::realNumber(9, 2);
72+
return self::realNumberByLength(9, 2);
7373
}
7474

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

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

122122
return mt_rand($min, $max);
123123
}
124124

125-
public static function realNumber(int $precision, int $scale): float {
125+
public static function integer(int $min, int $max): int {
126+
return mt_rand($min, $max);
127+
}
128+
129+
public static function realNumberByLength(int $precision, int $scale): float {
126130
$max_int_part = pow(10, $precision) - 1;
127131
$min_int_part = -$max_int_part;
128132

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

142+
public static function realNumber(float $min, float $max, int $decimals): float {
143+
$scale = pow(10, $decimals);
144+
145+
return mt_rand($min * $scale, $max * $scale) / $scale;
146+
}
147+
138148
public static function hexadecimal(int $length): string {
139149
$num_bytes = ceil($length / 2);
140150
$random_bytes = random_bytes($num_bytes);

lib/equal/orm/usages/Usage.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function getScale(): int {
174174
public function __construct(string $usage_str) {
175175

176176
// check usage string consistency
177-
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)) {
177+
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)) {
178178
trigger_error("ORM::invalid usage format $usage_str", QN_REPORT_WARNING);
179179
}
180180
else {

lib/equal/orm/usages/UsageAmount.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ public function generateRandomValue(): float {
6565
case 'money':
6666
case 'percent':
6767
case 'rate':
68-
return DataGenerator::realNumber($this->getPrecision(), $this->getScale());
68+
if($this->getMin() === 0 && $this->getMax() === 0) {
69+
return DataGenerator::realNumberByLength($this->getLength(), $this->getScale());
70+
}
71+
72+
return DataGenerator::realNumber($this->getMin(), $this->getMax(), $this->getScale());
6973
}
7074
return 0;
7175
}

lib/equal/orm/usages/UsageNumber.class.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ public function generateRandomValue() {
9999
case 'boolean':
100100
return DataGenerator::boolean();
101101
case 'integer':
102-
return DataGenerator::integer($this->getLength());
102+
if($this->getMin() === 0 && $this->getMax() === 0) {
103+
return DataGenerator::integerByLength($this->getLength());
104+
}
105+
106+
return DataGenerator::integer($this->getMin(), $this->getMax());
103107
case 'real':
104-
return DataGenerator::realNumber($this->getPrecision(), $this->getScale());
108+
if($this->getMin() === 0 && $this->getMax() === 0) {
109+
return DataGenerator::realNumberByLength($this->getLength(), $this->getScale());
110+
}
111+
112+
return DataGenerator::realNumber($this->getMin(), $this->getMax(), $this->getScale());
105113
case 'hexadecimal':
106114
return DataGenerator::hexadecimal($this->getLength());
107115
}

packages/core/actions/init/seed.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,20 @@
5353
continue;
5454
}
5555
foreach($classes as $class) {
56-
if(!isset($class['name'], $class['qty'])) {
56+
if(!isset($class['name'])) {
5757
continue;
5858
}
5959

6060
$generate_params = [
6161
'entity' => $class['name'],
6262
];
63-
foreach(['lang', 'fields', 'relations', 'add_to_domain_data'] as $param_key) {
63+
foreach(['qty', 'random_qty', 'fields', 'relations', 'set_object_data', 'lang'] as $param_key) {
6464
if(isset($class[$param_key])) {
6565
$generate_params[$param_key] = $class[$param_key];
6666
}
6767
}
6868

69-
$qty = is_array($class['qty']) ? mt_rand($class['qty'][0], $class['qty'][1]) : $class['qty'];
70-
for($i = 0; $i < $qty; $i++) {
71-
eQual::run('do', 'core_model_generate', $generate_params);
72-
}
69+
eQual::run('do', 'core_model_generate', $generate_params);
7370
}
7471
}
7572
}

0 commit comments

Comments
 (0)