Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Add named unique() "bags" #1844

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
15 changes: 10 additions & 5 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Base
protected $generator;

/**
* @var \Faker\UniqueGenerator
* @var \Faker\UniqueGenerator[]
*/
protected $unique;
protected $unique = array();

/**
* @param \Faker\Generator $generator
Expand Down Expand Up @@ -566,20 +566,25 @@ public function optional($weight = 0.5, $default = null)
* $faker->unique()->randomElement(array(1, 2, 3));
* </code>
*
* @param string $bag Defines separate lists of existsing values
* @param boolean $reset If set to true, resets the list of existing values
* @param integer $maxRetries Maximum number of retries to find a unique value,
* After which an OverflowException is thrown.
* @throws \OverflowException When no unique value can be found by iterating $maxRetries times
*
* @return UniqueGenerator A proxy class returning only non-existing values
*/
public function unique($reset = false, $maxRetries = 10000)
public function unique($bag = null, $reset = false, $maxRetries = 10000)
{
if (!is_string($bag)) {
return $this->unique('default', $bag, is_int($reset) ? $reset : 10000);
}

if ($reset || !$this->unique) {
$this->unique = new UniqueGenerator($this->generator, $maxRetries);
$this->unique[$bag] = new UniqueGenerator($this->generator, $maxRetries);
}

return $this->unique;
return $this->unique[$bag];
}

/**
Expand Down