Skip to content

Commit 80852dc

Browse files
committed
Allow the iterating over existing data
As a side effect of this data previously submitted in a collection that wasn't completely valid will no longer be lost
1 parent ac19435 commit 80852dc

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

src/Bridge/Interaction/CollectionInteractor.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,24 @@ public function interactWith(
6262

6363
$submittedData = [];
6464
$prototype = $form->getConfig()->getAttribute('prototype');
65+
$askIfEntryNeedsToBeSubmitted = function ($entryNumber) use ($helperSet, $input, $output) {
66+
return $this->askIfExistingEntryShouldBeAdded($helperSet, $input, $output, $entryNumber);
67+
};
6568

66-
while ($this->askIfContinueToAdd($helperSet, $input, $output)) {
67-
$submittedData[] = $this->formInteractor->interactWith($prototype, $helperSet, $input, $output);
69+
foreach ((array) $form->getData() as $key => $entryData) {
70+
$this->printEntryHeader($key, $output);
71+
$prototype->setData($entryData);
72+
73+
$submittedEntry = $this->formInteractor->interactWith($prototype, $helperSet, $input, $output);
74+
if (!$form->getConfig()->getOption('allow_delete') || $askIfEntryNeedsToBeSubmitted($key)) {
75+
$submittedData[] = $submittedEntry;
76+
}
77+
}
78+
79+
if ($form->getConfig()->getOption('allow_add')) {
80+
while ($this->askIfContinueToAdd($helperSet, $input, $output)) {
81+
$submittedData[] = $this->formInteractor->interactWith($prototype, $helperSet, $input, $output);
82+
}
6883
}
6984

7085
return $submittedData;
@@ -117,4 +132,52 @@ private function printHeader(FormInterface $form, OutputInterface $output)
117132
)
118133
);
119134
}
135+
136+
/**
137+
* @param int $entryNumber
138+
* @param OutputInterface $output
139+
*/
140+
private function printEntryHeader($entryNumber, OutputInterface $output)
141+
{
142+
$output->writeln(
143+
strtr(
144+
'<fieldset>Edit entry {entryNumber}</fieldset>',
145+
[
146+
'{entryNumber}' => $entryNumber,
147+
]
148+
)
149+
);
150+
}
151+
152+
/**
153+
* @param HelperSet $helperSet
154+
* @param InputInterface $input
155+
* @param OutputInterface $output
156+
* @param int $entryNumber
157+
*
158+
* @return string
159+
*/
160+
private function askIfExistingEntryShouldBeAdded(
161+
HelperSet $helperSet,
162+
InputInterface $input,
163+
OutputInterface $output,
164+
$entryNumber
165+
) {
166+
return $this->questionHelper($helperSet)->ask(
167+
$input,
168+
$output,
169+
new ConfirmationQuestion(
170+
Format::forQuestion(
171+
strtr(
172+
'Add entry {entryNumber} to the submitted entries?',
173+
[
174+
'{entryNumber}' => $entryNumber,
175+
]
176+
),
177+
'y'
178+
),
179+
true
180+
)
181+
);
182+
}
120183
}

0 commit comments

Comments
 (0)