diff --git a/src/Integrations/Divi/ThemeBuilder/TemplateConditions.php b/src/Integrations/Divi/ThemeBuilder/TemplateConditions.php new file mode 100644 index 0000000..7e32d6f --- /dev/null +++ b/src/Integrations/Divi/ThemeBuilder/TemplateConditions.php @@ -0,0 +1,193 @@ +|null */ + private ?array $flat = null; + + public function __construct(private DiviThemeBuilder $store) + { + } + + /** + * Checks a client's condition list for field $field: null = no conditions; errors are keyed "" + * (the list itself) or ".". Duplicates are dropped. + * + * @return array{ids: list, errors: array} + */ + public function check(mixed $conditions, string $field): array + { + if ($conditions === null) { + return ['ids' => [], 'errors' => []]; + } + + if (! is_array($conditions) || ! array_is_list($conditions)) { + return ['ids' => [], 'errors' => [$field => 'Liste von Bedingungs-IDs erwartet']]; + } + + if (count($conditions) > self::MAX) { + return ['ids' => [], 'errors' => [$field => sprintf('höchstens %d Bedingungen', self::MAX)]]; + } + + $ids = []; + $errors = []; + foreach ($conditions as $index => $condition) { + $problem = is_string($condition) ? $this->problem($condition) : 'Bedingungs-ID als Text erwartet'; + if ($problem === null) { + $ids[] = (string) $condition; + } else { + $errors[$field . '.' . $index] = $problem; + } + } + + return ['ids' => array_values(array_unique($ids)), 'errors' => $errors]; + } + + /** Divi's label, for an ID with object "