Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Formal
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\Leximorph\Handler;
8
9use Wikimedia\Leximorph\Provider;
10
11/**
12 * Formal
13 *
14 * The Formal class selects the appropriate text form (formal or informal)
15 * based on a language-specific formality index. It loads the index from a JSON file
16 * and returns either the formal or informal variant provided in the options.
17 *
18 * Usage Example:
19 * <code>
20 *            echo $formal->process( [ 'Du hast', 'Sie haben' ] );
21 * </code>
22 *
23 * @since     1.45
24 * @author    Doğu Abaris (abaris@null.net)
25 * @license   https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
26 */
27class Formal {
28
29    /**
30     * Initializes the Formal handler with the provider.
31     *
32     * @param Provider $provider The provider instance to use.
33     *
34     * @since 1.45
35     */
36    public function __construct(
37        private readonly Provider $provider,
38    ) {
39    }
40
41    /**
42     * Selects the appropriate text form based on formality.
43     *
44     * Given an array of options containing a formal form and an informal form (with the informal form
45     * defaulting to the formal form if not provided), this method returns the form that matches the
46     * language-specific formality index loaded from configuration.
47     *
48     * @param string[] $options An array with the formal and informal text variants.
49     *
50     * @since 1.45
51     * @return string The text variant corresponding to the determined formality.
52     */
53    public function process( array $options ): string {
54        $informal = $options[0];
55        $formal = $options[1] ?? $informal;
56
57        $index = $this->provider->getFormalityIndexProvider()->getFormalityIndex();
58
59        return ( $index === 1 ) ? $formal : $informal;
60    }
61}