Page MenuHomeWickedGov Phorge

AssociativeArrayGenerator.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

AssociativeArrayGenerator.php

<?php
namespace Eris\Generator;
use Eris\Generator;
use Eris\Generators;
use Eris\Random\RandomRange;
/**
* @return AssociativeArrayGenerator
*/
function associative(array $generators)
{
return Generators::associative($generators);
}
/**
* @template-implements Generator<array<string, mixed>>
*/
class AssociativeArrayGenerator implements Generator
{
private $generators;
private $tupleGenerator;
public function __construct(array $generators)
{
$this->generators = $generators;
$this->tupleGenerator = new TupleGenerator(array_values($generators));
}
public function __invoke($size, RandomRange $rand)
{
$tuple = $this->tupleGenerator->__invoke($size, $rand);
return $this->mapToAssociativeArray($tuple);
}
public function shrink(GeneratedValue $element)
{
$input = $element->input();
$shrunkInput = $this->tupleGenerator->shrink($input);
return $this->mapToAssociativeArray($shrunkInput);
}
private function mapToAssociativeArray(GeneratedValue $tuple)
{
return $tuple->map(
function ($value) {
$associativeArray = [];
$keys = array_keys($this->generators);
for ($i = 0, $iMax = count($value); $i < $iMax; $i++) {
$key = $keys[$i];
$associativeArray[$key] = $value[$i];
}
return $associativeArray;
},
'associative'
);
}
}

File Metadata

Mime Type
text/x-php
Expires
Oct 7 2026, 01:13 (6 w, 3 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
3a/f4/f8efe52710acfdd7ae9ee6d7d2e3
Default Alt Text
AssociativeArrayGenerator.php (1 KB)

Event Timeline