Passed
Push — master ( 982e46...a8ac91 )
by Timo
19:05
created

QueryFields::toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 <[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 QueryFields class holds all information for the query which fields should be used to query (Solr qf parameter).
31
 *
32
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
33
 */
34
class QueryFields implements ParameterBuilder
35
{
36
37
    /**
38
     * @var array
39
     */
40
    protected $queryFields = [];
41
42
    /**
43
     * QueryFields constructor.
44
     *
45
     * private constructor should only be created with the from* methods
46
     *
47
     * @param array $queryFields
48
     */
49
    private function __construct(array $queryFields)
50
    {
51
        $this->queryFields = $queryFields;
52
    }
53
54
    /**
55
     * @param string $fieldName
56
     * @param float $boost
57
     */
58
    public function set($fieldName, $boost = 1.0)
59
    {
60
        $this->queryFields[$fieldName] = (float)$boost;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function build()
67
    {
68
        $string = $this->toString();
69
        // return empty array on empty string
70
        return trim($string) === '' ? [] : ['qf' => $string];
71
    }
72
73
    /**
74
     * Creates the string representation
75
     *
76
     * @param string $delimiter
77
     * @return string
78
     */
79
    public function toString($delimiter = ' ') {
80
        $queryFieldString = '';
81
82
        foreach ($this->queryFields as $fieldName => $fieldBoost) {
83
            $queryFieldString .= $fieldName;
84
85
            if ($fieldBoost != 1.0) {
86
                $queryFieldString .= '^' . number_format($fieldBoost, 1, '.', '');
87
            }
88
89
            $queryFieldString .= $delimiter;
90
        }
91
92
        return rtrim($queryFieldString, $delimiter);
93
    }
94
95
    /**
96
     * Parses the string representation of the queryFields (e.g. content^100, title^10) to the object representation.
97
     *
98
     * @param string $queryFieldsString
99
     * @param string $delimiter
100
     * @return QueryFields
101
     */
102
    public static function fromString($queryFieldsString, $delimiter = ',') {
103
        $fields = GeneralUtility::trimExplode($delimiter, $queryFieldsString, true);
104
        $queryFields = [];
105
106
        foreach ($fields as $field) {
107
            $fieldNameAndBoost = explode('^', $field);
108
109
            $boost = 1.0;
110
            if (isset($fieldNameAndBoost[1])) {
111
                $boost = floatval($fieldNameAndBoost[1]);
112
            }
113
114
            $fieldName = $fieldNameAndBoost[0];
115
            $queryFields[$fieldName] = $boost;
116
        }
117
118
        return new QueryFields($queryFields);
119
    }
120
}