Skip to content

Commit

Permalink
Merge pull request #213 from erikgaal/patch-generic-data-collection-t…
Browse files Browse the repository at this point in the history
…ypes

feat: Use generics with `Data::collection`
  • Loading branch information
rubenvanassche authored Sep 28, 2022
2 parents fa4a65c + 4cfb10b commit 5dff9ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 41 deletions.
21 changes: 6 additions & 15 deletions src/Contracts/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,20 @@
use Spatie\LaravelData\PaginatedDataCollection;

/**
* @template TValue of object
* @template TValue
*/
interface BaseData
{
/**
* @return TValue|null
*/
public static function optional(mixed ...$payloads): ?object;
public static function optional(mixed ...$payloads): ?static;

/**
* @return TValue
*/
public static function from(mixed ...$payloads): object;
public static function from(mixed ...$payloads): static;

/**
* @return TValue
*/
public static function withoutMagicalCreationFrom(mixed ...$payloads): object;
public static function withoutMagicalCreationFrom(mixed ...$payloads): static;

/**
* @param \Illuminate\Support\Enumerable|array|\Illuminate\Pagination\AbstractPaginator|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Pagination\AbstractCursorPaginator|\Illuminate\Contracts\Pagination\CursorPaginator|\Spatie\LaravelData\DataCollection $items
* @param \Illuminate\Support\Enumerable<array-key, TValue>|TValue[]|\Illuminate\Pagination\AbstractPaginator|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Pagination\AbstractCursorPaginator|\Illuminate\Contracts\Pagination\CursorPaginator|\Spatie\LaravelData\DataCollection<array-key, TValue> $items
*
* @return ($items is \Illuminate\Pagination\AbstractCursorPaginator|\Illuminate\Pagination\CursorPaginator ? \Spatie\LaravelData\CursorPaginatedDataCollection : ($items is \Illuminate\Pagination\Paginator|\Illuminate\Pagination\AbstractPaginator ? \Spatie\LaravelData\PaginatedDataCollection : \Spatie\LaravelData\DataCollection))
* @return ($items is \Illuminate\Pagination\AbstractCursorPaginator|\Illuminate\Pagination\CursorPaginator ? \Spatie\LaravelData\CursorPaginatedDataCollection<array-key, static> : ($items is \Illuminate\Pagination\Paginator|\Illuminate\Pagination\AbstractPaginator ? \Spatie\LaravelData\PaginatedDataCollection<array-key, static> : \Spatie\LaravelData\DataCollection<array-key, static>))
*/
public static function collection(Enumerable|array|AbstractPaginator|Paginator|AbstractCursorPaginator|CursorPaginator|DataCollection $items): DataCollection|CursorPaginatedDataCollection|PaginatedDataCollection;

Expand Down
32 changes: 6 additions & 26 deletions types/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,26 @@

/** @noinspection PhpExpressionResultUnusedInspection */

use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

use Spatie\LaravelData\CursorPaginatedDataCollection;
use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

// Regular collections
$collection = SimpleData::collection(['A', 'B']);
assertType(DataCollection::class, $collection);
assertType('Spatie\LaravelData\DataCollection<(int|string), Spatie\LaravelData\Tests\Fakes\SimpleData>', $collection);
$collection = SimpleData::collection(collect(['A', 'B']));
assertType(DataCollection::class, $collection);

assertType('Spatie\LaravelData\DataCollection<(int|string), Spatie\LaravelData\Tests\Fakes\SimpleData>', $collection);

// PaginatedDataCollection
$items = Collection::times(100, fn (int $index) => "Item {$index}");

$paginator = new LengthAwarePaginator(
$items->forPage(1, 15),
100,
15
);
$paginator = \Illuminate\Database\Eloquent\Model::query()->paginate();

$collection = SimpleData::collection($paginator);

assertType(PaginatedDataCollection::class, $collection);
assertType('Spatie\LaravelData\PaginatedDataCollection<(int|string), Spatie\LaravelData\Tests\Fakes\SimpleData>', $collection);

// CursorPaginatedDataCollection
$items = Collection::times(100, fn (int $index) => "Item {$index}");

$paginator = new CursorPaginator(
$items,
15,
);
$paginator = \Illuminate\Database\Eloquent\Model::query()->cursorPaginate();

$collection = SimpleData::collection($paginator);

assertType(CursorPaginatedDataCollection::class, $collection);
assertType('Spatie\LaravelData\CursorPaginatedDataCollection<(int|string), Spatie\LaravelData\Tests\Fakes\SimpleData>', $collection);

0 comments on commit 5dff9ec

Please sign in to comment.