Synonyms::getSynonymsAsString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * class Synonyms|Firesphere\SolrSearch\Helpers\Synonyms Get the default UK-US synonyms helper
4
 *
5
 * @package Firesphere\Solr\Search
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Helpers;
11
12
use SilverStripe\Core\Config\Configurable;
13
14
/**
15
 * Class Synonyms
16
 * List out UK to US synonyms and synonyms from {@link SiteConfig}
17
 * Source: @link https://raw.githubusercontent.com/heiswayi/spelling-uk-vs-us/
18
 *
19
 * @package Firesphere\Solr\Search
20
 */
21
class Synonyms
22
{
23
    use Configurable;
24
25
    /**
26
     * @var array Synonym list
27
     */
28
    protected static $synonyms;
29
30
    /**
31
     * Make the UK to US spelling synonyms as a newline separated string
32
     * Or any other synonyms defined if the user wishes to do so
33
     *
34
     * @param bool $defaults add Default UK-US synonyms to the list
35
     * @return string
36
     */
37 37
    public static function getSynonymsAsString($defaults = true)
38
    {
39 37
        $result = [];
40 37
        foreach (static::getSynonyms($defaults) as $word => $synonym) {
41
            // Make all synonym strings
42
            // @todo remove duplicates
43 37
            $synonym = implode(',', (array)$synonym);
44 37
            $result[] = sprintf('%s,%s', $word, $synonym);
45
        }
46
47 37
        return implode(PHP_EOL, $result) . PHP_EOL;
48
    }
49
50
    /**
51
     * Get the available synonyms as an array from config
52
     * Defaulting to adding the UK to US spelling differences
53
     *
54
     * @param bool $defaults adds the UK to US spelling to the list if true
55
     * @return array
56
     */
57 38
    public static function getSynonyms($defaults = true)
58
    {
59
        // If we want the defaults, load it in to the array, otherwise return an empty array
60 38
        $usuk = $defaults ? static::config()->get('usuk')['synonyms'] : [];
61 38
        $synonyms = static::config()->get('synonyms') ?: [];
62
63 38
        return array_merge($usuk, $synonyms);
64
    }
65
}
66