generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09b38e5
commit 40b231e
Showing
5 changed files
with
241 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support\Iterables\Concerns; | ||
|
||
use Spatie\LaravelData\Support\DataContainer; | ||
use Spatie\LaravelData\Support\Transformation\TransformationContextFactory; | ||
|
||
trait IterableData | ||
{ | ||
public function data( | ||
string $dataClass, | ||
array|string|null $include = null, | ||
array|string|null $exclude = null, | ||
array|string|null $only = null, | ||
array|string|null $except = null, | ||
array|string|null $includePermanently = null, | ||
array|string|null $excludePermanently = null, | ||
array|string|null $onlyPermanently = null, | ||
array|string|null $exceptPermanently = null, | ||
null|bool $wrap = null, | ||
bool|null $transformValues = null, | ||
bool|null $mapPropertyNames = null, | ||
TransformationContextFactory $factory = null | ||
): self { | ||
$factory = $factory ?? TransformationContextFactory::create(); | ||
|
||
if ($include) { | ||
$factory->include($include); | ||
} | ||
|
||
if ($exclude) { | ||
$factory->exclude($exclude); | ||
} | ||
|
||
if ($only) { | ||
$factory->only($only); | ||
} | ||
|
||
if ($except) { | ||
$factory->except($except); | ||
} | ||
|
||
if ($includePermanently) { | ||
$factory->includePermanently($includePermanently); | ||
} | ||
|
||
if ($excludePermanently) { | ||
$factory->excludePermanently($excludePermanently); | ||
} | ||
|
||
if ($onlyPermanently) { | ||
$factory->onlyPermanently($onlyPermanently); | ||
} | ||
|
||
if ($exceptPermanently) { | ||
$factory->exceptPermanently($exceptPermanently); | ||
} | ||
|
||
if ($wrap === true) { | ||
$factory->withWrapping(); | ||
} | ||
|
||
if ($wrap === false) { | ||
$factory->withoutWrapping(); | ||
} | ||
|
||
if ($transformValues !== null) { | ||
$factory->withValueTransformation($transformValues); | ||
} | ||
|
||
if ($mapPropertyNames !== null) { | ||
$factory->withPropertyNameMapping($mapPropertyNames); | ||
} | ||
|
||
$transformationContext = null; | ||
|
||
foreach ($this->items as $key => $value) { | ||
$this->items[$key] = $value instanceof $dataClass | ||
? $value | ||
: $dataClass::from($value); | ||
|
||
$transformationContext ??= $factory->create($this->items[$key]); | ||
|
||
|
||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return DataContainer::get() | ||
} | ||
Check failure on line 93 in src/Support/Iterables/Concerns/IterableData.php GitHub Actions / phpstan
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support\Iterables; | ||
|
||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Spatie\LaravelData\Support\Iterables\Concerns\IterableData; | ||
|
||
class LengthAwareDataPaginator extends LengthAwarePaginator | ||
{ | ||
use IterableData; | ||
Check failure on line 10 in src/Support/Iterables/LengthAwareDataPaginator.php GitHub Actions / phpstan
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
|
||
use function Pest\Laravel\get; | ||
use function Pest\Laravel\withExceptionHandling; | ||
|
||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Support\Iterables\LengthAwareDataPaginator; | ||
use Spatie\LaravelData\Tests\Fakes\SimpleData; | ||
|
||
it('can paginate data', function () { | ||
withExceptionHandling(); | ||
|
||
Route::get('/', function () { | ||
return (new LengthAwareDataPaginator(['Hello', 'World', 'Welcome'], 3, 1))->data(SimpleData::class); | ||
// | ||
// | ||
// return SimpleData::collect(["Hello", "World", "Weclome"], LengthAwareDataPaginator::class)->data( | ||
// include: 'single', | ||
// exclude: ['multiple', 'items'], | ||
// only: [ | ||
// 'callable' => fn(Data $data) => $data->property === 'something', | ||
// ], | ||
// includePermanently: 'permanently', | ||
// wrap: null, // default | ||
// // wrap: false // disabled | ||
// // wrap: 'key' // defined | ||
// transformValues: true, // | ||
// mapPropertyNames: true, | ||
// ); | ||
}); | ||
|
||
get('/')->dump(); | ||
}); |