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

Synonyms   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 10
c 2
b 1
f 0
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSynonyms() 0 7 3
A getSynonymsAsString() 0 8 2
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