generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix survey mapping to include all currently used question types
- Loading branch information
Showing
19 changed files
with
321 additions
and
105 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
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,15 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Exceptions; | ||
|
||
class QuestionMapperNotImplementedException extends \Exception { | ||
public string $questionType; | ||
|
||
public static function create(string $questionType): self | ||
{ | ||
$ex = new self("There is no mapper implementation for question type: $questionType"); | ||
$ex->questionType = $questionType; | ||
|
||
return $ex; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/Services/Factories/QuestionMapper/AbstractQuestionMapper.php
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,23 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
use Statikbe\Surveyhero\Models\SurveyAnswer; | ||
|
||
abstract class AbstractQuestionMapper implements QuestionMapper { | ||
/** | ||
* @param int $questionId | ||
* @param string $questionType | ||
* @param string $mappedDataType | ||
* @param int|string $questionFieldSuffix | ||
* @return array{'question_id': int, 'type': string, 'field': string, 'mapped_data_type': string } | ||
*/ | ||
public function createQuestionMap(int $questionId, string $questionType, string $mappedDataType, int|string $questionFieldSuffix): array { | ||
return [ | ||
'question_id' => $questionId, | ||
'type' => $questionType, | ||
'field' => 'question_'.$questionFieldSuffix, | ||
'mapped_data_type' => $mappedDataType, | ||
]; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Services/Factories/QuestionMapper/ChoiceListQuestionMapper.php
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,22 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
use Statikbe\Surveyhero\Models\SurveyAnswer; | ||
use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceListQuestionAndAnswerCreator; | ||
|
||
class ChoiceListQuestionMapper extends AbstractQuestionMapper { | ||
const TYPE = 'choice_list'; | ||
|
||
public function mapQuestion(\stdClass $question, int $questionCounter): array { | ||
$questionData = $this->createQuestionMap($question->element_id, | ||
$question->question->type, | ||
SurveyAnswer::CONVERTED_TYPE_INT, | ||
$questionCounter); | ||
|
||
foreach ($question->question->choice_list->choices as $choiceKey => $choice) { | ||
$questionData['answer_mapping'][$choice->choice_id] = $choiceKey + 1; | ||
} | ||
return $questionData; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Services/Factories/QuestionMapper/ChoiceTableQuestionMapper.php
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,34 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
use Statikbe\Surveyhero\Models\SurveyAnswer; | ||
|
||
class ChoiceTableQuestionMapper extends AbstractQuestionMapper { | ||
const TYPE = 'choice_table'; | ||
|
||
public function mapQuestion(\stdClass $question, int $questionCounter): array { | ||
$mappedQuestions = []; | ||
$subquestionIndex = 1; | ||
// make answer mapping which is the same for each question: | ||
$answerMapping = []; | ||
$choiceCounter = 1; | ||
foreach($question->question->choice_table->choices as $questionChoice){ | ||
$answerMapping[$questionChoice->choice_id] = $choiceCounter; | ||
$choiceCounter ++; | ||
} | ||
|
||
//create subquestions: | ||
foreach ($question->question->choice_table->rows as $rowQuestion){ | ||
$questionData = $this->createQuestionMap($rowQuestion->row_id, | ||
$question->question->type, | ||
SurveyAnswer::CONVERTED_TYPE_INT, | ||
"{$questionCounter}_{$subquestionIndex}"); | ||
|
||
$questionData['answer_mapping'] = $answerMapping; | ||
$mappedQuestions[] = $questionData; | ||
} | ||
|
||
return $mappedQuestions; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Services/Factories/QuestionMapper/InputQuestionMapper.php
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,18 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
use Statikbe\Surveyhero\Models\SurveyAnswer; | ||
|
||
class InputQuestionMapper extends AbstractQuestionMapper { | ||
const TYPE = 'input'; | ||
|
||
public function mapQuestion(\stdClass $question, int $questionCounter): array { | ||
$questionData = $this->createQuestionMap($question->element_id, | ||
$question->question->type, | ||
SurveyAnswer::CONVERTED_TYPE_STRING, | ||
$questionCounter); | ||
|
||
return $questionData; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
interface QuestionMapper { | ||
public function mapQuestion(\stdClass $question, int $questionCounter): array ; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Services/Factories/QuestionMapper/RatingScaleQuestionMapper.php
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,22 @@ | ||
<?php | ||
|
||
namespace Statikbe\Surveyhero\Services\Factories\QuestionMapper; | ||
|
||
use Statikbe\Surveyhero\Models\SurveyAnswer; | ||
|
||
class RatingScaleQuestionMapper extends AbstractQuestionMapper { | ||
const TYPE = 'rating_scale'; | ||
|
||
public function mapQuestion(\stdClass $question, int $questionCounter): array { | ||
$questionData = $this->createQuestionMap($question->element_id, | ||
$question->question->type, | ||
SurveyAnswer::CONVERTED_TYPE_STRING, | ||
$questionCounter); | ||
|
||
if ($question->question->rating_scale->style == 'numerical_scale') { | ||
$questionData['mapped_data_type'] = SurveyAnswer::CONVERTED_TYPE_INT; | ||
} | ||
|
||
return $questionData; | ||
} | ||
} |
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
Oops, something went wrong.