Completed
Push — master ( cefe26...80523f )
by Timo
23:41
created

AbstractFieldList::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 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\Utility\GeneralUtility;
28
29
/**
30
 * The AbstractFieldList class
31
 */
32
abstract class AbstractFieldList implements ParameterBuilder
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
     * private constructor should only be created with the from* methods
50
     *
51
     * @param array $fieldList
52
     */
53 135
    private function __construct(array $fieldList)
54
    {
55 135
        $this->fieldList = $fieldList;
56 135
    }
57
58
    /**
59
     * @param string $fieldName
60
     * @param float $boost
61
     *
62
     * @return ParameterBuilder
63
     */
64 7
    public function add(string $fieldName, float $boost = 1.0): ParameterBuilder
65
    {
66 7
        $this->fieldList[$fieldName] = (float)$boost;
67 7
        return $this;
68
    }
69
70
    /**
71
     * @return array
72
     */
73 9
    public function build() : array
74
    {
75 9
        $string = $this->toString();
76
        // return empty array on empty string
77 9
        return trim($string) === '' ? [] : [$this->parameterKey => $string];
78
    }
79
80
    /**
81
     * Creates the string representation
82
     *
83
     * @param string $delimiter
84
     * @return string
85
     */
86 11
    public function toString(string $delimiter = ' ')
87
    {
88 11
        $fieldListString = '';
89
90 11
        foreach ($this->fieldList as $fieldName => $fieldBoost) {
91 9
            $fieldListString .= $fieldName;
92
93 9
            if ($fieldBoost != 1.0) {
94 9
                $fieldListString .= '^' . number_format($fieldBoost, 1, '.', '');
95
            }
96
97 9
            $fieldListString .= $delimiter;
98
        }
99
100 11
        return rtrim($fieldListString, $delimiter);
101
    }
102
103
    /**
104
     * Parses the string representation of the fieldList (e.g. content^100, title^10) to the object representation.
105
     *
106
     * @param string $fieldListString
107
     * @param string $delimiter
108
     * @return AbstractFieldList
109
     */
110 135
    protected static function initializeFromString(string $fieldListString, string $delimiter = ',') : AbstractFieldList
111
    {
112 135
        $fields = GeneralUtility::trimExplode($delimiter, $fieldListString, true);
113 135
        $fieldList = [];
114
115 135
        foreach ($fields as $field) {
116 5
            $fieldNameAndBoost = explode('^', $field);
117
118 5
            $boost = 1.0;
119 5
            if (isset($fieldNameAndBoost[1])) {
120 5
                $boost = floatval($fieldNameAndBoost[1]);
121
            }
122
123 5
            $fieldName = $fieldNameAndBoost[0];
124 5
            $fieldList[$fieldName] = $boost;
125
        }
126
127 135
        return new static($fieldList);
128
    }
129
}
130