v7.0.0-beta.2 release
This commit is contained in:
1 parent
6595fd0eb5
commit
fa8eba7a0d
1165 files changed
+107118
-762127
No files matched your search
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\SampleData;
|
||||
|
||||
/**
|
||||
* FakerShim::optional() 프록시
|
||||
*
|
||||
* Faker 와 동일한 API 제공:
|
||||
* $faker->optional()->sentence() // 50% 확률로 null
|
||||
* $faker->optional(0.7)->name() // 70% 확률로 name, 30% null
|
||||
* $faker->optional(0.3, 'default')->x() // 30% 확률 x, 70% default
|
||||
*
|
||||
* weight 해석:
|
||||
* - 0.0 ~ 1.0 : 확률
|
||||
* - 1 초과 값 : Faker 관례상 정수 0-100 퍼센트로 간주
|
||||
*/
|
||||
class OptionalShim
|
||||
{
|
||||
public function __construct(
|
||||
private FakerShim $shim,
|
||||
private float|int $weight,
|
||||
private mixed $default = null,
|
||||
) {}
|
||||
|
||||
public function __call(string $method, array $arguments): mixed
|
||||
{
|
||||
$threshold = $this->weight <= 1
|
||||
? (int) round($this->weight * 100)
|
||||
: (int) $this->weight;
|
||||
|
||||
if (random_int(1, 100) <= $threshold) {
|
||||
return $this->shim->{$method}(...$arguments);
|
||||
}
|
||||
|
||||
return $this->default;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user