Skip to content

Instantly share code, notes, and snippets.

@Sekaiichi
Created December 6, 2021 09:50
Show Gist options
  • Save Sekaiichi/c0554cad1c1b401ac9cfa93281c23703 to your computer and use it in GitHub Desktop.
Save Sekaiichi/c0554cad1c1b401ac9cfa93281c23703 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Resources\Client\WidgetProduct;
use App\Http\Resources\Client\City\CityResource;
use App\Http\Resources\Client\WidgetLocation\WidgetLocationResource;
use App\Models\WidgetProduct;
use App\Services\Products\ProductService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
class WidgetProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request): array
{
$getDataBySource = $this->getDataBySource();
if ($getDataBySource->isNotEmpty()) {
return [
'id' => $this->id,
'name' => $this->name,
'title' => $this->title,
'title_hide' => $this->title_hide,
'city' => new CityResource($this->city),
'widget_source' => WidgetProduct::WIDGET_SOURCE_ARRAY[$this->widget_source],
'widget_items' => $getDataBySource,
'widget_location' => new WidgetLocationResource($this->widgetLocation),
'view_all_url' => $this->view_all_url,
'status' => WidgetProduct::STATUS_ARRAY[$this->status],
'position' => $this->position
];
}
return [];
}
public function getDataBySource(): Collection
{
return (new ProductService())->getData($this->widget_items->toArray(), $this->city_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment