'Ist gleich', 'not_equals' => 'Ist nicht gleich', 'starts_with' => 'Beginnt mit', 'ends_with' => 'Endet mit', 'contains' => 'Enthält', 'exists' => 'Ist vorhanden', 'empty' => 'Ist leer']; public const TYPES = ['unknown' => 'Nicht erkannt', 'portal_reservation' => 'Portalreservierung', 'block' => 'Allgemeine Sperre']; public const REPRESENTATIONS = ['unspecified' => 'Unbestimmt', 'leading_candidate' => 'Kandidat für führende Belegung', 'additional_block' => 'Zusätzliche Sperre']; public const TARGETS = ['guest_first_name' => 'Gastvorname / Namensbestandteil', 'guest_full_name' => 'Vollständiger Gastname', 'booking_reference' => 'Buchungsreferenz', 'occupation_label' => 'Belegungsbeschreibung', 'note' => 'Zusätzliche Textinformation']; public const EXTRACTIONS = ['whole' => 'Gesamter Text', 'after' => 'Text nach', 'before' => 'Text vor', 'between' => 'Text zwischen']; /** @return list */ public function values(array $properties, string $field): array { return array_map(fn (array $property): string => ICalParser::text($property['value']), $properties[$field] ?? []); } /** @return array */ public function evaluate(Channel $channel, array $properties, ?array $rules = null): array { $rules = collect($rules ?? $channel->ical_active_rules ?? [])->values() ->map(fn (array $rule, int $index): array => [...$rule, '_index' => $index]) ->sortBy(fn (array $rule): array => [(int) ($rule['priority'] ?? 100), $rule['_index']]); foreach ($rules as $rule) { if (! ($rule['enabled'] ?? true) || empty($rule['conditions'])) { continue; } $matches = array_map(fn (array $condition): bool => $this->matches($properties, $condition, (bool) ($rule['case_sensitive'] ?? false)), array_values($rule['conditions'])); if (($rule['match'] ?? 'all') === 'any' ? ! in_array(true, $matches, true) : in_array(false, $matches, true)) { continue; } $fields = []; foreach ($rule['extractions'] ?? [] as $extraction) { $value = $this->extract($this->values($properties, $extraction['field'] ?? '')[0] ?? '', $extraction); if (isset(self::TARGETS[$extraction['target'] ?? '']) && filled($value)) { $fields[$extraction['target']] = $value; } } $origin = $rule['origin'] ?? 'unknown'; return ['matched' => true, 'rule' => $rule['name'] ?? 'Unbenannte Regel', 'rule_index' => $rule['_index'], 'event_type' => $rule['event_type'] ?? 'unknown', 'representation' => $rule['representation'] ?? 'unspecified', 'priority' => (int) ($rule['priority'] ?? 100), 'origin_channel_id' => $origin === 'current' ? $channel->id : (str_starts_with($origin, 'channel:') ? (int) substr($origin, 8) : null), 'fields' => $fields]; } return ['matched' => false, 'rule' => null, 'event_type' => 'unknown', 'representation' => 'unspecified', 'origin_channel_id' => null, 'priority' => 0, 'fields' => []]; } private function matches(array $properties, array $condition, bool $caseSensitive): bool { $field = $condition['field'] ?? ''; if ($field === '') { return false; } $values = $this->values($properties, $field); $operator = $condition['operator'] ?? 'equals'; if ($operator === 'exists') { return array_key_exists($field, $properties); } if ($operator === 'empty') { return $values === [] || collect($values)->every(fn (string $value): bool => trim($value) === ''); } $expected = (string) ($condition['value'] ?? ''); if (! $caseSensitive) { $expected = mb_strtolower($expected); $values = array_map(mb_strtolower(...), $values); } if ($operator === 'not_equals') { return $values !== [] && ! in_array($expected, $values, true); } return collect($values)->contains(fn (string $value): bool => match ($operator) { 'equals' => $value === $expected, 'starts_with' => str_starts_with($value, $expected), 'ends_with' => str_ends_with($value, $expected), 'contains' => str_contains($value, $expected), default => false, }); } private function extract(string $text, array $extraction): ?string { $method = $extraction['method'] ?? 'whole'; if (in_array($method, ['after', 'between'], true)) { $start = (string) ($extraction['start'] ?? ''); $position = $start === '' ? false : mb_strpos($text, $start); if ($position === false) { return null; } $text = mb_substr($text, $position + mb_strlen($start)); } if (in_array($method, ['before', 'between'], true)) { $end = (string) ($extraction['end'] ?? ''); $position = $end === '' ? false : mb_strpos($text, $end); if ($position === false) { return null; } $text = mb_substr($text, 0, $position); } return trim($text); } /** @return list> */ public function validate(Channel $channel, array $rules): array { $fields = array_keys($channel->ical_analysis['fields'] ?? []); $origins = ['unknown', 'current', ...$channel->property->channels()->pluck('id')->map(fn ($id): string => 'channel:'.$id)->all()]; Validator::make(['rules' => $rules], [ 'rules' => ['array'], 'rules.*.name' => ['required', 'string', 'max:120'], 'rules.*.enabled' => ['boolean'], 'rules.*.priority' => ['required', 'integer', 'min:0', 'max:10000'], 'rules.*.match' => ['required', Rule::in(['all', 'any'])], 'rules.*.case_sensitive' => ['boolean'], 'rules.*.conditions' => ['required', 'array', 'min:1', 'max:30'], 'rules.*.conditions.*.field' => ['required', Rule::in($fields)], 'rules.*.conditions.*.operator' => ['required', Rule::in(array_keys(self::OPERATORS))], 'rules.*.conditions.*.value' => ['nullable', 'string', 'max:2000'], 'rules.*.event_type' => ['required', Rule::in(array_keys(self::TYPES))], 'rules.*.origin' => ['required', Rule::in($origins)], 'rules.*.representation' => ['required', Rule::in(array_keys(self::REPRESENTATIONS))], 'rules.*.extractions' => ['array', 'max:20'], 'rules.*.extractions.*.field' => ['required', Rule::in($fields)], 'rules.*.extractions.*.target' => ['required', Rule::in(array_keys(self::TARGETS))], 'rules.*.extractions.*.method' => ['required', Rule::in(array_keys(self::EXTRACTIONS))], 'rules.*.extractions.*.start' => ['nullable', 'string', 'max:500'], 'rules.*.extractions.*.end' => ['nullable', 'string', 'max:500'], ])->validate(); foreach ($rules as $index => $rule) { foreach ($rule['extractions'] ?? [] as $extraction) { if ((in_array($extraction['method'], ['after', 'between'], true) && blank($extraction['start'] ?? null)) || (in_array($extraction['method'], ['before', 'between'], true) && blank($extraction['end'] ?? null))) { throw ValidationException::withMessages(['rules.'.$index.'.extractions' => 'Für die Textextraktion die benötigten Anfangs- und Endtexte angeben.']); } } } return array_values($rules); } }