Files

81 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateRatingRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$config = config('restaurant-delivery.rating');
$minRating = $config['min_rating'] ?? 1;
$maxRating = $config['max_rating'] ?? 5;
$maxReviewLength = $config['review_max_length'] ?? 500;
return [
'overall_rating' => ['required', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'speed_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'communication_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'food_condition_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'professionalism_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'review' => ['nullable', 'string', "max:{$maxReviewLength}"],
'tags' => ['nullable', 'array'],
'tags.*' => ['string', 'max:50'],
'is_anonymous' => ['boolean'],
'is_restaurant_rating' => ['boolean'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'overall_rating' => 'overall rating',
'speed_rating' => 'speed rating',
'communication_rating' => 'communication rating',
'food_condition_rating' => 'food condition rating',
'professionalism_rating' => 'professionalism rating',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'is_anonymous' => $this->boolean('is_anonymous'),
'is_restaurant_rating' => $this->boolean('is_restaurant_rating'),
]);
}
/**
* Get validated category ratings as array.
*/
public function categoryRatings(): array
{
return array_filter([
'speed' => $this->validated('speed_rating'),
'communication' => $this->validated('communication_rating'),
'food_condition' => $this->validated('food_condition_rating'),
'professionalism' => $this->validated('professionalism_rating'),
]);
}
}