The media.* rules were duplicated across the web update request and both API requests (and diverged: web requires hosted id+path and tracks source; the API accepts a bare external url it downloads). Pull them into one App\Support\PostMediaRules::rules(hosted:) — same pattern as PostPlatformMetaRules — parameterized by contract, so there's a single place to add a media key and the validated()-strips-unlisted-keys footgun can't drift between entry points. Behavior is unchanged (each ruleset is reproduced exactly). Web store keeps its loose 'media' => array (no item rules) and is left out on purpose — adding strict rules there would change the web create contract.
116 lines
3.7 KiB
PHP
116 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\App\Post;
|
|
|
|
use App\Enums\Post\Status;
|
|
use App\Enums\PostPlatform\ContentType;
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Rules\ContentFitsPlatformLimits;
|
|
use App\Rules\ContentTypeCompatibleWithMedia;
|
|
use App\Support\PostMediaRules;
|
|
use App\Support\PostPlatformMetaRules;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpdatePostRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$enforcesMediaCompatibility = in_array(
|
|
$this->input('status'),
|
|
[Status::Scheduled->value, Status::Publishing->value],
|
|
true,
|
|
);
|
|
|
|
return [
|
|
'status' => ['required', 'string', Rule::in([Status::Draft->value, Status::Scheduled->value, Status::Publishing->value])],
|
|
'content' => [
|
|
'nullable',
|
|
'string',
|
|
'max:10000',
|
|
Rule::when(
|
|
$enforcesMediaCompatibility,
|
|
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms())]
|
|
),
|
|
],
|
|
...PostMediaRules::rules(hosted: true),
|
|
'scheduled_at' => [
|
|
'sometimes',
|
|
'nullable',
|
|
'date',
|
|
Rule::when(
|
|
$this->input('status') === Status::Scheduled->value,
|
|
['after:now']
|
|
),
|
|
],
|
|
'platforms' => ['sometimes', 'array'],
|
|
'platforms.*.id' => ['required', 'uuid', Rule::exists('post_platforms', 'id')->where('post_id', $this->route('post')->id)],
|
|
'platforms.*.content_type' => [
|
|
$enforcesMediaCompatibility ? 'required' : 'sometimes',
|
|
'string',
|
|
Rule::in(array_column(ContentType::cases(), 'value')),
|
|
Rule::when($enforcesMediaCompatibility, [new ContentTypeCompatibleWithMedia]),
|
|
],
|
|
...PostPlatformMetaRules::rules(),
|
|
'label_ids' => ['sometimes', 'array'],
|
|
'label_ids.*' => ['uuid', Rule::exists('workspace_labels', 'id')->where('workspace_id', $this->user()->currentWorkspace->id)],
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function ($validator): void {
|
|
if (! $this->isPublishingOrScheduling()) {
|
|
return;
|
|
}
|
|
|
|
$platforms = $this->input('platforms', []);
|
|
$ids = collect($platforms)->pluck('id')->filter()->all();
|
|
|
|
$platformsById = $this->route('post')
|
|
->postPlatforms()
|
|
->whereIn('id', $ids)
|
|
->pluck('platform', 'id');
|
|
|
|
PostPlatformMetaRules::addRequiredOnPublishErrors(
|
|
$validator,
|
|
$platforms,
|
|
fn ($platform) => $platformsById[data_get($platform, 'id')] ?? null,
|
|
);
|
|
});
|
|
}
|
|
|
|
private function isPublishingOrScheduling(): bool
|
|
{
|
|
return in_array(
|
|
$this->input('status'),
|
|
[Status::Scheduled->value, Status::Publishing->value],
|
|
true,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int|string, Platform>
|
|
*/
|
|
private function resolveSelectedPlatforms(): Collection
|
|
{
|
|
$ids = collect($this->input('platforms', []))->pluck('id')->filter()->all();
|
|
if (empty($ids)) {
|
|
return collect();
|
|
}
|
|
|
|
return $this->route('post')
|
|
->postPlatforms()
|
|
->whereIn('id', $ids)
|
|
->pluck('platform', 'id');
|
|
}
|
|
}
|