Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Plugin / search_api_synonym / export / Solr.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Plugin\search_api_synonym\export;
4
5 use Drupal\search_api_synonym\Export\ExportPluginBase;
6 use Drupal\search_api_synonym\Export\ExportPluginInterface;
7
8 /**
9  * Provides a synonym export plugin for Apache Solr..
10  *
11  * @SearchApiSynonymExport(
12  *   id = "solr",
13  *   label = @Translation("Solr"),
14  *   description = @Translation("Synonym export plugin for Apache Solr")
15  * )
16  */
17 class Solr extends ExportPluginBase implements ExportPluginInterface {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getFormattedSynonyms(array $synonyms) {
23     $lines = [];
24
25     $lines[] = "#";
26     $lines[] = "# Synonyms file for Apache Solr generated by Search API Synonym.";
27     $lines[] = "# See file https://www.drupal.org/project/search_api_synonym.";
28     $lines[] = "#";
29     $lines[] = "";
30
31     // Generate a line for each synonym.
32     foreach ($synonyms as $synonym) {
33       $lines[] = $this->generateLine($synonym->word, $synonym->synonyms, $synonym->type);
34     }
35
36     return implode("\n", $lines);
37   }
38
39   /**
40    * Generate a single synonyms line for the export file.
41    *
42    * @param string $word
43    *   The main word.
44    *
45    * @param string $synonyms
46    *   The comma separated string with synonyms.
47    *
48    * @param string $type
49    *   Synonym (synonym) og Spelling error (spelling_error)
50    *
51    * @return string
52    *   Return the single line with synonyms and the corresponding word.
53    */
54   private function generateLine($word, $synonyms, $type) {
55     $line = '';
56
57     switch ($type) {
58       case 'synonym':
59         // We force using of equivalent mappings for type = synonym.
60         $line = "{$word}, {$synonyms}";
61         break;
62       case 'spelling_error':
63         $line = "{$word} => {$synonyms}";
64         break;
65     }
66     return $line;
67   }
68
69 }