-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from swagger-mock/unique-array-improvements
Unique array improvements
- Loading branch information
Showing
31 changed files
with
686 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ build: | |
- php-scrutinizer-run | ||
tests: | ||
environment: | ||
php: | ||
version: 7.3 | ||
variables: | ||
APP_ENV: 'test' | ||
tests: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,36 +10,39 @@ | |
|
||
namespace App\Mock\Generation\Value\Composite\ArrayGenerator; | ||
|
||
use App\Mock\Generation\Value\Length\LengthGenerator; | ||
use App\Mock\Generation\Value\Unique\UniqueValueGeneratorFactory; | ||
use App\Mock\Generation\Value\ValueGeneratorInterface; | ||
use App\Mock\Parameters\Schema\Type\Composite\ArrayType; | ||
use App\Mock\Parameters\Schema\Type\TypeInterface; | ||
|
||
/** | ||
* @author Igor Lazarev <[email protected]> | ||
*/ | ||
class ArrayUniqueValueGenerator | ||
{ | ||
private const MAX_ATTEMPTS = 100; | ||
|
||
/** @var ArrayLengthGenerator */ | ||
/** @var LengthGenerator */ | ||
private $lengthGenerator; | ||
|
||
public function __construct(ArrayLengthGenerator $lengthGenerator) | ||
/** @var UniqueValueGeneratorFactory */ | ||
private $uniqueValueGeneratorFactory; | ||
|
||
public function __construct(LengthGenerator $lengthGenerator, UniqueValueGeneratorFactory $uniqueValueGeneratorFactory) | ||
{ | ||
$this->lengthGenerator = $lengthGenerator; | ||
$this->uniqueValueGeneratorFactory = $uniqueValueGeneratorFactory; | ||
} | ||
|
||
public function generateArray(ValueGeneratorInterface $generator, ArrayType $type): array | ||
{ | ||
$length = $this->lengthGenerator->generateArrayLength($type); | ||
$uniqueGenerator = $this->uniqueValueGeneratorFactory->createGenerator($generator, $type->items); | ||
$length = $this->lengthGenerator->generateLength($type->minItems, $type->maxItems); | ||
|
||
$values = []; | ||
$uniqueValues = []; | ||
|
||
for ($i = 1; $i <= $length->value; $i++) { | ||
[$value, $attemptsExceeded] = $this->generateUniqueValue($generator, $type->items, $uniqueValues); | ||
$value = $uniqueGenerator->nextValue(); | ||
|
||
if ($attemptsExceeded) { | ||
if ($uniqueGenerator->isAttemptsExceedingLimit()) { | ||
if ($i > $length->minValue) { | ||
break; | ||
} | ||
|
@@ -52,25 +55,4 @@ public function generateArray(ValueGeneratorInterface $generator, ArrayType $typ | |
|
||
return $values; | ||
} | ||
|
||
private function generateUniqueValue(ValueGeneratorInterface $generator, TypeInterface $itemsType, array &$uniqueValues): array | ||
{ | ||
$attempts = 0; | ||
$attemptsExceeded = false; | ||
|
||
do { | ||
$value = $generator->generateValue($itemsType); | ||
$attempts++; | ||
|
||
if ($attempts > self::MAX_ATTEMPTS) { | ||
$attemptsExceeded = true; | ||
|
||
break; | ||
} | ||
} while (\in_array($value, $uniqueValues, true)); | ||
|
||
$uniqueValues[] = $value; | ||
|
||
return [$value, $attemptsExceeded]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.