Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SurveyQuestion
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMessages
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3namespace QuickSurveys;
4
5use Wikimedia\Assert\ParameterTypeException;
6
7class SurveyQuestion extends Schema {
8
9    /**
10     * A list of question dependencies and their required types.
11     */
12    private const VALID_DEPENDS_ON_KEYS = [
13        'question' => 'string',
14        'answerIsOneOf' => 'array',
15    ];
16
17    /**
18     * A list of accepted keys for answers and their required types.
19     */
20    private const VALID_ANSWER_KEYS = [
21        'label' => 'string',
22        'freeformTextLabel' => 'string',
23    ];
24
25    /**
26     * A list of accepted keys for internal survey questions and their required types.
27     */
28    private const VALID_INTERNAL_QUESTION_KEYS = [
29        'name' => 'string',
30        'layout' => 'string',
31        'question' => 'string',
32        'description' => 'string',
33        'shuffleAnswersDisplay' => 'boolean',
34        'answers' => [ self::ARRAY_OF, self::VALID_ANSWER_KEYS ],
35        'dependsOn' => [ self::ARRAY_OF, self::VALID_DEPENDS_ON_KEYS ],
36    ];
37
38    /**
39     * A list of accepted keys for external survey questions and their required types.
40     */
41    private const VALID_EXTERNAL_QUESTION_KEYS = [
42        'name' => 'string',
43        'question' => 'string',
44        'description' => 'string',
45        'link' => 'string',
46        'instanceTokenParameterName' => 'string',
47        'yesMsg' => 'string',
48        'noMsg' => 'string',
49    ];
50
51    /**
52     * Validate a survey question definition against the provided survey type.
53     *
54     * @param array $questionDefinition defining the question with keys
55     *     that match the available keys defined in VALID_INTERNAL_QUESTION_KEYS
56     * @param string $surveyType the type of survey this question is meant for
57     * @throws ParameterTypeException when a key has the wrong type
58     */
59    public function __construct( array $questionDefinition, string $surveyType ) {
60        $surveyType === 'internal'
61            ? parent::__construct( $questionDefinition, self::VALID_INTERNAL_QUESTION_KEYS )
62            : parent::__construct( $questionDefinition, self::VALID_EXTERNAL_QUESTION_KEYS );
63    }
64
65    public function getMessages(): array {
66        // List the keys that need translation from the main question array
67        $translationKeys = [ 'question', 'description', 'link', 'yesMsg', 'noMsg' ];
68        $question = $this->toArray();
69        $messages = [];
70
71        foreach ( $translationKeys as $key ) {
72            if ( isset( $question[ $key ] ) ) {
73                $messages[] = $question[ $key ];
74            }
75        }
76
77        // Handle the `answers` key separately
78        if ( isset( $question[ 'answers' ] ) &&
79            is_array( $question[ 'answers' ] ) ) {
80            foreach ( $question[ 'answers' ] as $answer ) {
81                if ( isset( $answer[ 'label' ] ) ) {
82                    $messages[] = $answer[ 'label' ];
83                }
84                if ( isset( $answer[ 'freeformTextLabel' ] ) ) {
85                    $messages[] = $answer[ 'freeformTextLabel' ];
86                }
87            }
88        }
89
90        return $messages;
91    }
92}