Open robert-abram opened 3 months ago
+1
<?php
declare(strict_types=1);
namespace Momentum\Lock\Data;
use Illuminate\Contracts\Pagination\CursorPaginator as CursorPaginatorContract;
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\AbstractCursorPaginator;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use Momentum\Lock\Lock;
use Spatie\LaravelData\CursorPaginatedDataCollection;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\LaravelData\Support\Transformation\TransformationContext;
use Spatie\LaravelData\Support\Transformation\TransformationContextFactory;
class DataResource extends Data
{
protected ?Model $model;
/** @var null|array */
protected $permissions = null;
protected string $modelClass;
public static function from(mixed ...$payloads): static
{
/** @var static $data */
$data = parent::from(...$payloads);
if (count($payloads) === 1 && $payloads[0] instanceof Model) {
$data->setModel($payloads[0]);
}
return $data;
}
public static function collect(mixed $items, ?string $into = null): array|DataCollection|PaginatedDataCollection|CursorPaginatedDataCollection|Enumerable|AbstractPaginator|PaginatorContract|AbstractCursorPaginator|CursorPaginatorContract|LazyCollection|Collection
{
/** @var static $data */
$data = parent::collect($items, $into)->transform(function ($data, $key) use ($items) {
if ($items[$key] instanceof Model) {
$data->setModel($items[$key]);
}
return $data;
});
return $data;
}
protected function setModel(Model $model): static
{
$this->model = $model;
return $this;
}
protected function appendPermissions(): void
{
if (isset($this->model)) {
$this->additional([
'permissions' => Lock::getPermissions($this->model, $this->permissions),
]);
}
}
public function transform(
null|TransformationContextFactory|TransformationContext $transformationContext = null,
): array {
$this->appendPermissions();
return parent::transform($transformationContext);
}
}
@lepikhinb It's temporary solution...
Thank you @robert-abram for the solution. How can this work with a paginated response?
It's not working with collect method