37 lines
1.1 KiB
PHP
37 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Models\Breeder;
|
|
use App\Models\Recipe;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
|
class UriResource extends ResourceCollection
|
|
{
|
|
/**
|
|
* Transform the resource collection into an array.
|
|
*
|
|
* @param mixed $request
|
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function toArray($request)
|
|
{
|
|
/** @var \Illuminate\Support\Collection $collection */
|
|
$collection = $this->resource;
|
|
return $collection->map(function (Model $model) {
|
|
switch (\get_class($model)) {
|
|
case Breeder::class:
|
|
return new BreederUriResource($model);
|
|
case Recipe::class:
|
|
return new RecipeUriResource($model);
|
|
default:
|
|
throw new \RuntimeException(sprintf(
|
|
'Unhandled model %s',
|
|
\get_class($model)
|
|
));
|
|
}
|
|
});
|
|
}
|
|
}
|