Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| RandomRecords | |
100.00% |
31 / 31 |
|
100.00% |
7 / 7 |
22 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getColumnNames | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getColumns | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getRowCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| fakeValue | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
13 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\Tabular; |
| 3 | |
| 4 | use DateTimeImmutable; |
| 5 | use Faker\Generator; |
| 6 | use IteratorAggregate; |
| 7 | use Traversable; |
| 8 | |
| 9 | /** |
| 10 | * Iterates over a random number of fake records with a random set of columns. |
| 11 | * The columns are chosen at construction, the records are generated lazily while iterating. |
| 12 | * |
| 13 | * @implements IteratorAggregate<int, array<string, string|int|float|DateTimeImmutable>> |
| 14 | */ |
| 15 | final class RandomRecords implements IteratorAggregate |
| 16 | { |
| 17 | /** @var array<string, ColumnType> */ |
| 18 | private const COLUMNS = [ |
| 19 | 'Name' => ColumnType::Text, |
| 20 | 'Email' => ColumnType::Text, |
| 21 | 'City' => ColumnType::Text, |
| 22 | 'Company' => ColumnType::Text, |
| 23 | 'Country' => ColumnType::Text, |
| 24 | 'Phone' => ColumnType::Text, |
| 25 | 'Quantity' => ColumnType::Integer, |
| 26 | 'Age' => ColumnType::Integer, |
| 27 | 'Price' => ColumnType::Decimal, |
| 28 | 'Rating' => ColumnType::Decimal, |
| 29 | 'Created at' => ColumnType::Date, |
| 30 | 'Birthday' => ColumnType::Date, |
| 31 | ]; |
| 32 | |
| 33 | /** @var array<int, string> */ |
| 34 | private array $columnNames; |
| 35 | |
| 36 | public function __construct( |
| 37 | private readonly Generator $faker, |
| 38 | private readonly int $rowCount, |
| 39 | int $minColumns = 2, |
| 40 | int $maxColumns = 8 |
| 41 | ) { |
| 42 | $this->columnNames = $faker->randomElements( |
| 43 | array_keys(self::COLUMNS), |
| 44 | $faker->numberBetween($minColumns, min($maxColumns, count(self::COLUMNS))) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | public static function create(Generator $faker, int $minRows = 5, int $maxRows = 60): self |
| 49 | { |
| 50 | return new self($faker, $faker->numberBetween($minRows, $maxRows)); |
| 51 | } |
| 52 | |
| 53 | /** @return array<int, string> */ |
| 54 | public function getColumnNames(): array |
| 55 | { |
| 56 | return $this->columnNames; |
| 57 | } |
| 58 | |
| 59 | /** @return array<string, ColumnType> */ |
| 60 | public function getColumns(): array |
| 61 | { |
| 62 | $result = []; |
| 63 | foreach ($this->columnNames as $name) { |
| 64 | $result[$name] = self::COLUMNS[$name]; |
| 65 | } |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | public function getRowCount(): int |
| 70 | { |
| 71 | return $this->rowCount; |
| 72 | } |
| 73 | |
| 74 | /** @return Traversable<int, array<string, string|int|float|DateTimeImmutable>> */ |
| 75 | public function getIterator(): Traversable |
| 76 | { |
| 77 | for ($i = 0; $i < $this->rowCount; $i++) { |
| 78 | $record = []; |
| 79 | foreach ($this->columnNames as $name) { |
| 80 | $record[$name] = $this->fakeValue($name); |
| 81 | } |
| 82 | yield $i => $record; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private function fakeValue(string $column): string|int|float|DateTimeImmutable |
| 87 | { |
| 88 | $faker = $this->faker; |
| 89 | return match ($column) { |
| 90 | 'Name' => $faker->name(), |
| 91 | 'Email' => $faker->safeEmail(), |
| 92 | 'City' => $faker->city(), |
| 93 | 'Company' => $faker->company(), |
| 94 | 'Country' => $faker->country(), |
| 95 | 'Phone' => $faker->phoneNumber(), |
| 96 | 'Quantity' => $faker->numberBetween(1, 5000), |
| 97 | 'Age' => $faker->numberBetween(18, 90), |
| 98 | 'Price' => $faker->randomFloat(2, 1, 2500), |
| 99 | 'Rating' => $faker->randomFloat(1, 1, 10), |
| 100 | 'Birthday' => DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-80 years', '-18 years')), |
| 101 | default => DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-3 years')), |
| 102 | }; |
| 103 | } |
| 104 | } |