75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\RestaurantDelivery\DTOs;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
readonly class RatingDTO
|
|
{
|
|
public function __construct(
|
|
public int $overallRating,
|
|
public ?int $speedRating = null,
|
|
public ?int $communicationRating = null,
|
|
public ?int $foodConditionRating = null,
|
|
public ?int $professionalismRating = null,
|
|
public ?string $review = null,
|
|
public array $tags = [],
|
|
public bool $isAnonymous = false,
|
|
public bool $isRestaurantRating = false,
|
|
public ?int $customerId = null,
|
|
) {}
|
|
|
|
/**
|
|
* Create DTO from request.
|
|
*/
|
|
public static function fromRequest(Request $request): self
|
|
{
|
|
return new self(
|
|
overallRating: $request->integer('overall_rating'),
|
|
speedRating: $request->filled('speed_rating') ? $request->integer('speed_rating') : null,
|
|
communicationRating: $request->filled('communication_rating') ? $request->integer('communication_rating') : null,
|
|
foodConditionRating: $request->filled('food_condition_rating') ? $request->integer('food_condition_rating') : null,
|
|
professionalismRating: $request->filled('professionalism_rating') ? $request->integer('professionalism_rating') : null,
|
|
review: $request->filled('review') ? $request->string('review')->toString() : null,
|
|
tags: $request->input('tags', []),
|
|
isAnonymous: $request->boolean('is_anonymous'),
|
|
isRestaurantRating: $request->boolean('is_restaurant_rating'),
|
|
customerId: $request->filled('customer_id') ? $request->integer('customer_id') : null,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get category ratings as array.
|
|
*/
|
|
public function categoryRatings(): array
|
|
{
|
|
return array_filter([
|
|
'speed' => $this->speedRating,
|
|
'communication' => $this->communicationRating,
|
|
'food_condition' => $this->foodConditionRating,
|
|
'professionalism' => $this->professionalismRating,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Convert to array.
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'overall_rating' => $this->overallRating,
|
|
'speed_rating' => $this->speedRating,
|
|
'communication_rating' => $this->communicationRating,
|
|
'food_condition_rating' => $this->foodConditionRating,
|
|
'professionalism_rating' => $this->professionalismRating,
|
|
'review' => $this->review,
|
|
'tags' => $this->tags,
|
|
'is_anonymous' => $this->isAnonymous,
|
|
'is_restaurant_rating' => $this->isRestaurantRating,
|
|
'customer_id' => $this->customerId,
|
|
];
|
|
}
|
|
}
|