Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Gender
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 process
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\Leximorph\Handler;
8
9/**
10 * Gender
11 *
12 * The Gender class selects the appropriate text variant based on a provided gender value.
13 * It accepts a gender identifier along with an array of text options for male, female, and other,
14 * returning the corresponding text.
15 *
16 * Usage Example:
17 * <code>
18 *            echo $gender->process( 'female', [ 'he', 'she', 'they' ] );
19 * </code>
20 *
21 * @since     1.45
22 * @author    Doğu Abaris (abaris@null.net)
23 * @license   https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
24 */
25class Gender {
26
27    /**
28     * Selects the appropriate text variant based on gender.
29     *
30     * This method converts the provided gender value to lowercase and returns the text variant
31     * from the supplied options that corresponds to 'male', 'female', or a default value for any
32     * other or unspecified gender.
33     *
34     * @param string $value The gender value (e.g., "male", "female", or another).
35     * @param string[] $options An array containing text variants for male, female, and other.
36     *                          All three elements are optional.
37     *
38     * @since 1.45
39     * @return string The text variant that matches the provided gender.
40     */
41    public function process( string $value, array $options ): string {
42        $male = $options[0] ?? '';
43        $female = $options[1] ?? $male;
44        $other = $options[2] ?? $male;
45
46        if ( $value === 'male' ) {
47            return $male;
48        } elseif ( $value === 'female' ) {
49            return $female;
50        }
51
52        return $other;
53    }
54}