93 lines
3.7 KiB
PHP
93 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\DietType;
|
|
use App\Enums\IngredientUnit;
|
|
use App\Enums\MealPostType;
|
|
use App\Enums\MealPostVisibility;
|
|
use App\Http\Requests\Concerns\ModeratesUserContent;
|
|
use App\Models\MealPosts;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class MealPostsRequest extends FormRequest
|
|
{
|
|
use ModeratesUserContent;
|
|
|
|
public function rules(): array
|
|
{
|
|
$isCreating = $this->isMethod('post');
|
|
|
|
return [
|
|
'image' => [$isCreating ? 'required_without:image_url' : 'sometimes', 'nullable', 'image', 'max:4096'],
|
|
'image_url' => [$isCreating ? 'required_without:image' : 'sometimes', 'nullable', 'url:http,https', 'max:2048'],
|
|
'caption' => ['nullable', 'string', 'max:1000'],
|
|
'calories' => ['nullable', 'integer', 'min:0'],
|
|
'proteins' => ['nullable', 'numeric', 'min:0'],
|
|
'carbs' => ['nullable', 'numeric', 'min:0'],
|
|
'fats' => ['nullable', 'numeric', 'min:0'],
|
|
'title' => [$isCreating ? 'required' : 'sometimes', 'string', 'max:255'],
|
|
'eaten_at' => [$isCreating ? 'required' : 'sometimes', 'date'],
|
|
'type' => [$isCreating ? 'required' : 'sometimes', Rule::enum(MealPostType::class)],
|
|
'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)],
|
|
'diet_type' => ['sometimes', Rule::enum(DietType::class)],
|
|
'ingredients' => ['sometimes', 'array'],
|
|
'ingredients.*' => ['required', 'array:position,ingredient,quantity,unit'],
|
|
'ingredients.*.position' => ['required', 'integer', 'min:0'],
|
|
'ingredients.*.ingredient' => ['required', 'string', 'max:255'],
|
|
'ingredients.*.quantity' => ['required', 'integer', 'min:1'],
|
|
'ingredients.*.unit' => ['required', Rule::enum(IngredientUnit::class)],
|
|
];
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
$mealPost = $this->route('mealPost');
|
|
$currentVisibility = $mealPost instanceof MealPosts
|
|
? $mealPost->visibility
|
|
: MealPostVisibility::Private;
|
|
$visibility = MealPostVisibility::tryFrom((string) $this->input('visibility')) ?? $currentVisibility;
|
|
|
|
if ($visibility !== MealPostVisibility::Public) {
|
|
return [];
|
|
}
|
|
|
|
$isBecomingPublic = $mealPost instanceof MealPosts
|
|
&& $mealPost->visibility !== MealPostVisibility::Public;
|
|
$ingredients = $this->has('ingredients')
|
|
? collect($this->input('ingredients', []))->pluck('ingredient')->all()
|
|
: ($isBecomingPublic ? $mealPost->ingredients()->pluck('ingredient')->all() : []);
|
|
$existingImageUrl = $isBecomingPublic && filled($mealPost->image_url)
|
|
? (Str::startsWith($mealPost->image_url, ['http://', 'https://'])
|
|
? $mealPost->image_url
|
|
: asset(Storage::url($mealPost->image_url)))
|
|
: null;
|
|
|
|
return $this->moderationChecks(
|
|
[
|
|
$this->has('title') ? $this->input('title') : ($isBecomingPublic ? $mealPost->title : null),
|
|
$this->has('caption') ? $this->input('caption') : ($isBecomingPublic ? $mealPost->caption : null),
|
|
$ingredients,
|
|
],
|
|
[$this->file('image'), $this->input('image_url'), $existingImageUrl],
|
|
);
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'image.max' => __('api.validation.image_max_4mb'),
|
|
'image_url.required_without' => __('api.validation.meal_image_required'),
|
|
'visibility' => __('api.validation.visibility_valid'),
|
|
];
|
|
}
|
|
}
|