Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SurveyAudience
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace QuickSurveys;
4
5use Wikimedia\Assert\Assert;
6use Wikimedia\Assert\ParameterTypeException;
7
8class SurveyAudience {
9    /**
10     * a list of accepted keys and their required types
11     */
12    private const VALID_KEYS = [
13        'minEdits' => 'integer',
14        'maxEdits' => 'integer',
15        'countries' => 'array',
16        'anons' => 'boolean',
17        'registrationStart' => 'string',
18        'registrationEnd' => 'string',
19        'pageIds' => 'array',
20        'userAgent' => 'array',
21    ];
22
23    /**
24     * an internal description of the audience with validated data.
25     * @var array
26     */
27    private $audience;
28
29    /**
30     * @param array $audienceDefinition defining the audience with keys
31     *     that match the available keys defined in VALID_KEYS
32     * @throws ParameterTypeException when a key has the wrong type
33     */
34    public function __construct( array $audienceDefinition ) {
35        $audienceData = [];
36        foreach ( self::VALID_KEYS as $name => $type ) {
37            if ( array_key_exists( $name, $audienceDefinition ) ) {
38                Assert::parameterType( $type, $audienceDefinition[ $name ], $name );
39                // data is in the correct form so add.
40                $audienceData[$name] = $audienceDefinition[$name];
41            }
42        }
43        $this->audience = $audienceData;
44    }
45
46    /**
47     * Returns the JSON-encodable, minimal representation of the survey audience
48     *
49     * @return array
50     */
51    public function toArray(): array {
52        return $this->audience;
53    }
54}