field key => label */ public const FIELDS = [ 'guest.first_name' => 'Vorname', 'guest.last_name' => 'Nachname', 'guest.email' => 'E-Mail-Adresse', 'guest.street' => 'Straße und Hausnummer', 'guest.postal_code' => 'PLZ', 'guest.city' => 'Ort', 'guest.country' => 'Land', 'booking.guests' => 'Personenzahl', 'booking.arrival' => 'Anreise', 'booking.departure' => 'Abreise', ]; /** Fields the guest can give for a case (contact and stay details beyond the required ones). */ public const OPTIONAL_FIELDS = [ 'guest.phone' => 'Telefon', 'booking.arrival_time' => 'Voraussichtliche Anreisezeit', ]; /** * Required fields of the action that are missing or not valid. * * @return list field keys of {@see self::FIELDS} */ public static function missing(Booking $booking, string $action = self::OFFER): array { $missing = []; foreach (array_keys(self::FIELDS) as $field) { if (! self::isValid($field, self::value($booking, $field))) { $missing[] = $field; } } return $missing; } public static function isComplete(Booking $booking, string $action = self::OFFER): bool { return self::missing($booking, $action) === []; } /** * The blocker text for the action, or null when nothing is missing. */ public static function blocker(Booking $booking, string $action = self::OFFER): ?string { $missing = self::missing($booking, $action); if ($missing === []) { return null; } $what = $action === self::WISH_CONFIRMATION ? 'die verbindliche Bestätigung' : 'ein verbindliches Angebot'; return 'Für '.$what.' fehlen noch Angaben: '.implode(', ', self::labels($missing)).'.'; } /** * Whether an incomplete request may get the clarification hold (V1, E-033): property, unambiguous dates and an * e-mail address that can be written to. The free calendar is checked by the caller. */ public static function qualifiesForClarificationHold(Booking $booking): bool { return $booking->property_id !== null && $booking->arrival !== null && $booking->departure !== null && $booking->departure->greaterThan($booking->arrival) && self::isValid('guest.email', $booking->guest?->email); } /** * @param list $fields * @return list */ public static function labels(array $fields): array { return array_map(fn (string $field): string => self::FIELDS[$field] ?? self::OPTIONAL_FIELDS[$field] ?? $field, $fields); } public static function label(string $field): string { return self::labels([$field])[0]; } /** * Current value of a field of the case (guest fields from the guest record). */ public static function value(Booking $booking, string $field): mixed { [$scope, $attribute] = explode('.', $field, 2); $value = $scope === 'guest' ? $booking->guest?->{$attribute} : $booking->{$attribute}; return $value instanceof \DateTimeInterface ? $value->format('Y-m-d') : $value; } /** * Whether a value is usable for the field. */ public static function isValid(string $field, mixed $value): bool { if (is_string($value)) { $value = trim($value); } if ($value === null || $value === '') { return false; } return match ($field) { 'guest.email' => filter_var($value, FILTER_VALIDATE_EMAIL) !== false, 'guest.postal_code' => (bool) preg_match('/^[A-Za-z0-9][A-Za-z0-9 \-]{1,9}$/', (string) $value), 'guest.country' => (bool) preg_match('/^[A-Z]{2}$/', (string) $value), 'booking.guests' => is_numeric($value) && (int) $value >= 1, 'booking.arrival', 'booking.departure' => strtotime((string) $value) !== false, default => mb_strlen((string) $value) >= 1, }; } }