Passed
Push — master ( 2be180...c7e844 )
by Timo
04:30
created

importStopwordsFromPlainTextContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Utility;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Http\Stream;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Class ManagedResourcesUtility
32
 */
33
class ManagedResourcesUtility
34
{
35
36
    /**
37
     * Export synonyms in plain text format
38
     * @param array $synonyms
39
     * @return string
40
     */
41
    public static function exportSynonymsToTxt(array $synonyms) : string
42
    {
43
        if (empty($synonyms)) {
44
            throw new \InvalidArgumentException('Nothing to export!', 1502978329);
45
        }
46
47
        $contentLines = '';
48
        foreach ($synonyms as $synonymBaseWord => $synonymWords) {
49
            $contentLines .= $synonymBaseWord . ' => ' . implode(',', $synonymWords) . PHP_EOL;
50
        }
51
        return rtrim($contentLines);
52
    }
53
54
    /**
55
     * Read plain text synonym file and import these synonyms
56
     * @param array $synonymFileUpload
57
     * @return array
58
     */
59
    public static function importSynonymsFromPlainTextContents(array $synonymFileUpload) : array
60
    {
61
        $fileStream = new Stream($synonymFileUpload['tmp_name']);
62
63
        $fileLines = GeneralUtility::trimExplode(PHP_EOL, $fileStream->getContents(), true);
64
65
        $synonymList = [];
66
        foreach ($fileLines as $line) {
67
            $synonymList = self::convertSynonymFileLineForImport($line, $synonymList);
68
        }
69
70
        return $synonymList;
71
    }
72
73
    /**
74
     * Read plain text stopword file
75
     * @param array $stopwordsFileUpload
76
     * @return string
77
     */
78
    public static function importStopwordsFromPlainTextContents(array $stopwordsFileUpload) : string
79
    {
80
        $fileStream = new Stream($stopwordsFileUpload['tmp_name']);
81
82
        return $fileStream->getContents();
83
    }
84
85
    /**
86
     * Convert synonym file line for import
87
     * @param string $line
88
     * @param array $synonymList
89
     * @return array
90
     */
91
    protected static function convertSynonymFileLineForImport($line, $synonymList) : array
92
    {
93
        $lineParts = GeneralUtility::trimExplode('=>', $line, true);
94
95
        if (isset($lineParts[1])) {
96
            $baseWord = mb_strtolower($lineParts[0]);
97
            $synonyms = GeneralUtility::trimExplode(',', mb_strtolower($lineParts[1]), true);
98
        } else {
99
            $synonyms = GeneralUtility::trimExplode(',', mb_strtolower($lineParts[0]), true);
100
            $baseWord = mb_strtolower(reset($synonyms));
101
        }
102
        $synonymList[$baseWord] = $synonyms;
103
104
        return $synonymList;
105
    }
106
}
107