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())] ), ], 'media' => ['sometimes', 'array'], 'media.*.id' => ['required', 'string'], 'media.*.path' => ['required', 'string', 'max:500'], 'media.*.url' => ['required', 'string', 'max:2048'], 'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'], 'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'], 'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'], 'media.*.size' => ['sometimes', 'nullable', 'integer'], 'media.*.meta' => ['sometimes', 'nullable', 'array'], 'media.*.source' => ['sometimes', 'nullable', 'string', Rule::in(array_column(Source::cases(), 'value'))], 'media.*.source_meta' => ['sometimes', 'nullable', 'array'], '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 */ 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'); } }