| 1 |
<?php |
| 2 |
|
| 3 |
namespace Faker; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class UniqueGenerator |
| 10 |
{ |
| 11 |
protected $generator; |
| 12 |
protected $maxRetries; |
| 13 |
protected $moreStuff; |
| 14 |
protected $uniques = array(); |
| 15 |
|
| 16 |
public function __construct(Generator $generator, $maxRetries) |
| 17 |
{ |
| 18 |
$this->generator = $generator; |
| 19 |
$this->maxRetries = $maxRetries + 1; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function __get($attribute) |
| 26 |
{ |
| 27 |
return $this->__call($attribute, array()); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __call($name, $arguments) |
| 34 |
{ |
| 35 |
$i = 0; |
| 36 |
if (!isset($this->uniques[$name])) { |
| 37 |
$this->uniques[$name] = array(); |
| 38 |
} |
| 39 |
do { |
| 40 |
$res = call_user_func_array(array($this->generator, $name), $arguments); |
| 41 |
$i++; |
| 42 |
if ($i >= $this->maxRetries) { |
| 43 |
throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a unique value', $this->maxRetries)); |
| 44 |
} |
| 45 |
} while (in_array($res, $this->uniques[$name])); |
| 46 |
$this->uniques[$name][]= $res; |
| 47 |
|
| 48 |
return $res; |
| 49 |
} |
| 50 |
} |