diff --git a/src/DataCollection.php b/src/DataCollection.php index c6be7590..6222b70b 100644 --- a/src/DataCollection.php +++ b/src/DataCollection.php @@ -13,6 +13,7 @@ use Spatie\LaravelData\Concerns\WrappableData; use Spatie\LaravelData\Contracts\BaseData; use Spatie\LaravelData\Contracts\DataCollectable; +use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract; use Spatie\LaravelData\Exceptions\CannotCastData; use Spatie\LaravelData\Exceptions\InvalidDataCollectionOperation; use Spatie\LaravelData\Support\EloquentCasts\DataCollectionEloquentCast; @@ -101,7 +102,13 @@ public function offsetGet($offset): mixed throw InvalidDataCollectionOperation::create(); } - return $this->items->offsetGet($offset); + $data = $this->items->offsetGet($offset); + + if($data instanceof IncludeableDataContract){ + $data->withPartialTrees($this->getPartialTrees()); + } + + return $data; } /** diff --git a/tests/DataCollectionTest.php b/tests/DataCollectionTest.php index 174b4a4e..f2b0f24f 100644 --- a/tests/DataCollectionTest.php +++ b/tests/DataCollectionTest.php @@ -10,12 +10,13 @@ use Spatie\LaravelData\DataCollection; use Spatie\LaravelData\PaginatedDataCollection; use Spatie\LaravelData\Support\PartialTrees; +use Spatie\LaravelData\Tests\Fakes\ComplicatedData; use Spatie\LaravelData\Tests\Fakes\DataCollections\CustomDataCollection; use Spatie\LaravelData\Tests\Fakes\DataCollections\CustomPaginatedDataCollection; use Spatie\LaravelData\Tests\Fakes\DefaultLazyData; use Spatie\LaravelData\Tests\Fakes\LazyData; +use Spatie\LaravelData\Tests\Fakes\MultiData; use Spatie\LaravelData\Tests\Fakes\SimpleData; - use function Spatie\Snapshots\assertMatchesJsonSnapshot; use function Spatie\Snapshots\assertMatchesSnapshot; @@ -116,7 +117,11 @@ expect($letters)->toMatchArray(['A', 'B', 'C', 'D']); }); -it('has array access', function (DataCollection $collection) { +it('has array access', function () { + $collection = SimpleData::collection([ + 'A', 'B', SimpleData::from('C'), SimpleData::from('D'), + ]); + // Count expect($collection)->toHaveCount(4); @@ -126,9 +131,9 @@ expect(empty($collection[5]))->toBeTrue(); // Offset get - expect(SimpleData::from('A'))->toEqual($collection[0]); + expect($collection[0])->toEqual(SimpleData::from('A')->withPartialTrees(new PartialTrees())); - expect(SimpleData::from('D'))->toEqual($collection[3]); + expect($collection[3])->toEqual(SimpleData::from('D')->withPartialTrees(new PartialTrees())); if ($collection->items() instanceof AbstractPaginator || $collection->items() instanceof CursorPaginator) { return; @@ -138,17 +143,24 @@ $collection[2] = 'And now something completely different'; $collection[4] = 'E'; - expect( - SimpleData::from('And now something completely different') - ) - ->toEqual($collection[2]); - expect(SimpleData::from('E'))->toEqual($collection[4]); + expect($collection[2])->toEqual( + SimpleData::from('And now something completely different')->withPartialTrees(new PartialTrees()) + ); + expect($collection[4])->toEqual(SimpleData::from('E')->withPartialTrees(new PartialTrees())); // Offset unset unset($collection[4]); expect($collection)->toHaveCount(4); -})->with('array-access-collections'); +}); + +it('has array access and will replicate partialtrees', function () { + $collection = MultiData::collection([ + new MultiData('first', 'second'), + ])->only('second'); + + expect($collection[0]->toArray())->toEqual(['second' => 'second']); +}); it('can dynamically include data based upon the request', function () { LazyData::$allowedIncludes = ['']; diff --git a/tests/DataTest.php b/tests/DataTest.php index 8fbbfe15..0ee2fe50 100644 --- a/tests/DataTest.php +++ b/tests/DataTest.php @@ -67,7 +67,6 @@ use Spatie\LaravelData\Tests\Fakes\UnionData; use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer; use Spatie\LaravelData\WithData; - use function Spatie\Snapshots\assertMatchesSnapshot; it('can create a resource', function () { @@ -2461,3 +2460,4 @@ public function __construct( yield 'no params' => [[], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 0 given. Parameters missing: first, second.'], yield 'one param' => [['first' => 'First'], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 1 given. Parameters given: first. Parameters missing: second.'], ]); + diff --git a/tests/Datasets/DataCollection.php b/tests/Datasets/DataCollection.php index 0007b3a7..73c1be60 100644 --- a/tests/Datasets/DataCollection.php +++ b/tests/Datasets/DataCollection.php @@ -2,19 +2,6 @@ use Spatie\LaravelData\Tests\Fakes\SimpleData; -dataset('array-access-collections', function () { - yield "array" => [ - fn () => SimpleData::collection([ - 'A', 'B', SimpleData::from('C'), SimpleData::from('D'), - ]), - ]; - - yield "collection" => [ - fn () => SimpleData::collection([ - 'A', 'B', SimpleData::from('C'), SimpleData::from('D'), - ]), - ]; -}); dataset('collection-operations', function () { yield [