Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(forms): centralize end user input name
- Loading branch information
Showing
9 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* --------------------------------------------------------------------- | ||
* | ||
* GLPI - Gestionnaire Libre de Parc Informatique | ||
* | ||
* http://glpi-project.org | ||
* | ||
* @copyright 2015-2024 Teclib' and contributors. | ||
* @copyright 2003-2014 by the INDEPNET Development Team. | ||
* @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
* | ||
* --------------------------------------------------------------------- | ||
* | ||
* LICENSE | ||
* | ||
* This file is part of GLPI. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* --------------------------------------------------------------------- | ||
*/ | ||
|
||
namespace Glpi\Form; | ||
|
||
/** | ||
* Utility class to provide the end user input name | ||
*/ | ||
final class EndUserInputNameProvider | ||
{ | ||
public const END_USER_INPUT_NAME = 'answers_%d'; | ||
public const END_USER_INPUT_NAME_REGEX = '/^_?answers_(\d+)$/'; | ||
|
||
/** | ||
* Get the end user input name for a given question | ||
* | ||
* @param Question $question | ||
* @return string | ||
*/ | ||
public function getEndUserInputName(Question $question): string | ||
{ | ||
return sprintf(self::END_USER_INPUT_NAME, $question->getID()); | ||
} | ||
|
||
/** | ||
* Get the answers submitted by the end user | ||
* The answers are indexed by question ID | ||
* | ||
* @param array $inputs The inputs submitted by the end user | ||
* @return array | ||
*/ | ||
public function getAnswers(array $inputs): array | ||
{ | ||
$filteredAnswers = self::filterAnswers($inputs); | ||
$reindexedAnswers = self::reindexAnswers($filteredAnswers); | ||
|
||
return $reindexedAnswers; | ||
} | ||
|
||
/** | ||
* Filter the answers submitted by the end user | ||
* Only the answers that match the end user input name pattern are kept | ||
* | ||
* @param array $answers | ||
* @return array | ||
*/ | ||
private function filterAnswers(array $answers): array | ||
{ | ||
return array_filter( | ||
$answers, | ||
function ($key) { | ||
return preg_match(self::END_USER_INPUT_NAME_REGEX, $key); | ||
}, | ||
ARRAY_FILTER_USE_KEY | ||
); | ||
} | ||
|
||
/** | ||
* Reindex the answers submitted by the end user | ||
* The answers are indexed by question ID | ||
* | ||
* @param array $answers | ||
* @return array | ||
*/ | ||
private function reindexAnswers(array $answers): array | ||
{ | ||
return array_reduce( | ||
array_keys($answers), | ||
function ($carry, $key) use ($answers) { | ||
$question_id = (int) preg_replace(self::END_USER_INPUT_NAME_REGEX, '$1', $key); | ||
$carry[$question_id] = $answers[$key]; | ||
return $carry; | ||
}, | ||
[] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/** | ||
* --------------------------------------------------------------------- | ||
* | ||
* GLPI - Gestionnaire Libre de Parc Informatique | ||
* | ||
* http://glpi-project.org | ||
* | ||
* @copyright 2015-2024 Teclib' and contributors. | ||
* @copyright 2003-2014 by the INDEPNET Development Team. | ||
* @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
* | ||
* --------------------------------------------------------------------- | ||
* | ||
* LICENSE | ||
* | ||
* This file is part of GLPI. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* --------------------------------------------------------------------- | ||
*/ | ||
|
||
namespace tests\units\Glpi\Form; | ||
|
||
use DbTestCase; | ||
use Glpi\Form\QuestionType\QuestionTypeShortText; | ||
use Glpi\Tests\FormBuilder; | ||
use Glpi\Tests\FormTesterTrait; | ||
|
||
class EndUserInputNameProvider extends DbTestCase | ||
{ | ||
use FormTesterTrait; | ||
|
||
public function testGetEndUserInputName() | ||
{ | ||
// Create a new form | ||
$form = $this->createForm( | ||
(new FormBuilder()) | ||
->addQuestion('Name', QuestionTypeShortText::class) | ||
); | ||
|
||
// Check that the end user input name contains the question ID | ||
// and if the regex match with the generated end user input name | ||
foreach ($form->getQuestions() as $question) { | ||
$this->string($question->getEndUserInputName()) | ||
->contains($question->getID()) | ||
->match(\Glpi\Form\EndUserInputNameProvider::END_USER_INPUT_NAME_REGEX); | ||
} | ||
} | ||
|
||
public function testGetAnswers() | ||
{ | ||
// Create a new form | ||
$form = $this->createForm( | ||
(new FormBuilder()) | ||
->addQuestion('Name', QuestionTypeShortText::class) | ||
->addQuestion('Email', QuestionTypeShortText::class) | ||
); | ||
|
||
// Generate the answers | ||
$inputs = [ | ||
$form->getQuestions()[array_keys($form->getQuestions())[0]]->getEndUserInputName() => 'John Doe', | ||
$form->getQuestions()[array_keys($form->getQuestions())[1]]->getEndUserInputName() => 'john.doe@mail.mail', | ||
'invalid_input' => 'invalid_value', | ||
]; | ||
|
||
// Check that the answers are correctly indexed by question ID | ||
$this->array((new \Glpi\Form\EndUserInputNameProvider())->getAnswers($inputs)) | ||
->hasSize(2) | ||
->isEqualTo([ | ||
$form->getQuestions()[array_keys($form->getQuestions())[0]]->getID() => 'John Doe', | ||
$form->getQuestions()[array_keys($form->getQuestions())[1]]->getID() => 'john.doe@mail.mail', | ||
]); | ||
} | ||
} |