Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

AbstractFieldList::initializeFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
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 3 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\Utility\GeneralUtility;
28
29
/**
30
 * The AbstractFieldList class
31
 */
32
abstract class AbstractFieldList extends AbstractDeactivatable
33
{
34
    /**
35
     * @var array
36
     */
37
    protected $fieldList = [];
38
39
    /**
40
     * Parameter key which should be used for Apache Solr URL query
41
     *
42
     * @var string
43
     */
44
    protected $parameterKey = '';
45
46
    /**
47
     * FieldList parameter builder constructor.
48
     *
49
     * @param array $fieldList
50
     */
51 141
    public function __construct($isEnabled, array $fieldList = [])
52
    {
53 141
        $this->isEnabled = $isEnabled;
54 141
        $this->fieldList = $fieldList;
55 141
    }
56
57
    /**
58
     * @param string $fieldListString
59
     * @param string $delimiter
60
     * @return array
61
     */
62 13
    protected static function buildFieldList(string $fieldListString, string $delimiter):array
63
    {
64 13
        $fields = GeneralUtility::trimExplode($delimiter, $fieldListString, true);
65 13
        $fieldList = [];
66
67 13
        foreach ($fields as $field) {
68 9
            $fieldNameAndBoost = explode('^', $field);
69
70 9
            $boost = 1.0;
71 9
            if (isset($fieldNameAndBoost[1])) {
72 9
                $boost = floatval($fieldNameAndBoost[1]);
73
            }
74
75 9
            $fieldName = $fieldNameAndBoost[0];
76 9
            $fieldList[$fieldName] = $boost;
77
        }
78 13
        return $fieldList;
79
    }
80
81
    /**
82
     * @param string $fieldName
83
     * @param float $boost
84
     *
85
     * @return AbstractFieldList
86
     */
87 7
    public function add(string $fieldName, float $boost = 1.0): AbstractFieldList
88
    {
89 7
        $this->fieldList[$fieldName] = (float)$boost;
90 7
        return $this;
91
    }
92
93
    /**
94
     * Creates the string representation
95
     *
96
     * @param string $delimiter
97
     * @return string
98
     */
99 141
    public function toString(string $delimiter = ' ')
100
    {
101 141
        $fieldListString = '';
102
103 141
        foreach ($this->fieldList as $fieldName => $fieldBoost) {
104 16
            $fieldListString .= $fieldName;
105
106 16
            if ($fieldBoost != 1.0) {
107 16
                $fieldListString .= '^' . number_format($fieldBoost, 1, '.', '');
108
            }
109
110 16
            $fieldListString .= $delimiter;
111
        }
112
113 141
        return rtrim($fieldListString, $delimiter);
114
    }
115
}
116