Passed
Push — hans/logtests ( c24cf9...971601 )
by Simon
07:03
created

Synonyms::getSynonyms()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Helpers;
5
6
use SilverStripe\Core\Config\Configurable;
7
8
/**
9
 * Class Synonyms
10
 * Source: @link https://raw.githubusercontent.com/heiswayi/spelling-uk-vs-us/
11
 * @package Firesphere\SolrSearch\Helpers
12
 */
13
class Synonyms
14
{
15
    use Configurable;
16
17
    /**
18
     * @var array Synonym list
19
     */
20
    protected static $synonyms;
21
22
    /**
23
     * Make the UK to US spelling synonyms as a newline separated string
24
     * Or any other synonyms defined if the user wishes to do so
25
     * @param bool $defaults add Default UK-US synonyms to the list
26
     * @return string
27
     */
28 28
    public static function getSynonymsAsString($defaults = true)
29
    {
30 28
        $result = [];
31 28
        foreach (static::getSynonyms($defaults) as $synonym) {
32 28
            $result[] = implode(',', $synonym);
33
        }
34
35 28
        return implode(PHP_EOL, $result) . PHP_EOL;
36
    }
37
38
    /**
39
     * Get the available synonyms as an array from config
40
     * Defaulting to adding the UK to US spelling differences
41
     * @param bool $defaults adds the UK to US spelling to the list if true
42
     * @return array
43
     */
44 29
    public static function getSynonyms($defaults = true)
45
    {
46
        // If we want the defaults, load it in to the array, otherwise return an empty array
47 29
        $usuk = $defaults ? static::config()->get('usuk')['synonyms'] : [];
48 29
        $synonyms = static::config()->get('synonyms') ?: [];
49
50 29
        return array_merge($usuk, $synonyms);
51
    }
52
}
53