$changes * @return list, * title: string, * message: list * }> */ public function format(Collection $changes): array { return collect([ $this->suspensionNotice( $changes->where('change_type', EventDateChangeType::Suspended) ), $this->rescheduleNotice( $changes->where('change_type', EventDateChangeType::Rescheduled) ), ])->filter()->values()->all(); } /** * @param Collection $changes * @return array{type: string, change_ids: list, title: string, message: list}|null */ private function suspensionNotice(Collection $changes): ?array { $dates = $this->formatDates($changes, 'previous_date'); if ($dates === null) { return null; } $plural = $changes->count() > 1; return [ 'type' => EventDateChangeType::Suspended->value, 'change_ids' => $this->changeIds($changes), 'title' => $plural ? 'FECHAS CANCELADAS!' : 'FECHA CANCELADA!', 'message' => [ ['text' => $plural ? 'Las fechas del ' : 'La fecha del ', 'bold' => false], ['text' => $dates, 'bold' => true], ['text' => $plural ? ' han sido canceladas.' : ' ha sido cancelada.', 'bold' => false], ], ]; } /** * @param Collection $changes * @return array{type: string, change_ids: list, title: string, message: list}|null */ private function rescheduleNotice(Collection $changes): ?array { $changes = $changes->whereNotNull('new_date'); $sourceDates = $this->formatDates($changes, 'previous_date'); $destinationDates = $this->formatDates($changes, 'new_date'); if ($sourceDates === null || $destinationDates === null) { return null; } $plural = $changes->count() > 1; $message = [ ['text' => $plural ? 'Las fechas del ' : 'La fecha del ', 'bold' => false], ['text' => $sourceDates, 'bold' => true], [ 'text' => $plural ? ' han sido reprogramadas para el ' : ' ha sido reprogramada para el ', 'bold' => false, ], ['text' => $destinationDates, 'bold' => true], ]; if ($plural) { $message[] = ['text' => ', ', 'bold' => false]; $message[] = ['text' => 'respectivamente', 'bold' => true]; } $message[] = ['text' => '.', 'bold' => false]; return [ 'type' => EventDateChangeType::Rescheduled->value, 'change_ids' => $this->changeIds($changes), 'title' => $plural ? 'FECHAS REPROGRAMADAS!' : 'FECHA REPROGRAMADA!', 'message' => $message, ]; } /** * @param Collection $changes */ private function formatDates(Collection $changes, string $attribute): ?string { return $this->dateTextFormatter->formatForSentence( $changes ->pluck($attribute) ->filter() ->map(fn ($date): string => $date->format('Y-m-d')) ); } /** * @param Collection $changes * @return list */ private function changeIds(Collection $changes): array { return $changes ->pluck('id') ->filter(fn ($id): bool => $id !== null) ->map(fn ($id): int => (int) $id) ->values() ->all(); } }