Skip to content

Commit

Permalink
AssociativeArrayGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyBel committed Jul 17, 2023
1 parent 76218aa commit b9f3ac1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion invariants/examples/generators/Arrays/ArrayInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/Generator/Generator/Arrays/ArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion src/Generator/Generator/Arrays/AssociativeArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,31 @@ public function __construct(
* @return array<mixed>
*/
public function __invoke(AssociativeArrayType $type): array
{
$depth = call_user_func($this->integerGenerator, $type->depth);
return $this->recursiveGenerate([], $type, $depth);
}

/**
* @param array<mixed> $current
* @return array<mixed>
*/
private function recursiveGenerate(array $current, AssociativeArrayType $type, int $depth): array
{
$count = call_user_func($this->integerGenerator, $type->count);
$keyGenerator = $this->generatorFactory->getGenerator($type->key);
$valueGenerator = $this->generatorFactory->getGenerator($type->value);

$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;

}

Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Generator/Float/FloatGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpInvariant\Generator\Generator\Float;

use PhpInvariant\Generator\Type\Float\FloatType;
use PhpInvariant\Random\Random;

class FloatGenerator
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Type/Arrays/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Type/Arrays/AssociativeArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class AssociativeArrayType
public function __construct(
public mixed $key,
public mixed $value,
public IntegerType $count
public IntegerType $count,
public IntegerType $depth,
) {
}
}

0 comments on commit b9f3ac1

Please sign in to comment.