Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/UniqueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,45 @@ public function ext(string $id)
return new self($this->generator->ext($id), $this->maxRetries, $this->uniques);
}

/**
* Returns a ChanceGenerator that wraps this UniqueGenerator.
*
* This ensures that chaining unique()->optional() works correctly
* by having ChanceGenerator delegate back to UniqueGenerator for
* actual value generation, rather than bypassing uniqueness tracking.
*
* @param float $weight A probability between 0 and 1, 0 means that we always get the default value.
* @param mixed $default The default value to return when the random check fails.
*
* @return ChanceGenerator
*/
public function optional(float $weight = 0.5, $default = null)
{
if ($weight > 1) {
trigger_deprecation('fakerphp/faker', '1.16', 'First argument ($weight) to method "optional()" must be between 0 and 1. You passed %f, we assume you meant %f.', $weight, $weight / 100);
$weight /= 100;
}

return new ChanceGenerator($this, $weight, $default);
}

/**
* Returns a ValidGenerator that wraps this UniqueGenerator.
*
* This ensures that chaining unique()->valid() works correctly
* by having ValidGenerator delegate back to UniqueGenerator for
* actual value generation.
*
* @param \Closure|null $validator A function returning true for valid values
* @param int $maxRetries Maximum number of retries to find a valid value
*
* @return ValidGenerator
*/
public function valid(?\Closure $validator = null, int $maxRetries = 10000)
{
return new ValidGenerator($this, $validator, $maxRetries);
}

/**
* Catch and proxy all generator calls but return only unique values
*
Expand Down