From 7d0aedde2789ad91eba36552dd88bb76e6af72f7 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 20 Mar 2024 09:49:40 +0100 Subject: [PATCH] Undo BC break for nette/component-model < 3.1.0 Partly reverts the parent commit. --- composer.json | 3 +-- src/Multiplier.php | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 45d201e..c83fef2 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,7 @@ "webchemistry/testing-helpers": "^4.0.0" }, "conflict": { - "latte/latte": "<3.0.0", - "nette/component-model": "<3.1.0" + "latte/latte": "<3.0.0" }, "autoload": { "psr-4": { diff --git a/src/Multiplier.php b/src/Multiplier.php index 2a63855..04efd0a 100644 --- a/src/Multiplier.php +++ b/src/Multiplier.php @@ -168,8 +168,10 @@ public function addCreateButton(?string $caption = null, int $copyCount = 1): Cr */ public function validate(?array $controls = null): void { - /** @var Control[] $components */ + /** @var iterable $components */ $components = $controls ?? $this->getComponents(); + // Support both component-model ≥ 3.1 and < 3.1 + $components = is_array($components) ? $components : iterator_to_array($components); foreach ($components as $index => $control) { foreach ($this->noValidate as $item) { @@ -302,16 +304,21 @@ public function getControls(): Iterator } /** - * @return array + * @return Iterator */ - public function getContainers(): iterable + public function getContainers(): Iterator { $this->createCopies(); + /** @var iterable $components */ + $components = $this->getComponents(); + // Support both component-model ≥ 3.1 and < 3.1 + $components = is_array($components) ? $components : iterator_to_array($components); + /** @var array $containers */ - $containers = array_filter($this->getComponents(), fn ($component) => $component instanceof Container); + $containers = array_filter($components, fn ($component) => $component instanceof Container); - return $containers; + return new \ArrayIterator($containers); } /** @@ -385,7 +392,12 @@ protected function loadHttpData(): void protected function createNumber(): int { - $count = count(array_filter($this->getComponents(), fn ($component) => $component instanceof Form)); + /** @var iterable $components */ + $components = $this->getComponents(); + // Support both component-model ≥ 3.1 and < 3.1 + $components = is_array($components) ? $components : iterator_to_array($components); + + $count = count(array_filter($components, fn ($component) => $component instanceof Form)); while ($this->getComponent((string) $count, false)) { $count++; } @@ -420,7 +432,11 @@ protected function createContainer(): Container */ protected function getFirstSubmit(): ?string { - $submits = array_filter($this->getComponents(), fn ($component) => $component instanceof SubmitButton); + /** @var iterable $components */ + $components = $this->getComponents(); + // Support both component-model ≥ 3.1 and < 3.1 + $components = is_array($components) ? $components : iterator_to_array($components); + $submits = array_filter($components, fn ($component) => $component instanceof SubmitButton); if ($submits) { return reset($submits)->getName(); }