Files
wticreatorstudio/Modules/StudioPlus/Transformers/AmazonASINResource.php
T
2024-02-12 22:54:20 -05:00

42 lines
1.2 KiB
PHP

<?php
namespace Modules\StudioPlus\Transformers;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Resources\Json\JsonResource;
class AmazonASINResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$product = $this;
$title = Arr::first(explode(',', $product['title']));
$brand = Arr::get($product, 'brand') ?? null;
if (Str::startsWith($brand, 'Visit the')) {
$newBrand = Str::replace('Visit the', '', $brand);
}
if (Str::startsWith($brand, 'Brand:')) {
$newBrand = Str::replace('Brand:', '', $brand);
}
return [
'product_name' => $title ?? null,
'studio_plus_category_id' => $product['category_id'] ?? null,
'amazon_total_reviews' => Arr::get($product, 'amazon_total_reviews') ?? null,
'price' => Arr::get($product, 'price', 0),
'brand' => $newBrand ?? $brand,
'amazon_url' => null,
'exists' => $this['exists']
];
}
}