Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Internal/Extractors/IterableExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function __construct(private ReflectionExtractor $extractor)

public function extract(object $object): array
{
if ($object instanceof Traversable) {
return iterator_to_array($object);
}

$properties = $this->extractor->extractProperties(object: $object);

$candidates = array_filter(
Expand Down
27 changes: 27 additions & 0 deletions tests/CollectionMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Test\TinyBlocks\Mapper\Models\Description;
use Test\TinyBlocks\Mapper\Models\Employee;
use Test\TinyBlocks\Mapper\Models\Employees;
use Test\TinyBlocks\Mapper\Models\Invoice;
use Test\TinyBlocks\Mapper\Models\Invoices;
use Test\TinyBlocks\Mapper\Models\InvoiceSummaries;
use Test\TinyBlocks\Mapper\Models\Member;
use Test\TinyBlocks\Mapper\Models\MemberId;
use Test\TinyBlocks\Mapper\Models\Members;
Expand Down Expand Up @@ -89,6 +92,30 @@ public function testCollectionOfScalars(): void
self::assertSame('mixed', $attributes->getType());
}

public function testCollectionOfTraversable(): void
{
/** @Given an InvoiceSummaries collection with traversable invoices */
$invoiceSummaries = InvoiceSummaries::createFrom(
invoices: Invoices::createFrom(elements: [
new Invoice(id: 'INV001', amount: 100.0, customer: 'Customer A'),
new Invoice(id: 'INV002', amount: 150.5, customer: 'Customer B'),
new Invoice(id: 'INV003', amount: 200.75, customer: 'Customer C')
])
);

/** @When mapping the InvoiceSummaries collection to an array */
$actual = $invoiceSummaries->toArray();

/** @Then the mapped array should have expected values */
$expected = [
'INV001' => ['id' => 'INV001', 'amount' => 100.0, 'customer' => 'Customer A'],
'INV002' => ['id' => 'INV002', 'amount' => 150.5, 'customer' => 'Customer B'],
'INV003' => ['id' => 'INV003', 'amount' => 200.75, 'customer' => 'Customer C']
];

self::assertSame($expected, $actual);
}

#[DataProvider('collectionDiscardKeysProvider')]
public function testCollectionDiscardKeys(IterableMapper $collection, array $expected): void
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Models/Invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Test\TinyBlocks\Mapper\Models;

final readonly class Invoice
{
public function __construct(public string $id, public float $amount, public string $customer)
{
}
}
31 changes: 31 additions & 0 deletions tests/Models/InvoiceSummaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Test\TinyBlocks\Mapper\Models;

use IteratorAggregate;
use TinyBlocks\Mapper\IterableMappability;
use TinyBlocks\Mapper\IterableMapper;
use Traversable;

final class InvoiceSummaries implements IterableMapper, IteratorAggregate
{
use IterableMappability;

private function __construct(private readonly Invoices $invoices)
{
}

public static function createFrom(Invoices $invoices): InvoiceSummaries
{
return new InvoiceSummaries(invoices: $invoices);
}

public function getIterator(): Traversable
{
foreach ($this->invoices as $invoice) {
yield $invoice->id => $invoice;
}
}
}
29 changes: 29 additions & 0 deletions tests/Models/Invoices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Test\TinyBlocks\Mapper\Models;

use IteratorAggregate;
use TinyBlocks\Mapper\IterableMappability;
use TinyBlocks\Mapper\IterableMapper;
use Traversable;

final class Invoices implements IterableMapper, IteratorAggregate
{
use IterableMappability;

public function __construct(public array $elements)
{
}

public static function createFrom(array $elements): Invoices
{
return new Invoices(elements: $elements);
}

public function getIterator(): Traversable
{
return yield from $this->elements;
}
}