From b9f3ac11f5ba2d996ee39c8fca5dafe14e47ea3f Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Mon, 17 Jul 2023 22:10:41 +0300 Subject: [PATCH] AssociativeArrayGenerator --- .../generators/Arrays/ArrayInvariant.php | 3 ++- .../Arrays/AssociativeArrayInvariant.php | 5 ++--- .../Generator/Arrays/ArrayGenerator.php | 7 +++++-- .../Arrays/AssociativeArrayGenerator.php | 19 ++++++++++++++++++- .../Generator/Float/FloatGenerator.php | 3 ++- src/Generator/Type/Arrays/ArrayType.php | 3 ++- .../Type/Arrays/AssociativeArrayType.php | 3 ++- 7 files changed, 33 insertions(+), 10 deletions(-) diff --git a/invariants/examples/generators/Arrays/ArrayInvariant.php b/invariants/examples/generators/Arrays/ArrayInvariant.php index b1905c7..d97aeeb 100644 --- a/invariants/examples/generators/Arrays/ArrayInvariant.php +++ b/invariants/examples/generators/Arrays/ArrayInvariant.php @@ -5,12 +5,13 @@ use PhpInvariant\BaseInvariant\BaseInvariant; use PhpInvariant\Finish\FinishCount; use PhpInvariant\Generator\Type\Arrays\ArrayType; +use PhpInvariant\Generator\Type\Integer\IntegerType; use PhpInvariant\Generator\Type\String\StringType; class ArrayInvariant extends BaseInvariant { #[FinishCount(5)] - public function checkArray(#[ArrayType(3, new StringType(5, 10, ['a', 'b']))] array $elements) + public function checkArray(#[ArrayType(new IntegerType(3, 3), new StringType(5, 10, ['a', 'b']))] array $elements) { $this->assertCount($elements, 3); foreach ($elements as $element) { diff --git a/invariants/examples/generators/Arrays/AssociativeArrayInvariant.php b/invariants/examples/generators/Arrays/AssociativeArrayInvariant.php index f8cd9fb..3458860 100644 --- a/invariants/examples/generators/Arrays/AssociativeArrayInvariant.php +++ b/invariants/examples/generators/Arrays/AssociativeArrayInvariant.php @@ -16,16 +16,15 @@ public function checkAssociativeArray( new StringType(1, 10, ['a', 'b']), new IntegerType(100, 999), new IntegerType(3, 5), + new IntegerType(2, 3), )] array $associativeArray ) { $this->assertGreaterOrEqual(count($associativeArray), 3); $this->assertLessOrEqual(count($associativeArray), 5); foreach ($associativeArray as $key => $value) { - $this->assertTrue(is_string($key)); $this->assertGreaterOrEqual(strlen($key), 1); $this->assertLessOrEqual(strlen($key), 10); - $this->assertGreaterOrEqual($value, 100); - $this->assertLessOrEqual($value, 999); + $this->assertTrue(is_array($value)); } } diff --git a/src/Generator/Generator/Arrays/ArrayGenerator.php b/src/Generator/Generator/Arrays/ArrayGenerator.php index f2e263a..1c986de 100644 --- a/src/Generator/Generator/Arrays/ArrayGenerator.php +++ b/src/Generator/Generator/Arrays/ArrayGenerator.php @@ -2,13 +2,15 @@ namespace PhpInvariant\Generator\Generator\Arrays; +use PhpInvariant\Generator\Generator\Integer\IntegerGenerator; use PhpInvariant\Generator\GeneratorFactory; use PhpInvariant\Generator\Type\Arrays\ArrayType; class ArrayGenerator { public function __construct( - private GeneratorFactory $generatorFactory + private GeneratorFactory $generatorFactory, + private IntegerGenerator $integerGenerator ) { } @@ -17,9 +19,10 @@ public function __construct( */ public function __invoke(ArrayType $type): array { + $count = call_user_func($this->integerGenerator, $type->count); $generator = $this->generatorFactory->getGenerator($type->elementType); $data = []; - for ($i = 0; $i < $type->count; $i++) { + for ($i = 0; $i < $count; $i++) { $data[] = $generator($type->elementType); } return $data; diff --git a/src/Generator/Generator/Arrays/AssociativeArrayGenerator.php b/src/Generator/Generator/Arrays/AssociativeArrayGenerator.php index 9202788..58c5c26 100644 --- a/src/Generator/Generator/Arrays/AssociativeArrayGenerator.php +++ b/src/Generator/Generator/Arrays/AssociativeArrayGenerator.php @@ -18,6 +18,16 @@ public function __construct( * @return array */ public function __invoke(AssociativeArrayType $type): array + { + $depth = call_user_func($this->integerGenerator, $type->depth); + return $this->recursiveGenerate([], $type, $depth); + } + + /** + * @param array $current + * @return array + */ + private function recursiveGenerate(array $current, AssociativeArrayType $type, int $depth): array { $count = call_user_func($this->integerGenerator, $type->count); $keyGenerator = $this->generatorFactory->getGenerator($type->key); @@ -25,7 +35,14 @@ public function __invoke(AssociativeArrayType $type): array $data = []; for ($i = 0; $i < $count; $i++) { - $data[call_user_func($keyGenerator, $type->key)] = call_user_func($valueGenerator, $type->value); + if ($depth === 0) { + $value = $valueGenerator($type->value); + } else { + $value = $this->recursiveGenerate($current, $type, $depth - 1); + } + + + $data[$keyGenerator($type->key)] = $value; } diff --git a/src/Generator/Generator/Float/FloatGenerator.php b/src/Generator/Generator/Float/FloatGenerator.php index bd2114e..4d64e01 100644 --- a/src/Generator/Generator/Float/FloatGenerator.php +++ b/src/Generator/Generator/Float/FloatGenerator.php @@ -2,6 +2,7 @@ namespace PhpInvariant\Generator\Generator\Float; +use PhpInvariant\Generator\Type\Float\FloatType; use PhpInvariant\Random\Random; class FloatGenerator @@ -12,7 +13,7 @@ public function __construct( } - public function __invoke(\PhpInvariant\Generator\Type\Float\FloatType $type): float + public function __invoke(FloatType $type): float { $maxIntegerPart = (int)floor($type->max); $minIntegerPart = (int)floor($type->min); diff --git a/src/Generator/Type/Arrays/ArrayType.php b/src/Generator/Type/Arrays/ArrayType.php index 352935a..225a658 100644 --- a/src/Generator/Type/Arrays/ArrayType.php +++ b/src/Generator/Type/Arrays/ArrayType.php @@ -3,12 +3,13 @@ namespace PhpInvariant\Generator\Type\Arrays; use Attribute; +use PhpInvariant\Generator\Type\Integer\IntegerType; #[Attribute] class ArrayType { public function __construct( - public int $count, + public IntegerType $count, public mixed $elementType ) { } diff --git a/src/Generator/Type/Arrays/AssociativeArrayType.php b/src/Generator/Type/Arrays/AssociativeArrayType.php index c5babd8..36d28b2 100644 --- a/src/Generator/Type/Arrays/AssociativeArrayType.php +++ b/src/Generator/Type/Arrays/AssociativeArrayType.php @@ -11,7 +11,8 @@ class AssociativeArrayType public function __construct( public mixed $key, public mixed $value, - public IntegerType $count + public IntegerType $count, + public IntegerType $depth, ) { } }