1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class Synonyms|Firesphere\SearchBackend\Helpers\Synonyms Get the default UK-US synonyms helper |
4
|
|
|
* |
5
|
|
|
* @package Firesphere\Search\Backend |
6
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
7
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Firesphere\SearchBackend\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\Search\Backend |
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
|
|
|
public static function getSynonymsAsString($defaults = true) |
38
|
|
|
{ |
39
|
|
|
$result = []; |
40
|
|
|
foreach (static::getSynonyms($defaults) as $word => $synonym) { |
41
|
|
|
// Make all synonym strings |
42
|
|
|
// @todo remove duplicates |
43
|
|
|
$synonym = implode(',', (array)$synonym); |
44
|
|
|
$result[] = sprintf('%s,%s', $word, $synonym); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
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
|
|
|
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
|
|
|
$usuk = $defaults ? static::config()->get('usuk')['synonyms'] : []; |
61
|
|
|
$synonyms = static::config()->get('synonyms') ?: []; |
62
|
|
|
|
63
|
|
|
return array_merge($usuk, $synonyms); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function getSynonymsFlattened($defaults = true) |
67
|
|
|
{ |
68
|
|
|
$synonyms = self::getSynonyms($defaults); |
69
|
|
|
foreach ($synonyms as $key => &$value) { |
70
|
|
|
if (is_array($value)) { |
71
|
|
|
$value = implode(',', $value); |
72
|
|
|
} |
73
|
|
|
$value = sprintf('%s,%s', $key, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $synonyms; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|