Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GrammarFi
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 1
 process
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace Wikimedia\Leximorph\Handler\Overrides\Grammar;
8
9use Wikimedia\Leximorph\Handler\Overrides\IGrammarTransformer;
10
11/**
12 * GrammarFi
13 *
14 * Implements grammar transformations for Finnish (fi).
15 *
16 * These rules don't cover the whole grammar of the language.
17 * This logic was originally taken from MediaWiki Core.
18 * Thanks to all contributors.
19 *
20 * @since     1.45
21 * @author    Niklas Laxström
22 * @author    Doğu Abaris (abaris@null.net)
23 * @license   https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
24 */
25class GrammarFi implements IGrammarTransformer {
26    /**
27     * Applies Finnish-specific grammatical transformations.
28     *
29     * @param string $word The word to process.
30     * @param string $case The grammatical case.
31     *
32     * @since 1.45
33     * @return string The processed word.
34     */
35    public function process( string $word, string $case ): string {
36        # These rules don't cover the whole language.
37        # They are used only for site names.
38
39        # vowel harmony flag
40        $aou = preg_match( '/[aou][^äöy]*$/i', $word );
41
42        # The flag should be false for compounds where the last word has only neutral vowels (e/i).
43        # The general case cannot be handled without a dictionary, but there's at least one notable
44        # special case we should check for:
45
46        if ( preg_match( '/wiki$/i', $word ) ) {
47            $aou = false;
48        }
49
50        # append i after final consonant
51        if ( preg_match( '/[bcdfghjklmnpqrstvwxz]$/i', $word ) ) {
52            $word .= 'i';
53        }
54
55        switch ( $case ) {
56            case 'genitive':
57                $word .= 'n';
58                break;
59            case 'elative':
60                $word .= ( $aou ? 'sta' : 'stä' );
61                break;
62            case 'partitive':
63                $word .= ( $aou ? 'a' : 'ä' );
64                break;
65            case 'illative':
66                # Double the last letter and add 'n'
67                $word .= mb_substr( $word, -1 ) . 'n';
68                break;
69            case 'inessive':
70                $word .= ( $aou ? 'ssa' : 'ssä' );
71                break;
72            default:
73                break;
74        }
75
76        return $word;
77    }
78}